create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / motor / MotorInstanceConfiguration.java
1 package net.sf.openrocket.motor;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import net.sf.openrocket.models.atmosphere.AtmosphericConditions;
8 import net.sf.openrocket.rocketcomponent.MotorMount;
9 import net.sf.openrocket.util.Coordinate;
10 import net.sf.openrocket.util.Monitorable;
11
12 /**
13  * A configuration of motor instances identified by a string id.  Each motor instance has
14  * an individual position, ingition time etc.
15  * 
16  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17  */
18 public final class MotorInstanceConfiguration implements Monitorable, Cloneable {
19         
20         private final List<MotorId> ids = new ArrayList<MotorId>();
21         private final List<MotorId> unmodifiableIds = Collections.unmodifiableList(ids);
22         private final List<MotorInstance> motors = new ArrayList<MotorInstance>();
23         private final List<MotorMount> mounts = new ArrayList<MotorMount>();
24         private final List<Coordinate> positions = new ArrayList<Coordinate>();
25         private final List<Double> ignitionTimes = new ArrayList<Double>();
26         
27
28         private int modID = 0;
29         
30         
31         /**
32          * Add a motor instance to this configuration.  The motor is placed at
33          * the specified position and with an infinite ignition time (never ignited).
34          * 
35          * @param id            the ID of this motor instance.
36          * @param motor         the motor instance.
37          * @param mount         the motor mount containing this motor
38          * @param position      the position of the motor in absolute coordinates.
39          * @throws IllegalArgumentException     if a motor with the specified ID already exists.
40          */
41         public void addMotor(MotorId id, MotorInstance motor, MotorMount mount, Coordinate position) {
42                 if (this.ids.contains(id)) {
43                         throw new IllegalArgumentException("MotorInstanceConfiguration already " +
44                                         "contains a motor with id " + id);
45                 }
46                 this.ids.add(id);
47                 this.motors.add(motor);
48                 this.mounts.add(mount);
49                 this.positions.add(position);
50                 this.ignitionTimes.add(Double.POSITIVE_INFINITY);
51                 modID++;
52         }
53         
54         /**
55          * Return a list of all motor IDs in this configuration (not only ones in active stages).
56          */
57         public List<MotorId> getMotorIDs() {
58                 return unmodifiableIds;
59         }
60         
61         public MotorInstance getMotorInstance(MotorId id) {
62                 return motors.get(indexOf(id));
63         }
64         
65         public MotorMount getMotorMount(MotorId id) {
66                 return mounts.get(indexOf(id));
67         }
68         
69         public Coordinate getMotorPosition(MotorId id) {
70                 return positions.get(indexOf(id));
71         }
72         
73         public void setMotorPosition(MotorId id, Coordinate position) {
74                 positions.set(indexOf(id), position);
75                 modID++;
76         }
77         
78         public double getMotorIgnitionTime(MotorId id) {
79                 return ignitionTimes.get(indexOf(id));
80         }
81         
82         public void setMotorIgnitionTime(MotorId id, double time) {
83                 this.ignitionTimes.set(indexOf(id), time);
84                 modID++;
85         }
86         
87         
88
89         private int indexOf(MotorId id) {
90                 int index = ids.indexOf(id);
91                 if (index < 0) {
92                         throw new IllegalArgumentException("MotorInstanceConfiguration does not " +
93                                         "contain a motor with id " + id);
94                 }
95                 return index;
96         }
97         
98         
99
100         /**
101          * Step all of the motor instances to the specified time minus their ignition time.
102          * @param time  the "global" time
103          */
104         public void step(double time, double acceleration, AtmosphericConditions cond) {
105                 for (int i = 0; i < motors.size(); i++) {
106                         double t = time - ignitionTimes.get(i);
107                         if (t >= 0) {
108                                 motors.get(i).step(t, acceleration, cond);
109                         }
110                 }
111                 modID++;
112         }
113         
114         @Override
115         public int getModID() {
116                 int id = modID;
117                 for (MotorInstance motor : motors) {
118                         id += motor.getModID();
119                 }
120                 return id;
121         }
122         
123         /**
124          * Return a copy of this motor instance configuration with independent motor instances
125          * from this instance.
126          */
127         @Override
128         public MotorInstanceConfiguration clone() {
129                 MotorInstanceConfiguration clone = new MotorInstanceConfiguration();
130                 clone.ids.addAll(this.ids);
131                 clone.mounts.addAll(this.mounts);
132                 clone.positions.addAll(this.positions);
133                 clone.ignitionTimes.addAll(this.ignitionTimes);
134                 for (MotorInstance motor : this.motors) {
135                         clone.motors.add(motor.clone());
136                 }
137                 clone.modID = this.modID;
138                 return clone;
139         }
140         
141 }