File: Speed.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (102 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (6)
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
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;
	}
}