]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/DoubleRange.java
4e361224f420ed32b72473edc1e5adf87d37a3f0
[GpsPrune.git] / 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         /**
50          * @return true if data values were found
51          */
52         public boolean hasData()
53         {
54                 return (!_empty);
55         }
56
57
58         /**
59          * @return minimum value, or 0.0 if none found
60          */
61         public double getMinimum()
62         {
63                 return _min;
64         }
65
66
67         /**
68          * @return maximum value, or 0.0 if none found
69          */
70         public double getMaximum()
71         {
72                 return _max;
73         }
74
75         /**
76          * @return range, as maximum - minimum
77          */
78         public double getRange()
79         {
80                 return _max - _min;
81         }
82
83         /**
84          * @return mid value, halfway between min and max
85          */
86         public double getMidValue()
87         {
88                 return (_max + _min) / 2.0;
89         }
90
91         /**
92          * Copy this range into a new object, which can then be modified without changing this one
93          * @return deep copy of this object
94          */
95         public DoubleRange copy()
96         {
97                 if (_empty) return new DoubleRange();
98                 return new DoubleRange(_min, _max);
99         }
100 }