create changelog entry
[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(trans.get("FlightEvent.Type.LAUNCH")),
26                 /** 
27                  * Ignition of a motor.  Source is the motor mount the motor of which has ignited,
28                  * and the data is the MotorId of the motor instance.
29                  */
30                 IGNITION(trans.get("FlightEvent.Type.IGNITION")),
31                 /**
32                  * When the motor has lifted off the ground.
33                  */
34                 LIFTOFF(trans.get("FlightEvent.Type.LIFTOFF")),
35                 /**
36                  * Launch rod has been cleared.
37                  */
38                 LAUNCHROD(trans.get("FlightEvent.Type.LAUNCHROD")),
39                 /** 
40                  * Burnout of a motor.  Source is the motor mount the motor of which has burnt out,
41                  * and the data is the MotorId of the motor instance.
42                  */
43                 BURNOUT(trans.get("FlightEvent.Type.BURNOUT")),
44                 /** 
45                  * Ejection charge of a motor fired.  Source is the motor mount the motor of
46                  * which has exploded its ejection charge, and data is the MotorId of the motor instance.
47                  */
48                 EJECTION_CHARGE(trans.get("FlightEvent.Type.EJECTION_CHARGE")),
49                 /** 
50                  * Separation of a stage.  Source is the stage which is being separated from the upper stages.
51                  */
52                 STAGE_SEPARATION(trans.get("FlightEvent.Type.STAGE_SEPARATION")),
53                 /** 
54                  * Apogee has been reached.
55                  */
56                 APOGEE(trans.get("FlightEvent.Type.APOGEE")),
57                 /** 
58                  * Opening of a recovery device.  Source is the RecoveryComponent which has opened. 
59                  */
60                 RECOVERY_DEVICE_DEPLOYMENT(trans.get("FlightEvent.Type.RECOVERY_DEVICE_DEPLOYMENT")),
61                 /** 
62                  * Ground has been hit after flight.
63                  */
64                 GROUND_HIT(trans.get("FlightEvent.Type.GROUND_HIT")),
65                 
66                 /**
67                  * End of simulation.  Placing this to the queue will end the simulation.
68                  */
69                 SIMULATION_END(trans.get("FlightEvent.Type.SIMULATION_END")),
70                 
71                 /**
72                  * A change in altitude has occurred.  Data is a <code>Pair<Double,Double></code>
73                  * which contains the old and new altitudes.
74                  */
75                 ALTITUDE(trans.get("FlightEvent.Type.ALTITUDE"));
76                 
77                 private final String name;
78                 
79                 private Type(String name) {
80                         this.name = name;
81                 }
82                 
83                 @Override
84                 public String toString() {
85                         return name;
86                 }
87         }
88         
89         private final Type type;
90         private final double time;
91         private final RocketComponent source;
92         private final Object data;
93         
94         
95         public FlightEvent(Type type, double time) {
96                 this(type, time, null);
97         }
98         
99         public FlightEvent(Type type, double time, RocketComponent source) {
100                 this(type, time, source, null);
101         }
102         
103         public FlightEvent(Type type, double time, RocketComponent source, Object data) {
104                 this.type = type;
105                 this.time = time;
106                 this.source = source;
107                 this.data = data;
108         }
109         
110         
111         
112         public Type getType() {
113                 return type;
114         }
115         
116         public double getTime() {
117                 return time;
118         }
119         
120         public RocketComponent getSource() {
121                 return source;
122         }
123         
124         public Object getData() {
125                 return data;
126         }
127         
128         
129         /**
130          * Return a new FlightEvent with the same information as the current event
131          * but with <code>null</code> source.  This is used to avoid memory leakage by
132          * retaining references to obsolete components.
133          * 
134          * @return      a new FlightEvent with same type, time and data.
135          */
136         public FlightEvent resetSourceAndData() {
137                 return new FlightEvent(type, time, null, null);
138         }
139         
140         
141         /**
142          * Compares this event to another event depending on the event time.  Secondary
143          * sorting is performed based on the event type ordinal.
144          */
145         @Override
146         public int compareTo(FlightEvent o) {
147                 if (this.time < o.time)
148                         return -1;
149                 if (this.time > o.time)
150                         return 1;
151                 
152                 return this.type.ordinal() - o.type.ordinal();
153         }
154         
155         @Override
156         public String toString() {
157                 return "FlightEvent[type=" + type.name() + ",time=" + time + ",source=" + source + "]";
158         }
159 }