ead84eecf0263929264a25235694fa93f4c69f79
[debian/openrocket] / core / src / net / sf / openrocket / simulation / FlightEvent.java
1 package net.sf.openrocket.simulation;
2
3 import net.sf.openrocket.l10n.Translator;
4 import net.sf.openrocket.rocketcomponent.RocketComponent;
5 import net.sf.openrocket.startup.Application;
6
7
8 /**
9  * A class that defines an event during the flight of a rocket.
10  * 
11  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12  */
13 public class FlightEvent implements Comparable<FlightEvent> {
14         private static final Translator trans = Application.getTranslator();
15
16         /**
17          * The type of the flight event.
18          * 
19          * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20          */
21         public enum Type {
22                 /** 
23                  * Rocket launch.
24                  */
25                 //// Launch
26                 LAUNCH(trans.get("FlightEvent.Type.LAUNCH")),
27                 /** 
28                  * Ignition of a motor.  Source is the motor mount the motor of which has ignited,
29                  * and the data is the MotorId of the motor instance.
30                  */
31                 //// Motor ignition
32                 IGNITION(trans.get("FlightEvent.Type.IGNITION")),
33                 /**
34                  * When the motor has lifted off the ground.
35                  */
36                 //// Lift-off
37                 LIFTOFF(trans.get("FlightEvent.Type.LIFTOFF")),
38                 /**
39                  * Launch rod has been cleared.
40                  */
41                 //// Launch rod clearance
42                 LAUNCHROD(trans.get("FlightEvent.Type.LAUNCHROD")),
43                 /** 
44                  * Burnout of a motor.  Source is the motor mount the motor of which has burnt out,
45                  * and the data is the MotorId of the motor instance.
46                  */
47                 //// Motor burnout
48                 BURNOUT(trans.get("FlightEvent.Type.BURNOUT")),
49                 /** 
50                  * Ejection charge of a motor fired.  Source is the motor mount the motor of
51                  * which has exploded its ejection charge, and data is the MotorId of the motor instance.
52                  */
53                 //// Ejection charge
54                 EJECTION_CHARGE(trans.get("FlightEvent.Type.EJECTION_CHARGE")),
55                 /** 
56                  * Separation of a stage.  Source is the stage which has separated all lower stages. 
57                  */
58                 //// Stage separation
59                 STAGE_SEPARATION(trans.get("FlightEvent.Type.STAGE_SEPARATION")),
60                 /** 
61                  * Apogee has been reached.
62                  */
63                 //// Apogee
64                 APOGEE(trans.get("FlightEvent.Type.APOGEE")),
65                 /** 
66                  * Opening of a recovery device.  Source is the RecoveryComponent which has opened. 
67                  */
68                 //// Recovery device deployment
69                 RECOVERY_DEVICE_DEPLOYMENT(trans.get("FlightEvent.Type.RECOVERY_DEVICE_DEPLOYMENT")),
70                 /** 
71                  * Ground has been hit after flight.
72                  */
73                 //// Ground hit
74                 GROUND_HIT(trans.get("FlightEvent.Type.GROUND_HIT")),
75
76                 /**
77                  * End of simulation.  Placing this to the queue will end the simulation.
78                  */
79                 //// Simulation end
80                 SIMULATION_END(trans.get("FlightEvent.Type.SIMULATION_END")),
81
82                 /**
83                  * A change in altitude has occurred.  Data is a <code>Pair<Double,Double></code>
84                  * which contains the old and new altitudes.
85                  */
86                 //// Altitude change
87                 ALTITUDE(trans.get("FlightEvent.Type.ALTITUDE"));
88                 
89                 private final String name;
90                 
91                 private Type(String name) {
92                         this.name = name;
93                 }
94                 
95                 @Override
96                 public String toString() {
97                         return name;
98                 }
99         }
100         
101         private final Type type;
102         private final double time;
103         private final RocketComponent source;
104         private final Object data;
105         
106         
107         public FlightEvent(Type type, double time) {
108                 this(type, time, null);
109         }
110         
111         public FlightEvent(Type type, double time, RocketComponent source) {
112                 this(type, time, source, null);
113         }
114         
115         public FlightEvent(Type type, double time, RocketComponent source, Object data) {
116                 this.type = type;
117                 this.time = time;
118                 this.source = source;
119                 this.data = data;
120         }
121         
122         
123
124         public Type getType() {
125                 return type;
126         }
127         
128         public double getTime() {
129                 return time;
130         }
131         
132         public RocketComponent getSource() {
133                 return source;
134         }
135         
136         public Object getData() {
137                 return data;
138         }
139         
140         
141         /**
142          * Return a new FlightEvent with the same information as the current event
143          * but with <code>null</code> source.  This is used to avoid memory leakage by
144          * retaining references to obsolete components.
145          * 
146          * @return      a new FlightEvent with same type, time and data.
147          */
148         public FlightEvent resetSourceAndData() {
149                 return new FlightEvent(type, time, null, null);
150         }
151         
152         
153         /**
154          * Compares this event to another event depending on the event time.  Secondary
155          * sorting is performed based on the event type ordinal.
156          */
157         @Override
158         public int compareTo(FlightEvent o) {
159                 if (this.time < o.time)
160                         return -1;
161                 if (this.time > o.time)
162                         return 1;
163                 
164                 return this.type.ordinal() - o.type.ordinal();
165         }
166         
167         @Override
168         public String toString() {
169                 return "FlightEvent[type=" + type.name() + ",time=" + time + ",source=" + source + "]";
170         }
171 }