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 distance units.
20   */
21  public enum DistanceUnit {
22      /**
23       * Millimeter distance unit.
24       */
25      MILLIMETER,
26  
27      /**
28       * Centimeter distance unit.
29       */
30      CENTIMETER,
31  
32      /**
33       * Meter distance unit.
34       */
35      METER,
36  
37      /**
38       * Kilometer distance unit.
39       */
40      KILOMETER,
41  
42      /**
43       * Inch distance unit.
44       */
45      INCH,
46  
47      /**
48       * Foot distance unit.
49       */
50      FOOT,
51  
52      /**
53       * Yard distance unit.
54       */
55      YARD,
56  
57      /**
58       * Mile distance unit.
59       */
60      MILE;
61  
62      /**
63       * Returns unit system for provided distance unit.
64       *
65       * @param unit distance unit to be checked.
66       * @return unit system (metric or imperial).
67       * @throws IllegalArgumentException if unit is null or not supported.
68       */
69      public static UnitSystem getUnitSystem(final DistanceUnit unit) {
70          if (unit == null) {
71              throw new IllegalArgumentException();
72          }
73  
74          return switch (unit) {
75              case INCH, FOOT, YARD, MILE -> UnitSystem.IMPERIAL;
76              default -> UnitSystem.METRIC;
77          };
78      }
79  
80      /**
81       * Gets all supported metric distance units.
82       *
83       * @return all supported metric distance units.
84       */
85      public static DistanceUnit[] getMetricUnits() {
86          return new DistanceUnit[]{
87                  MILLIMETER,
88                  CENTIMETER,
89                  METER,
90                  KILOMETER
91          };
92      }
93  
94      /**
95       * Gets all supported imperial distance units.
96       *
97       * @return all supported imperial distance units.
98       */
99      public static DistanceUnit[] getImperialUnits() {
100         return new DistanceUnit[]{
101                 INCH,
102                 FOOT,
103                 YARD,
104                 MILE
105         };
106     }
107 
108     /**
109      * Indicates whether provided unit belongs to the metric unit system.
110      *
111      * @param unit distance unit to be checked.
112      * @return true if unit belongs to metric unit system.
113      * @throws IllegalArgumentException if unit is null or not supported.
114      */
115     public static boolean isMetric(final DistanceUnit unit) {
116         return getUnitSystem(unit) == UnitSystem.METRIC;
117     }
118 
119     /**
120      * Indicates whether provided unit belongs to the imperial unit system.
121      *
122      * @param unit distance unit to be checked.
123      * @return true if unit belongs to imperial unit system.
124      * @throws IllegalArgumentException if unit is null or not supported.
125      */
126     public static boolean isImperial(final DistanceUnit unit) {
127         return getUnitSystem(unit) == UnitSystem.IMPERIAL;
128     }
129 }