1 package tim.prune.data;
4 * Represents a range of altitudes, taking units into account.
5 * Values assumed to be >= 0.
7 public class AltitudeRange
9 /** Range of altitudes in metres */
10 private IntegerRange _range = new IntegerRange();
12 private boolean _empty;
13 /** Previous metric value */
14 private int _prevValue;
15 /** Total climb in metres */
16 private double _climb;
17 /** Total descent in metres */
18 private double _descent;
24 public AltitudeRange() {
29 * Clear the altitude range
42 * Add a value to the range
43 * @param inAltitude value to add, only positive values considered
45 public void addValue(Altitude inAltitude)
47 if (inAltitude != null && inAltitude.isValid())
49 int altValue = (int) inAltitude.getMetricValue();
50 _range.addValue(altValue);
51 // Compare with previous value if any
54 if (altValue > _prevValue)
55 _climb += (altValue - _prevValue);
57 _descent += (_prevValue - altValue);
59 _prevValue = altValue;
65 * Reset the climb/descent calculations starting from the given value
66 * @param inAltitude altitude value
68 public void ignoreValue(Altitude inAltitude)
70 // If we set the empty flag to true, that has the same effect as restarting a segment
76 * @return true if altitude range found
78 public boolean hasRange()
80 return _range.getMaximum() > _range.getMinimum();
85 * @param inUnit altitude units to use
86 * @return minimum value, or -1 if none found
88 public int getMinimum(Unit inUnit)
90 if (_range.getMinimum() <= 0) return _range.getMinimum();
91 return (int) (_range.getMinimum() * inUnit.getMultFactorFromStd());
95 * @param inUnit altitude units to use
96 * @return maximum value, or -1 if none found
98 public int getMaximum(Unit inUnit)
100 if (_range.getMaximum() <= 0) return _range.getMaximum();
101 return (int) (_range.getMaximum() * inUnit.getMultFactorFromStd());
105 * @param inUnit altitude units to use
106 * @return total climb
108 public int getClimb(Unit inUnit)
110 return (int) (_climb * inUnit.getMultFactorFromStd());
114 * @param inUnit altitude units to use
115 * @return total descent
117 public int getDescent(Unit inUnit)
119 return (int) (_descent * inUnit.getMultFactorFromStd());
123 * @return overall height gain in metres
125 public double getMetricHeightDiff()
127 return _climb - _descent;