1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
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;
private boolean _inverted = false;
/**
* Constructor
* @param inValue value as string
* @param inUnit unit, such as m/s or km/h
*/
public Speed(String inValue, Unit inUnit) {
this(inValue, inUnit, false);
}
/**
* Constructor
* @param inValue value as string
* @param inUnit unit, such as m/s or km/h
* @param isInverted true if value is inverted (positive downwards)
*/
public Speed(String inValue, Unit inUnit, boolean isInverted)
{
try {
_value = isInverted ? -Double.parseDouble(inValue) : Double.parseDouble(inValue);
_unit = inUnit;
_valid = isValidUnit(inUnit);
_inverted = isInverted;
}
catch (Exception e) {
_valid = false;
}
}
public static Speed createOrNull(String inValue, Unit inUnit) {
return createOrNull(inValue, inUnit, false);
}
public static Speed createOrNull(String inValue, Unit inUnit, boolean isInverted)
{
Speed speed = new Speed(inValue, inUnit, isInverted);
return speed.isValid() ? speed : null;
}
/**
* 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 == UnitSetLibrary.SPEED_UNITS_METRESPERSEC
|| inUnit == UnitSetLibrary.SPEED_UNITS_KMPERHOUR
|| inUnit == UnitSetLibrary.SPEED_UNITS_FEETPERSEC
|| inUnit == UnitSetLibrary.SPEED_UNITS_MILESPERHOUR
|| inUnit == UnitSetLibrary.SPEED_UNITS_KNOTS;
}
/** @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;
}
/** @return true if this field is inverted */
public boolean isInverted() {
return _inverted;
}
/**
* 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;
_inverted = inOther._inverted;
}
}
|