1 package tim.prune.data;
4 * Class to hold an altitude and provide conversion functions
8 private boolean _valid = false;
9 private int _value = 0;
10 private Format _format = Format.NO_FORMAT;
11 private String _stringValue = null;
13 /** Altitude formats */
23 /** Constants for conversion */
24 private static final double CONVERT_METRES_TO_FEET = UnitSetLibrary.UNITS_FEET.getMultFactorFromStd();
25 private static final double CONVERT_FEET_TO_METRES = 1 / CONVERT_METRES_TO_FEET;
27 /** Constant for no altitude value */
28 public static final Altitude NONE = new Altitude(null, Format.NO_FORMAT);
32 * Constructor using String
33 * @param inString string to parse
34 * @param inFormat format of altitude, either metres or feet
36 public Altitude(String inString, Format inFormat)
38 if (inString != null && !inString.equals(""))
42 _stringValue = inString;
43 _value = (int) Double.parseDouble(inString.trim());
47 catch (NumberFormatException nfe) {}
53 * Constructor with int value
54 * @param inValue int value of altitude
55 * @param inFormat format of altitude, either metres or feet
57 public Altitude(int inValue, Format inFormat)
65 * @return an exact copy of this Altitude object
67 public Altitude clone()
69 return new Altitude(_stringValue, _format);
73 * Reset the altitude parameters to the same as the given object
74 * @param inClone clone object to copy
76 public void reset(Altitude inClone)
78 _stringValue = inClone._stringValue;
79 _value = inClone._value;
80 _format = inClone._format;
81 _valid = inClone._valid;
85 * @return true if the value could be parsed
87 public boolean isValid()
94 * @return raw value as int
102 * @param inAltUnit altitude units to use
103 * @return rounded value in specified units
105 public int getValue(Unit inAltUnit)
107 return (int) (getMetricValue() * inAltUnit.getMultFactorFromStd());
111 * @return format of number
113 public Format getFormat()
120 * Get the altitude value in the specified format
121 * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET
122 * @return value as an int
124 public int getValue(Format inFormat)
126 // Note possible rounding errors here if converting to/from units
127 if (inFormat == _format)
129 if (inFormat == Format.METRES)
130 return (int) (_value * CONVERT_FEET_TO_METRES);
131 if (inFormat == Format.FEET)
132 return (int) (_value * CONVERT_METRES_TO_FEET);
137 * @return value of altitude in metres, used for calculations and charts
139 public double getMetricValue()
141 if (_format == Format.FEET) {
142 return _value / UnitSetLibrary.UNITS_FEET.getMultFactorFromStd();
148 * Get a string version of the value
149 * @param inFormat specified format
150 * @return string value, if possible the original one
152 public String getStringValue(Format inFormat)
154 if (!_valid) {return "";}
155 // Return string value if the same format or "no format" was requested
156 if ((inFormat == _format || inFormat == Format.NO_FORMAT)
157 && _stringValue != null && !_stringValue.equals("")) {
160 return "" + getValue(inFormat);
165 * Interpolate a new Altitude object between the given ones
166 * @param inStart start altitude
167 * @param inEnd end altitude
168 * @param inIndex index of interpolated point
169 * @param inNumSteps number of steps to interpolate
170 * @return Interpolated Altitude object
172 public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
174 return interpolate(inStart, inEnd, 1.0 * (inIndex + 1) / (inNumSteps + 1));
179 * Interpolate a new Altitude object between the given ones
180 * @param inStart start altitude
181 * @param inEnd end altitude
182 * @param inFrac fraction of distance from first point
183 * @return Interpolated Altitude object
185 public static Altitude interpolate(Altitude inStart, Altitude inEnd, double inFrac)
187 // Check if altitudes are valid
188 if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid())
189 return Altitude.NONE;
190 // Use altitude format of first point
191 Format altFormat = inStart.getFormat();
192 int startValue = inStart.getValue();
193 int endValue = inEnd.getValue(altFormat);
194 // interpolate between start and end
195 int newValue = startValue + (int) ((endValue - startValue) * inFrac);
196 return new Altitude(newValue, altFormat);
200 * Add the given offset to the current altitude
201 * @param inOffset offset as double
202 * @param inFormat format of offset, feet or metres
203 * @param inDecimals number of decimal places
205 public void addOffset(double inOffset, Format inFormat, int inDecimals)
207 // Use the maximum number of decimal places from current value and offset
208 int numDecimals = NumberUtils.getDecimalPlaces(_stringValue);
209 if (numDecimals < inDecimals) {numDecimals = inDecimals;}
210 // Convert offset to correct units
211 double offset = inOffset;
212 if (inFormat != _format)
214 if (inFormat == Format.FEET)
215 offset = inOffset * CONVERT_FEET_TO_METRES;
217 offset = inOffset * CONVERT_METRES_TO_FEET;
219 // FIXME: The following will fail if _stringValue is null - not sure how it can get in that state!
220 if (_stringValue == null) System.err.println("*** Altitude.addOffset - how did the string value get to be null?");
222 double newValue = Double.parseDouble(_stringValue.trim()) + offset;
223 _value = (int) newValue;
224 _stringValue = NumberUtils.formatNumber(newValue, numDecimals);