7981d37a6f419cee6176d6e4d2b0630a76599c91
[debian/openrocket] / src / net / sf / openrocket / aerodynamics / Warning.java
1 package net.sf.openrocket.aerodynamics;
2
3 import net.sf.openrocket.unit.UnitGroup;
4
5 public abstract class Warning {
6         
7         
8         /**
9          * Return a Warning with the specific text.
10          */
11         public static Warning fromString(String text) {
12                 return new Warning.Other(text);
13         }
14         
15         
16         /**
17          * Return <code>true</code> if the <code>other</code> warning should replace
18          * this warning.  The method should return <code>true</code> if the other
19          * warning indicates a "worse" condition than the current warning.
20          * 
21          * @param other  the warning to compare to
22          * @return       whether this warning should be replaced
23          */
24         public abstract boolean replaceBy(Warning other);
25         
26         
27         /**
28          * Two <code>Warning</code>s are by default considered equal if they are of
29          * the same class.  Therefore only one instance of a particular warning type 
30          * is stored in a {@link WarningSet}.  Subclasses may override this method for
31          * more specific functionality.
32          */
33         @Override
34         public boolean equals(Object o) {
35                 return (o.getClass() == this.getClass());
36         }
37         
38         /**
39          * A <code>hashCode</code> method compatible with the <code>equals</code> method.
40          */
41         @Override
42         public int hashCode() {
43                 return this.getClass().hashCode();
44         }
45         
46         
47         
48         
49         /////////////  Specific warning classes  /////////////
50         
51         
52         /**
53          * A <code>Warning</code> indicating a large angle of attack was encountered.
54          * 
55          * @author Sampo Niskanen <sampo.niskanen@iki.fi>
56          */
57         public static class LargeAOA extends Warning {
58                 private double aoa;
59                 
60                 /**
61                  * Sole constructor.  The argument is the AOA that caused this warning.
62                  * 
63                  * @param aoa  the angle of attack that caused this warning
64                  */
65                 public LargeAOA(double aoa) {
66                         this.aoa = aoa;
67                 }
68                 
69                 @Override
70                 public String toString() {
71                         if (Double.isNaN(aoa))
72                                 return "Large angle of attack encountered.";
73                         return ("Large angle of attack encountered (" +
74                                         UnitGroup.UNITS_ANGLE.getDefaultUnit().toString(aoa) + ").");
75                 }
76
77                 @Override
78                 public boolean replaceBy(Warning other) {
79                         if (!(other instanceof LargeAOA))
80                                 return false;
81                         
82                         LargeAOA o = (LargeAOA)other;
83                         if (Double.isNaN(this.aoa))   // If this has value NaN then replace
84                                 return true;
85                         return (o.aoa > this.aoa);
86                 }
87         }
88         
89         
90         
91         /**
92          * An unspecified warning type.  This warning type holds a <code>String</code>
93          * describing it.  Two warnings of this type are considered equal if the strings
94          * are identical.
95          * 
96          * @author Sampo Niskanen <sampo.niskanen@iki.fi>
97          */
98         public static class Other extends Warning {
99                 private String description;
100                 
101                 public Other(String description) {
102                         this.description = description;
103                 }
104                 
105                 @Override
106                 public String toString() {
107                         return description;
108                 }
109                 
110                 @Override
111                 public boolean equals(Object other) {
112                         if (!(other instanceof Other))
113                                 return false;
114                         
115                         Other o = (Other)other;
116                         return (o.description.equals(this.description));
117                 }
118                 
119                 @Override
120                 public int hashCode() {
121                         return description.hashCode();
122                 }
123
124                 @Override
125                 public boolean replaceBy(Warning other) {
126                         return false;
127                 }
128         }
129         
130         
131         /** A <code>Warning</code> that the body diameter is discontinuous. */
132         public static final Warning DISCONTINUITY = 
133                 new Other("Discontinuity in rocket body diameter.");
134         
135         /** A <code>Warning</code> that the fins are thick compared to the rocket body. */
136         public static final Warning THICK_FIN =
137                 new Other("Thick fins may not be modeled accurately.");
138         
139         /** A <code>Warning</code> that the fins have jagged edges. */
140         public static final Warning JAGGED_EDGED_FIN =
141                 new Other("Jagged-edged fin predictions may be inaccurate.");
142         
143         /** A <code>Warning</code> that simulation listeners have affected the simulation */
144         public static final Warning LISTENERS_AFFECTED =
145                 new Other("Listeners modified the flight simulation");
146         
147         public static final Warning RECOVERY_DEPLOYMENT_WHILE_BURNING =
148                 new Other("Recovery device opened while motor still burning.");
149         
150         
151         
152         public static final Warning FILE_INVALID_PARAMETER =
153                 new Other("Invalid parameter encountered, ignoring.");
154 }