1 package tim.prune.data;
4 * Class to hold either a horizontal speed or a vertical speed
9 private double _value = 0.0;
10 private Unit _unit = null;
11 private boolean _valid = false;
15 * @param inValue value
16 * @param inUnit unit, such as m/s or km/h
18 public Speed(double inValue, Unit inUnit)
22 _valid = isValidUnit(inUnit);
27 * @param inValue value as string
28 * @param inUnit unit, such as m/s or km/h
30 public Speed(String inValue, Unit inUnit)
33 _value = Double.parseDouble(inValue);
35 _valid = isValidUnit(inUnit);
44 * Check if the given unit is valid for a speed
46 * @return true if it's valid
48 private static boolean isValidUnit(Unit inUnit)
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);
58 * Invert the speed value, for example when vertical speeds are positive downwards
60 public void invert() {
61 if (_valid) _value = -_value;
64 /** @return the numerical value in whatever units they're in */
65 public double getValue() {return _value;}
67 /** @return the units they're in */
68 public Unit getUnit() {return _unit;}
71 * @return speed value in metres per second
73 public double getValueInMetresPerSec()
75 if (!_valid) return 0.0;
76 return _value / _unit.getMultFactorFromStd();
80 * @param inUnit specified speed units
81 * @return speed value in the specified units
83 public double getValue(Unit inUnit)
85 if (!_valid || !isValidUnit(inUnit)) return 0.0;
86 return getValueInMetresPerSec() * inUnit.getMultFactorFromStd();
89 /** @return true if this is valid */
90 public boolean isValid() {return _valid;}
93 * Copy the values from the other object into this one, to make this one a clone
94 * @param inOther other speed object
96 public void copyFrom(Speed inOther)
98 _value = inOther._value;
99 _unit = inOther._unit;
100 _valid = inOther._valid;