View Javadoc
1   /*
2    * Copyright (C) 2018 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.units;
17  
18  /**
19   * Enumerator containing recognized typical speed units.
20   */
21  public enum SpeedUnit {
22      /**
23       * Meters per second (m/s).
24       */
25      METERS_PER_SECOND,
26  
27      /**
28       * Kilometers per hour (Km/h)
29       */
30      KILOMETERS_PER_HOUR,
31  
32      /**
33       * Kilometers per second (Km/s)
34       */
35      KILOMETERS_PER_SECOND,
36  
37      /**
38       * Feet per second (ft/s)
39       */
40      FEET_PER_SECOND,
41  
42      /**
43       * Miles per hour (mph)
44       */
45      MILES_PER_HOUR;
46  
47      /**
48       * Returns unit system for provided speed unit.
49       *
50       * @param unit speed unit to be checked.
51       * @return unit system (metric or imperial).
52       * @throws IllegalArgumentException if unit is null or not supported.
53       */
54      public static UnitSystem getUnitSystem(final SpeedUnit unit) {
55          if (unit == null) {
56              throw new IllegalArgumentException();
57          }
58  
59          return switch (unit) {
60              case FEET_PER_SECOND, MILES_PER_HOUR -> UnitSystem.IMPERIAL;
61              default -> UnitSystem.METRIC;
62          };
63      }
64  
65      /**
66       * Gets all supported metric speed units.
67       *
68       * @return all supported metric speed units.
69       */
70      public static SpeedUnit[] getMetricUnits() {
71          return new SpeedUnit[]{
72                  METERS_PER_SECOND,
73                  KILOMETERS_PER_HOUR,
74                  KILOMETERS_PER_SECOND
75          };
76      }
77  
78      /**
79       * Gets all supported imperial speed units.
80       *
81       * @return all supported imperial speed units.
82       */
83      public static SpeedUnit[] getImperialUnits() {
84          return new SpeedUnit[]{
85                  FEET_PER_SECOND,
86                  MILES_PER_HOUR
87          };
88      }
89  
90      /**
91       * Indicates whether provided unit belongs to the metric unit system.
92       *
93       * @param unit speed unit to be checked.
94       * @return true if unit belongs to metric unit system.
95       * @throws IllegalArgumentException if unit is null or not supported.
96       */
97      public static boolean isMetric(final SpeedUnit unit) {
98          return getUnitSystem(unit) == UnitSystem.METRIC;
99      }
100 
101     /**
102      * Indicates whether provided unit belongs to the imperial unit system.
103      *
104      * @param unit speed unit to be checked.
105      * @return true if unit belongs to imperial unit system.
106      * @throws IllegalArgumentException if unit is null or not supported.
107      */
108     public static boolean isImperial(final SpeedUnit unit) {
109         return getUnitSystem(unit) == UnitSystem.IMPERIAL;
110     }
111 }