]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Speed.java
Version 19.2, December 2018
[GpsPrune.git] / tim / prune / data / Speed.java
1 package tim.prune.data;
2
3 /**
4  * Class to hold either a horizontal speed or a vertical speed
5  * including the units
6  */
7 public class Speed
8 {
9         private double _value = 0.0;
10         private Unit   _unit  = null;
11         private boolean _valid = false;
12
13         /**
14          * Constructor
15          * @param inValue value
16          * @param inUnit unit, such as m/s or km/h
17          */
18         public Speed(double inValue, Unit inUnit)
19         {
20                 _value = inValue;
21                 _unit  = inUnit;
22                 _valid = isValidUnit(inUnit);
23         }
24
25         /**
26          * Constructor
27          * @param inValue value as string
28          * @param inUnit unit, such as m/s or km/h
29          */
30         public Speed(String inValue, Unit inUnit)
31         {
32                 try {
33                         _value = Double.parseDouble(inValue);
34                         _unit  = inUnit;
35                         _valid = isValidUnit(inUnit);
36                 }
37                 catch (Exception e)
38                 {
39                         _valid = false;
40                 }
41         }
42
43         /**
44          * Check if the given unit is valid for a speed
45          * @param inUnit unit
46          * @return true if it's valid
47          */
48         private static boolean isValidUnit(Unit inUnit)
49         {
50                 return inUnit != null && (inUnit == UnitSetLibrary.SPEED_UNITS_METRESPERSEC
51                                 || inUnit == UnitSetLibrary.SPEED_UNITS_KMPERHOUR
52                                 || inUnit == UnitSetLibrary.SPEED_UNITS_FEETPERSEC
53                                 || inUnit == UnitSetLibrary.SPEED_UNITS_MILESPERHOUR
54                                 || inUnit == UnitSetLibrary.SPEED_UNITS_KNOTS);
55         }
56
57         /**
58          * Invert the speed value, for example when vertical speeds are positive downwards
59          */
60         public void invert() {
61                 if (_valid) _value = -_value;
62         }
63
64         /** @return the numerical value in whatever units they're in */
65         public double getValue() {return _value;}
66
67         /** @return the units they're in */
68         public Unit getUnit() {return _unit;}
69
70         /**
71          * @return speed value in metres per second
72          */
73         public double getValueInMetresPerSec()
74         {
75                 if (!_valid) return 0.0;
76                 return _value / _unit.getMultFactorFromStd();
77         }
78
79         /**
80          * @param inUnit specified speed units
81          * @return speed value in the specified units
82          */
83         public double getValue(Unit inUnit)
84         {
85                 if (!_valid || !isValidUnit(inUnit)) return 0.0;
86                 return getValueInMetresPerSec() * inUnit.getMultFactorFromStd();
87         }
88
89         /** @return true if this is valid */
90         public boolean isValid() {return _valid;}
91
92         /**
93          * Copy the values from the other object into this one, to make this one a clone
94          * @param inOther other speed object
95          */
96         public void copyFrom(Speed inOther)
97         {
98                 _value = inOther._value;
99                 _unit  = inOther._unit;
100                 _valid = inOther._valid;
101         }
102 }