]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/UnitSetLibrary.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / data / UnitSetLibrary.java
1 package tim.prune.data;
2
3 /**
4  * List of all possible unit sets, for example
5  * metric, imperial, nautical
6  */
7 public abstract class UnitSetLibrary
8 {
9         // Distance units - all conversion factors are from metres
10         /** Units for feet (used for loading and converting values) */
11         public static final Unit UNITS_FEET       = new Unit("feet", 3.2808);
12         /** Units for metres */
13         public static final Unit UNITS_METRES     = new Unit("metres");
14         /** Units for km */
15         public static final Unit UNITS_KILOMETRES = new Unit("kilometres", 1/1000.0);
16         /** Units for miles */
17         public static final Unit UNITS_MILES      = new Unit("miles", 1/1609.3);
18         /** Units for nautical miles */
19         public static final Unit UNITS_NAUTICAL_MILES = new Unit("nauticalmiles", 1/1852.0);
20
21         /** Array of available unit sets */
22         private static UnitSet[] _sets = {
23                 new UnitSet("unitset.kilometres", UNITS_KILOMETRES, UNITS_METRES, Altitude.Format.METRES),
24                 new UnitSet("unitset.miles", UNITS_MILES, UNITS_FEET, Altitude.Format.FEET),
25                 new UnitSet("unitset.nautical", UNITS_NAUTICAL_MILES, UNITS_FEET, Altitude.Format.FEET)
26         };
27
28         /**
29          * @return number of available unit sets
30          */
31         public static int getNumUnitSets() {
32                 return _sets.length;
33         }
34
35         /**
36          * Get the specified unit set
37          * @param inIndex index of set starting from 0
38          * @return specified unit set or the default one if index out of range
39          */
40         public static UnitSet getUnitSet(int inIndex)
41         {
42                 if (inIndex >= 0 && inIndex < getNumUnitSets()) {
43                         return _sets[inIndex];
44                 }
45                 return _sets[0];
46         }
47
48         /**
49          * Get the unit set specified by the given key
50          * @param inKey key to look for
51          * @return unit set with given key, or default set if key not found
52          */
53         public static UnitSet getUnitSet(String inKey)
54         {
55                 // Loop over all available unit sets
56                 for (int i=0; i<getNumUnitSets(); i++)
57                 {
58                         UnitSet set = getUnitSet(i);
59                         if (set.getNameKey().equals(inKey)) {
60                                 return set;
61                         }
62                 }
63                 // Not found in list, so just return the first one
64                 return getUnitSet(0);
65         }
66 }