]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/data/DoubleRange.java
3f252eed6cd2bcfe00bcdc272c2f1f82404dcbba
[GpsPrune.git] / src / tim / prune / data / DoubleRange.java
1 package tim.prune.data;
2
3 /**
4  * Represents a range of doubles, holding the maximum and
5  * minimum values.  Values can be positive or negative
6  */
7 public class DoubleRange
8 {
9         private boolean _empty = true;
10         private double _min = 0.0, _max = 0.0;
11
12
13         /** Empty constructor, cleared to zeroes */
14         public DoubleRange() {}
15
16         /**
17          * Constructor giving two initial values
18          * @param inValue1 first value
19          * @param inValue2 second value
20          */
21         public DoubleRange(double inValue1, double inValue2)
22         {
23                 addValue(inValue1);
24                 addValue(inValue2);
25         }
26
27         /**
28          * Clear for a new calculation
29          */
30         public void clear()
31         {
32                 _min = _max = 0.0;
33                 _empty = true;
34         }
35
36
37         /**
38          * Add a value to the range
39          * @param inValue value to add
40          */
41         public void addValue(double inValue)
42         {
43                 if (inValue < _min || _empty) _min = inValue;
44                 if (inValue > _max || _empty) _max = inValue;
45                 _empty = false;
46         }
47
48         /**
49          * Combine this range with another one
50          * @param inOtherRange other range to add to this one
51          */
52         public void combine(DoubleRange inOtherRange)
53         {
54                 if (inOtherRange != null && inOtherRange.getRange() > 1.0)
55                 {
56                         addValue(inOtherRange.getMinimum());
57                         addValue(inOtherRange.getMaximum());
58                 }
59         }
60
61         /**
62          * @return true if data values were found
63          */
64         public boolean hasData()
65         {
66                 return (!_empty);
67         }
68
69
70         /**
71          * @return minimum value, or 0.0 if none found
72          */
73         public double getMinimum()
74         {
75                 return _min;
76         }
77
78
79         /**
80          * @return maximum value, or 0.0 if none found
81          */
82         public double getMaximum()
83         {
84                 return _max;
85         }
86
87         /**
88          * @return range, as maximum - minimum
89          */
90         public double getRange()
91         {
92                 return _max - _min;
93         }
94
95         /**
96          * @return mid value, halfway between min and max
97          */
98         public double getMidValue()
99         {
100                 return (_max + _min) / 2.0;
101         }
102
103         /**
104          * Copy this range into a new object, which can then be modified without changing this one
105          * @return deep copy of this object
106          */
107         public DoubleRange copy()
108         {
109                 if (_empty) return new DoubleRange();
110                 return new DoubleRange(_min, _max);
111         }
112 }