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 surface units.
20   */
21  public enum SurfaceUnit {
22      /**
23       * Square millimeter (mm²).
24       */
25      SQUARE_MILLIMETER,
26  
27      /**
28       * Square centimeter (cm²).
29       */
30      SQUARE_CENTIMETER,
31  
32      /**
33       * Square meter (m²).
34       */
35      SQUARE_METER,
36  
37      /**
38       * Square kilometer (Km²).
39       */
40      SQUARE_KILOMETER,
41  
42      /**
43       * Square inch (sq in).
44       */
45      SQUARE_INCH,
46  
47      /**
48       * Square foot (sq ft).
49       */
50      SQUARE_FOOT,
51  
52      /**
53       * Square yard (sq yd).
54       */
55      SQUARE_YARD,
56  
57      /**
58       * Square mile (sq mi).
59       */
60      SQUARE_MILE,
61  
62      /**
63       * Centiare (ca).
64       */
65      CENTIARE,
66  
67      /**
68       * Are (a).
69       */
70      ARE,
71  
72      /**
73       * Decare (daa).
74       */
75      DECARE,
76  
77      /**
78       * Hectare (ha).
79       */
80      HECTARE,
81  
82      /**
83       * Acre (acre).
84       */
85      ACRE;
86  
87      /**
88       * Returns unit system for provided surface unit.
89       *
90       * @param unit surface unit to be checked.
91       * @return unit system (metric or imperial).
92       * @throws IllegalArgumentException if unit is null or not supported.
93       */
94      public static UnitSystem getUnitSystem(final SurfaceUnit unit) {
95          if (unit == null) {
96              throw new IllegalArgumentException();
97          }
98  
99          return switch (unit) {
100             case SQUARE_INCH, SQUARE_FOOT, SQUARE_YARD, SQUARE_MILE, ACRE -> UnitSystem.IMPERIAL;
101             default -> UnitSystem.METRIC;
102         };
103     }
104 
105     /**
106      * Gets all supported metric surface units.
107      *
108      * @return all supported metric surface units.
109      */
110     public static SurfaceUnit[] getMetricUnits() {
111         return new SurfaceUnit[]{
112                 SQUARE_MILLIMETER,
113                 SQUARE_CENTIMETER,
114                 SQUARE_METER,
115                 SQUARE_KILOMETER,
116                 CENTIARE,
117                 ARE,
118                 DECARE,
119                 HECTARE
120         };
121     }
122 
123     /**
124      * Gets all supported imperial distance units.
125      *
126      * @return all supported imperial distance units.
127      */
128     public static SurfaceUnit[] getImperialUnits() {
129         return new SurfaceUnit[]{
130                 SQUARE_INCH,
131                 SQUARE_FOOT,
132                 SQUARE_YARD,
133                 SQUARE_MILE,
134                 ACRE
135         };
136     }
137 
138     /**
139      * Indicates whether provided unit belongs to the metric unit system.
140      *
141      * @param unit distance unit to be checked.
142      * @return true if unit belongs to metric unit system.
143      * @throws IllegalArgumentException if unit is null or not supported.
144      */
145     public static boolean isMetric(final SurfaceUnit unit) {
146         return getUnitSystem(unit) == UnitSystem.METRIC;
147     }
148 
149     /**
150      * Indicates whether provided unit belongs to the imperial unit system.
151      *
152      * @param unit distance unit to be checked.
153      * @return true if unit belongs to imperial unit system.
154      * @throws IllegalArgumentException if unit is null or not supported.
155      */
156     public static boolean isImperial(final SurfaceUnit unit) {
157         return getUnitSystem(unit) == UnitSystem.IMPERIAL;
158     }
159 }