56c65a3a4c53535a328b2870217ad9cb57bb4f33
[debian/openrocket] / core / src / net / sf / openrocket / aerodynamics / Warning.java
1 package net.sf.openrocket.aerodynamics;
2
3 import net.sf.openrocket.l10n.Translator;
4 import net.sf.openrocket.motor.Motor;
5 import net.sf.openrocket.startup.Application;
6 import net.sf.openrocket.unit.UnitGroup;
7
8 public abstract class Warning {
9         
10         private static final Translator trans = Application.getTranslator();
11
12         /**
13          * Return a Warning with the specific text.
14          */
15         public static Warning fromString(String text) {
16                 return new Warning.Other(text);
17         }
18         
19         
20         /**
21          * Return <code>true</code> if the <code>other</code> warning should replace
22          * this warning.  The method should return <code>true</code> if the other
23          * warning indicates a "worse" condition than the current warning.
24          * 
25          * @param other  the warning to compare to
26          * @return       whether this warning should be replaced
27          */
28         public abstract boolean replaceBy(Warning other);
29         
30         
31         /**
32          * Two <code>Warning</code>s are by default considered equal if they are of
33          * the same class.  Therefore only one instance of a particular warning type 
34          * is stored in a {@link WarningSet}.  Subclasses may override this method for
35          * more specific functionality.
36          */
37         @Override
38         public boolean equals(Object o) {
39         return o != null && (o.getClass() == this.getClass());
40     }
41         
42         /**
43          * A <code>hashCode</code> method compatible with the <code>equals</code> method.
44          */
45         @Override
46         public int hashCode() {
47                 return this.getClass().hashCode();
48         }
49         
50         
51         
52         
53         /////////////  Specific warning classes  /////////////
54         
55         
56         /**
57          * A <code>Warning</code> indicating a large angle of attack was encountered.
58          * 
59          * @author Sampo Niskanen <sampo.niskanen@iki.fi>
60          */
61         public static class LargeAOA extends Warning {
62                 private double aoa;
63                 
64                 /**
65                  * Sole constructor.  The argument is the AOA that caused this warning.
66                  * 
67                  * @param aoa  the angle of attack that caused this warning
68                  */
69                 public LargeAOA(double aoa) {
70                         this.aoa = aoa;
71                 }
72                 
73                 @Override
74                 public String toString() {
75                         if (Double.isNaN(aoa))
76                                 //// Large angle of attack encountered.
77                                 return trans.get("Warning.LargeAOA.str1");
78                         //// Large angle of attack encountered (
79                         return (trans.get("Warning.LargeAOA.str2") +
80                                         UnitGroup.UNITS_ANGLE.getDefaultUnit().toString(aoa) + ").");
81                 }
82
83                 @Override
84                 public boolean replaceBy(Warning other) {
85                         if (!(other instanceof LargeAOA))
86                                 return false;
87                         
88                         LargeAOA o = (LargeAOA)other;
89                         if (Double.isNaN(this.aoa))   // If this has value NaN then replace
90                                 return true;
91                         return (o.aoa > this.aoa);
92                 }
93         }
94         
95         public static class MissingMotor extends Warning {
96
97                 private Motor.Type type = null;
98                 private String manufacturer = null;
99                 private String designation = null;
100                 private String digest = null;
101                 private double diameter = Double.NaN;
102                 private double length = Double.NaN;
103                 private double delay = Double.NaN;
104
105                 public String toString() {
106                         String str = "No motor with designation '" + designation + "'";
107                         if (manufacturer != null)
108                                 str += " for manufacturer '" + manufacturer + "'";
109                         str += " found.";
110                         return str;
111                 }
112
113                 public Motor.Type getType() {
114                         return type;
115                 }
116
117
118                 public void setType(Motor.Type type) {
119                         this.type = type;
120                 }
121
122
123                 public String getManufacturer() {
124                         return manufacturer;
125                 }
126
127
128                 public void setManufacturer(String manufacturer) {
129                         this.manufacturer = manufacturer;
130                 }
131
132
133                 public String getDesignation() {
134                         return designation;
135                 }
136
137
138                 public void setDesignation(String designation) {
139                         this.designation = designation;
140                 }
141
142
143                 public String getDigest() {
144                         return digest;
145                 }
146
147
148                 public void setDigest(String digest) {
149                         this.digest = digest;
150                 }
151
152
153                 public double getDiameter() {
154                         return diameter;
155                 }
156
157
158                 public void setDiameter(double diameter) {
159                         this.diameter = diameter;
160                 }
161
162
163                 public double getLength() {
164                         return length;
165                 }
166
167
168                 public void setLength(double length) {
169                         this.length = length;
170                 }
171
172
173                 public double getDelay() {
174                         return delay;
175                 }
176
177
178                 public void setDelay(double delay) {
179                         this.delay = delay;
180                 }
181
182
183                 @Override
184                 public boolean replaceBy(Warning other) {
185                         return false;
186                 }
187
188                 @Override
189                 public int hashCode() {
190                         final int prime = 31;
191                         int result = super.hashCode();
192                         long temp;
193                         temp = Double.doubleToLongBits(delay);
194                         result = prime * result + (int) (temp ^ (temp >>> 32));
195                         result = prime * result
196                                         + ((designation == null) ? 0 : designation.hashCode());
197                         temp = Double.doubleToLongBits(diameter);
198                         result = prime * result + (int) (temp ^ (temp >>> 32));
199                         result = prime * result
200                                         + ((digest == null) ? 0 : digest.hashCode());
201                         temp = Double.doubleToLongBits(length);
202                         result = prime * result + (int) (temp ^ (temp >>> 32));
203                         result = prime * result
204                                         + ((manufacturer == null) ? 0 : manufacturer.hashCode());
205                         result = prime * result + ((type == null) ? 0 : type.hashCode());
206                         return result;
207                 }
208
209                 @Override
210                 public boolean equals(Object obj) {
211                         if (this == obj)
212                                 return true;
213                         if (!super.equals(obj))
214                                 return false;
215                         if (getClass() != obj.getClass())
216                                 return false;
217                         MissingMotor other = (MissingMotor) obj;
218                         if (Double.doubleToLongBits(delay) != Double
219                                         .doubleToLongBits(other.delay))
220                                 return false;
221                         if (designation == null) {
222                                 if (other.designation != null)
223                                         return false;
224                         } else if (!designation.equals(other.designation))
225                                 return false;
226                         if (Double.doubleToLongBits(diameter) != Double
227                                         .doubleToLongBits(other.diameter))
228                                 return false;
229                         if (digest == null) {
230                                 if (other.digest != null)
231                                         return false;
232                         } else if (!digest.equals(other.digest))
233                                 return false;
234                         if (Double.doubleToLongBits(length) != Double
235                                         .doubleToLongBits(other.length))
236                                 return false;
237                         if (manufacturer == null) {
238                                 if (other.manufacturer != null)
239                                         return false;
240                         } else if (!manufacturer.equals(other.manufacturer))
241                                 return false;
242                         if (type != other.type)
243                                 return false;
244                         return true;
245                 }
246                 
247         }
248         
249         
250         /**
251          * An unspecified warning type.  This warning type holds a <code>String</code>
252          * describing it.  Two warnings of this type are considered equal if the strings
253          * are identical.
254          * 
255          * @author Sampo Niskanen <sampo.niskanen@iki.fi>
256          */
257         public static class Other extends Warning {
258                 private String description;
259                 
260                 public Other(String description) {
261                         this.description = description;
262                 }
263                 
264                 @Override
265                 public String toString() {
266                         return description;
267                 }
268                 
269                 @Override
270                 public boolean equals(Object other) {
271                         if (!(other instanceof Other))
272                                 return false;
273                         
274                         Other o = (Other)other;
275                         return (o.description.equals(this.description));
276                 }
277                 
278                 @Override
279                 public int hashCode() {
280                         return description.hashCode();
281                 }
282
283                 @Override
284                 public boolean replaceBy(Warning other) {
285                         return false;
286                 }
287         }
288         
289         
290         /** A <code>Warning</code> that the body diameter is discontinuous. */
291 ////Discontinuity in rocket body diameter.
292         public static final Warning DISCONTINUITY = 
293                 new Other(trans.get("Warning.DISCONTINUITY"));
294         
295         /** A <code>Warning</code> that the fins are thick compared to the rocket body. */
296 ////Thick fins may not be modeled accurately.
297         public static final Warning THICK_FIN =
298                 new Other(trans.get("Warning.THICK_FIN"));
299         
300         /** A <code>Warning</code> that the fins have jagged edges. */
301 ////Jagged-edged fin predictions may be inaccurate.
302         public static final Warning JAGGED_EDGED_FIN =
303                 new Other(trans.get("Warning.JAGGED_EDGED_FIN"));
304         
305         /** A <code>Warning</code> that simulation listeners have affected the simulation */
306 ////Listeners modified the flight simulation
307         public static final Warning LISTENERS_AFFECTED =
308                 new Other(trans.get("Warning.LISTENERS_AFFECTED"));
309         
310 ////Recovery device opened while motor still burning.
311         public static final Warning RECOVERY_DEPLOYMENT_WHILE_BURNING =
312                 new Other(trans.get("Warning.RECOVERY_DEPLOYMENT_WHILE_BURNING"));
313         
314         
315         //// Invalid parameter encountered, ignoring.
316         public static final Warning FILE_INVALID_PARAMETER =
317                 new Other(trans.get("Warning.FILE_INVALID_PARAMETER"));
318 }