]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/AltitudeRange.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / data / AltitudeRange.java
1 package tim.prune.data;
2
3 /**
4  * Represents a range of altitudes, taking units into account.
5  * Values assumed to be >= 0.
6  */
7 public class AltitudeRange
8 {
9         /** Range of altitudes in metres */
10         private IntegerRange _range = new IntegerRange();
11         /** Empty flag */
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;
19
20
21         /**
22          * Constructor
23          */
24         public AltitudeRange() {
25                 clear();
26         }
27
28         /**
29          * Clear the altitude range
30          */
31         public void clear()
32         {
33                 _range.clear();
34                 _climb = 0.0;
35                 _descent = 0.0;
36                 _empty = true;
37                 _prevValue = 0;
38         }
39
40
41         /**
42          * Add a value to the range
43          * @param inAltitude value to add, only positive values considered
44          */
45         public void addValue(Altitude inAltitude)
46         {
47                 if (inAltitude != null && inAltitude.isValid())
48                 {
49                         int altValue = (int) inAltitude.getMetricValue();
50                         _range.addValue(altValue);
51                         // Compare with previous value if any
52                         if (!_empty)
53                         {
54                                 if (altValue > _prevValue)
55                                         _climb += (altValue - _prevValue);
56                                 else
57                                         _descent += (_prevValue - altValue);
58                         }
59                         _prevValue = altValue;
60                         _empty = false;
61                 }
62         }
63
64         /**
65          * Reset the climb/descent calculations starting from the given value
66          * @param inAltitude altitude value
67          */
68         public void ignoreValue(Altitude inAltitude)
69         {
70                 // If we set the empty flag to true, that has the same effect as restarting a segment
71                 _empty = true;
72                 addValue(inAltitude);
73         }
74
75         /**
76          * @return true if altitude range found
77          */
78         public boolean hasRange()
79         {
80                 return _range.getMaximum() > _range.getMinimum();
81         }
82
83
84         /**
85          * @param inUnit altitude units to use
86          * @return minimum value, or -1 if none found
87          */
88         public int getMinimum(Unit inUnit)
89         {
90                 if (_range.getMinimum() <= 0) return _range.getMinimum();
91                 return (int) (_range.getMinimum() * inUnit.getMultFactorFromStd());
92         }
93
94         /**
95          * @param inUnit altitude units to use
96          * @return maximum value, or -1 if none found
97          */
98         public int getMaximum(Unit inUnit)
99         {
100                 if (_range.getMaximum() <= 0) return _range.getMaximum();
101                 return (int) (_range.getMaximum() * inUnit.getMultFactorFromStd());
102         }
103
104         /**
105          * @param inUnit altitude units to use
106          * @return total climb
107          */
108         public int getClimb(Unit inUnit)
109         {
110                 return (int) (_climb * inUnit.getMultFactorFromStd());
111         }
112
113         /**
114          * @param inUnit altitude units to use
115          * @return total descent
116          */
117         public int getDescent(Unit inUnit)
118         {
119                 return (int) (_descent * inUnit.getMultFactorFromStd());
120         }
121 }