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 volume units.
20   */
21  public enum VolumeUnit {
22      /**
23       * Cubic centimeter.
24       */
25      CUBIC_CENTIMETER,
26  
27      /**
28       * Milliliter.
29       */
30      MILLILITER,
31  
32      /**
33       * Cubic decimeter.
34       */
35      CUBIC_DECIMETER,
36  
37      /**
38       * Liter.
39       */
40      LITER,
41  
42      /**
43       * Hectoliter.
44       */
45      HECTOLITER,
46  
47      /**
48       * Cubic meter.
49       */
50      CUBIC_METER,
51  
52      /**
53       * Cubic inch.
54       */
55      CUBIC_INCH,
56  
57      /**
58       * Pint.
59       */
60      PINT,
61  
62      /**
63       * Gallon.
64       */
65      GALLON,
66  
67      /**
68       * Cubic foot.
69       */
70      CUBIC_FOOT,
71  
72      /**
73       * Barrel.
74       */
75      BARREL;
76  
77      /**
78       * Returns unit system from provided volume unit.
79       *
80       * @param unit volume unit to be checked.
81       * @return unit system (metric or imperial).
82       * @throws IllegalArgumentException if unit is null or not supported.
83       */
84      public static UnitSystem getUnitSystem(final VolumeUnit unit) {
85          if (unit == null) {
86              throw new IllegalArgumentException();
87          }
88  
89          return switch (unit) {
90              case CUBIC_INCH, PINT, GALLON, CUBIC_FOOT, BARREL -> UnitSystem.IMPERIAL;
91              default -> UnitSystem.METRIC;
92          };
93      }
94  
95      /**
96       * Gets all supported metric volume units.
97       *
98       * @return all supported metric volume units.
99       */
100     public static VolumeUnit[] getMetricUnits() {
101         return new VolumeUnit[]{
102                 CUBIC_CENTIMETER,
103                 MILLILITER,
104                 CUBIC_DECIMETER,
105                 LITER,
106                 HECTOLITER,
107                 CUBIC_METER
108         };
109     }
110 
111     /**
112      * Gets all supported imperial volume units.
113      *
114      * @return all supported imperial volume units.
115      */
116     public static VolumeUnit[] getImperialUnits() {
117         return new VolumeUnit[]{
118                 CUBIC_INCH,
119                 PINT,
120                 GALLON,
121                 CUBIC_FOOT,
122                 BARREL
123         };
124     }
125 
126     /**
127      * Indicates whether provided unit belongs to the metric unit system.
128      *
129      * @param unit volume unit to be checked.
130      * @return true if unit belongs to metric unit system.
131      * @throws IllegalArgumentException if unit is null or not supported.
132      */
133     public static boolean isMetric(final VolumeUnit unit) {
134         return getUnitSystem(unit) == UnitSystem.METRIC;
135     }
136 
137     /**
138      * Indicates whether provided unit belongs to the imperial unit system.
139      *
140      * @param unit volume unit to be checked.
141      * @return true if unit belongs to imperial unit system.
142      * @throws IllegalArgumentException if unit is null or not supported.
143      */
144     public static boolean isImperial(final VolumeUnit unit) {
145         return getUnitSystem(unit) == UnitSystem.IMPERIAL;
146     }
147 }