View Javadoc
1   /*
2    * Copyright (C) 2020 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 weight units.
20   */
21  public enum WeightUnit {
22      /**
23       * Picogram weight unit.
24       */
25      PICOGRAM,
26  
27      /**
28       * Nanogram weight unit.
29       */
30      NANOGRAM,
31  
32      /**
33       * Microgram weight unit.
34       */
35      MICROGRAM,
36  
37      /**
38       * Milligram weight unit.
39       */
40      MILLIGRAM,
41  
42      /**
43       * Gram weight unit.
44       */
45      GRAM,
46  
47      /**
48       * Kilogram weight unit.
49       */
50      KILOGRAM,
51  
52      /**
53       * Metric tonne weight unit.
54       */
55      TONNE,
56  
57      /**
58       * Metric mega tonne weight unit.
59       */
60      MEGATONNE,
61  
62      /**
63       * US ton weight unit.
64       */
65      US_TON,
66  
67      /**
68       * UK ton weight unit.
69       */
70      UK_TON,
71  
72      /**
73       * Pound weight unit.
74       */
75      POUND,
76  
77      /**
78       * Ounce weight unit.
79       */
80      OUNCE;
81  
82      /**
83       * Returns unit system for provided weight unit.
84       *
85       * @param unit weight unit to be checked.
86       * @return unit system (metric or imperial).
87       * @throws IllegalArgumentException if unit is null or not supported.
88       */
89      public static UnitSystem getUnitSystem(final WeightUnit unit) {
90          if (unit == null) {
91              throw new IllegalArgumentException();
92          }
93  
94          return switch (unit) {
95              case US_TON, UK_TON, POUND, OUNCE -> UnitSystem.IMPERIAL;
96              default -> UnitSystem.METRIC;
97          };
98      }
99  
100     /**
101      * Gets all supported metric weight units.
102      *
103      * @return all supported metric weight units.
104      */
105     public static WeightUnit[] getMetricUnits() {
106         return new WeightUnit[]{
107                 PICOGRAM,
108                 NANOGRAM,
109                 MICROGRAM,
110                 MILLIGRAM,
111                 GRAM,
112                 KILOGRAM,
113                 TONNE,
114                 MEGATONNE
115         };
116     }
117 
118     /**
119      * Gets all supported imperial weight units.
120      *
121      * @return all supported imperial weight units.
122      */
123     public static WeightUnit[] getImperialUnits() {
124         return new WeightUnit[]{
125                 US_TON,
126                 UK_TON,
127                 POUND,
128                 OUNCE
129         };
130     }
131 
132     /**
133      * Indicates whether provided unit belongs to the metric unit system
134      *
135      * @param unit weight unit to be checked.
136      * @return true if unit belongs to metric unit system.
137      * @throws IllegalArgumentException if unit is null or not supported.
138      */
139     public static boolean isMetric(final WeightUnit unit) {
140         return getUnitSystem(unit) == UnitSystem.METRIC;
141     }
142 
143     /**
144      * Indicates whether provided unit belongs to the imperial unit system.
145      *
146      * @param unit weight unit to be checked.
147      * @return true if unit belongs to imperial unit system.
148      * @throws IllegalArgumentException if unit is null or not supported.
149      */
150     public static boolean isImperial(final WeightUnit unit) {
151         return getUnitSystem(unit) == UnitSystem.IMPERIAL;
152     }
153 }