ee54143fac4794edf9214fd67ea05820cf25ad1f
[debian/openrocket] / 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         public List<MotorId> getMotorIDs() {
55                 return unmodifiableIds;
56         }
57         
58         public MotorInstance getMotorInstance(MotorId id) {
59                 return motors.get(indexOf(id));
60         }
61         
62         public MotorMount getMotorMount(MotorId id) {
63                 return mounts.get(indexOf(id));
64         }
65         
66         public Coordinate getMotorPosition(MotorId id) {
67                 return positions.get(indexOf(id));
68         }
69         
70         public void setMotorPosition(MotorId id, Coordinate position) {
71                 positions.set(indexOf(id), position);
72                 modID++;
73         }
74         
75         public double getMotorIgnitionTime(MotorId id) {
76                 return ignitionTimes.get(indexOf(id));
77         }
78         
79         public void setMotorIgnitionTime(MotorId id, double time) {
80                 this.ignitionTimes.set(indexOf(id), time);
81                 modID++;
82         }
83         
84         
85
86         private int indexOf(MotorId id) {
87                 int index = ids.indexOf(id);
88                 if (index < 0) {
89                         throw new IllegalArgumentException("MotorInstanceConfiguration does not " +
90                                         "contain a motor with id " + id);
91                 }
92                 return index;
93         }
94         
95         
96
97         /**
98          * Step all of the motor instances to the specified time minus their ignition time.
99          * @param time  the "global" time
100          */
101         public void step(double time, double acceleration, AtmosphericConditions cond) {
102                 for (int i = 0; i < motors.size(); i++) {
103                         double t = time - ignitionTimes.get(i);
104                         if (t >= 0) {
105                                 motors.get(i).step(t, acceleration, cond);
106                         }
107                 }
108                 modID++;
109         }
110         
111         @Override
112         public int getModID() {
113                 int id = modID;
114                 for (MotorInstance motor : motors) {
115                         id += motor.getModID();
116                 }
117                 return id;
118         }
119         
120         /**
121          * Return a copy of this motor instance configuration with independent motor instances
122          * from this instance.
123          */
124         @Override
125         public MotorInstanceConfiguration clone() {
126                 MotorInstanceConfiguration clone = new MotorInstanceConfiguration();
127                 clone.ids.addAll(this.ids);
128                 clone.mounts.addAll(this.mounts);
129                 clone.positions.addAll(this.positions);
130                 clone.ignitionTimes.addAll(this.ignitionTimes);
131                 for (MotorInstance motor : this.motors) {
132                         clone.motors.add(motor.clone());
133                 }
134                 clone.modID = this.modID;
135                 return clone;
136         }
137         
138 }