]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/UnitSetLibrary.java
37f3cfc5f4a8f54f94eef9311ee97cf925390a4e
[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         // Speed units - all conversion factors from metres per second
22         public static final Unit SPEED_UNITS_METRESPERSEC = new Unit(UNITS_METRES, "persec");
23         public static final Unit SPEED_UNITS_FEETPERSEC   = new Unit(UNITS_FEET, "persec");
24         public static final Unit SPEED_UNITS_MILESPERHOUR = new Unit(UNITS_MILES, "perhour", 60.0 * 60.0);
25         public static final Unit SPEED_UNITS_KNOTS        = new Unit(UNITS_NAUTICAL_MILES, "perhour", 60.0 * 60.0);
26         public static final Unit SPEED_UNITS_KMPERHOUR    = new Unit(UNITS_KILOMETRES, "perhour", 60.0 * 60.0);
27         public static final Unit[] ALL_SPEED_UNITS = {SPEED_UNITS_METRESPERSEC, SPEED_UNITS_KMPERHOUR,
28                 SPEED_UNITS_FEETPERSEC, SPEED_UNITS_MILESPERHOUR};
29
30         /** Array of available unit sets */
31         private static UnitSet[] _sets =
32         {
33                 new UnitSet("unitset.kilometres", UNITS_KILOMETRES, UNITS_METRES, SPEED_UNITS_KMPERHOUR, SPEED_UNITS_METRESPERSEC),
34                 new UnitSet("unitset.miles", UNITS_MILES, UNITS_FEET, SPEED_UNITS_MILESPERHOUR, SPEED_UNITS_FEETPERSEC),
35                 new UnitSet("unitset.nautical", UNITS_NAUTICAL_MILES, UNITS_FEET, SPEED_UNITS_KNOTS, SPEED_UNITS_FEETPERSEC)
36         };
37
38         /**
39          * @return number of available unit sets
40          */
41         public static int getNumUnitSets() {
42                 return _sets.length;
43         }
44
45         /**
46          * Get the specified unit set
47          * @param inIndex index of set starting from 0
48          * @return specified unit set or the default one if index out of range
49          */
50         public static UnitSet getUnitSet(int inIndex)
51         {
52                 if (inIndex >= 0 && inIndex < getNumUnitSets()) {
53                         return _sets[inIndex];
54                 }
55                 return _sets[0];
56         }
57
58         /**
59          * Get the unit set specified by the given key
60          * @param inKey key to look for
61          * @return unit set with given key, or default set if key not found
62          */
63         public static UnitSet getUnitSet(String inKey)
64         {
65                 // Loop over all available unit sets
66                 for (UnitSet set : _sets)
67                 {
68                         if (set.getNameKey().equals(inKey)) {
69                                 return set;
70                         }
71                 }
72                 // Not found in list, so just return the first one
73                 return getUnitSet(0);
74         }
75 }