X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fdata%2FSpeed.java;fp=src%2Ftim%2Fprune%2Fdata%2FSpeed.java;h=80cb7dffc0b95a1f4e126ffae2b82605880cd4f2;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/data/Speed.java b/src/tim/prune/data/Speed.java new file mode 100644 index 0000000..80cb7df --- /dev/null +++ b/src/tim/prune/data/Speed.java @@ -0,0 +1,102 @@ +package tim.prune.data; + +/** + * Class to hold either a horizontal speed or a vertical speed + * including the units + */ +public class Speed +{ + private double _value = 0.0; + private Unit _unit = null; + private boolean _valid = false; + + /** + * Constructor + * @param inValue value + * @param inUnit unit, such as m/s or km/h + */ + public Speed(double inValue, Unit inUnit) + { + _value = inValue; + _unit = inUnit; + _valid = isValidUnit(inUnit); + } + + /** + * Constructor + * @param inValue value as string + * @param inUnit unit, such as m/s or km/h + */ + public Speed(String inValue, Unit inUnit) + { + try { + _value = Double.parseDouble(inValue); + _unit = inUnit; + _valid = isValidUnit(inUnit); + } + catch (Exception e) + { + _valid = false; + } + } + + /** + * Check if the given unit is valid for a speed + * @param inUnit unit + * @return true if it's valid + */ + private static boolean isValidUnit(Unit inUnit) + { + return inUnit != null && (inUnit == UnitSetLibrary.SPEED_UNITS_METRESPERSEC + || inUnit == UnitSetLibrary.SPEED_UNITS_KMPERHOUR + || inUnit == UnitSetLibrary.SPEED_UNITS_FEETPERSEC + || inUnit == UnitSetLibrary.SPEED_UNITS_MILESPERHOUR + || inUnit == UnitSetLibrary.SPEED_UNITS_KNOTS); + } + + /** + * Invert the speed value, for example when vertical speeds are positive downwards + */ + public void invert() { + if (_valid) _value = -_value; + } + + /** @return the numerical value in whatever units they're in */ + public double getValue() {return _value;} + + /** @return the units they're in */ + public Unit getUnit() {return _unit;} + + /** + * @return speed value in metres per second + */ + public double getValueInMetresPerSec() + { + if (!_valid) return 0.0; + return _value / _unit.getMultFactorFromStd(); + } + + /** + * @param inUnit specified speed units + * @return speed value in the specified units + */ + public double getValue(Unit inUnit) + { + if (!_valid || !isValidUnit(inUnit)) return 0.0; + return getValueInMetresPerSec() * inUnit.getMultFactorFromStd(); + } + + /** @return true if this is valid */ + public boolean isValid() {return _valid;} + + /** + * Copy the values from the other object into this one, to make this one a clone + * @param inOther other speed object + */ + public void copyFrom(Speed inOther) + { + _value = inOther._value; + _unit = inOther._unit; + _valid = inOther._valid; + } +}