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_FEET_TO_METRES = 0.3048;
25 private static final double CONVERT_METRES_TO_FEET = 3.28084;
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 _value = inClone._value;
79 _format = inClone._format;
80 _valid = inClone._valid;
84 * @return true if the value could be parsed
86 public boolean isValid()
93 * @return raw value as int
102 * @return format of number
104 public Format getFormat()
111 * Get the altitude value in the specified format
112 * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET
113 * @return value as an int
115 public int getValue(Format inFormat)
117 // Note possible rounding errors here if converting to/from units
118 if (inFormat == _format)
120 if (inFormat == Format.METRES)
121 return (int) (_value * CONVERT_FEET_TO_METRES);
122 if (inFormat == Format.FEET)
123 return (int) (_value * CONVERT_METRES_TO_FEET);
128 * Get a string version of the value
129 * @param inFormat specified format
130 * @return string value, if possible the original one
132 public String getStringValue(Format inFormat)
134 if (!_valid) {return "";}
135 // Return string value if the same format or "no format" was requested
136 if ((inFormat == _format || inFormat == Format.NO_FORMAT)
137 && _stringValue != null && !_stringValue.equals("")) {
140 return "" + getValue(inFormat);
145 * Interpolate a new Altitude object between the given ones
146 * @param inStart start altitude
147 * @param inEnd end altitude
148 * @param inIndex index of interpolated point
149 * @param inNumSteps number of steps to interpolate
150 * @return Interpolated Altitude object
152 public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
154 return interpolate(inStart, inEnd, 1.0 * (inIndex + 1) / (inNumSteps + 1));
159 * Interpolate a new Altitude object between the given ones
160 * @param inStart start altitude
161 * @param inEnd end altitude
162 * @param inFrac fraction of distance from first point
163 * @return Interpolated Altitude object
165 public static Altitude interpolate(Altitude inStart, Altitude inEnd, double inFrac)
167 // Check if altitudes are valid
168 if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid())
169 return Altitude.NONE;
170 // Use altitude format of first point
171 Format altFormat = inStart.getFormat();
172 int startValue = inStart.getValue();
173 int endValue = inEnd.getValue(altFormat);
174 // interpolate between start and end
175 int newValue = startValue + (int) ((endValue - startValue) * inFrac);
176 return new Altitude(newValue, altFormat);
180 * Add the given offset to the current altitude
181 * @param inOffset offset as double
182 * @param inFormat format of offset, feet or metres
183 * @param inDecimals number of decimal places
185 public void addOffset(double inOffset, Format inFormat, int inDecimals)
187 // Use the maximum number of decimal places from current value and offset
188 int numDecimals = NumberUtils.getDecimalPlaces(_stringValue);
189 if (numDecimals < inDecimals) {numDecimals = inDecimals;}
190 // Convert offset to correct units
191 double offset = inOffset;
192 if (inFormat != _format)
194 if (inFormat == Format.FEET)
195 offset = inOffset * CONVERT_FEET_TO_METRES;
197 offset = inOffset * CONVERT_METRES_TO_FEET;
200 double newValue = Double.parseDouble(_stringValue.trim()) + offset;
201 _value = (int) newValue;
202 _stringValue = NumberUtils.formatNumber(newValue, numDecimals);