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 temperature units.
20   */
21  public enum TemperatureUnit {
22      /**
23       * Celsius (ºC).
24       */
25      CELSIUS,
26  
27      /**
28       * Fahrenheit (ºF).
29       */
30      FAHRENHEIT,
31  
32      /**
33       * Kelvin (K).
34       */
35      KELVIN;
36  
37      /**
38       * Returns unit system for provided temperature unit.
39       *
40       * @param unit temperature unit to be checked.
41       * @return unit system (metric or imperial).
42       * @throws IllegalArgumentException if unit is null or not supported.
43       */
44      @SuppressWarnings("DuplicatedCode")
45      public static UnitSystem getUnitSystem(final TemperatureUnit unit) {
46          if (unit == null) {
47              throw new IllegalArgumentException();
48          }
49  
50          if (unit == FAHRENHEIT) {
51              return UnitSystem.IMPERIAL;
52          } else {
53              return UnitSystem.METRIC;
54          }
55      }
56  
57      /**
58       * Gets all supported metric temperature units.
59       *
60       * @return all supported metric temperature units.
61       */
62      public static TemperatureUnit[] getMetricUnits() {
63          return new TemperatureUnit[]{
64                  CELSIUS,
65                  KELVIN
66          };
67      }
68  
69      /**
70       * Gets all supported imperial temperature units.
71       *
72       * @return all supported imperial temperature units.
73       */
74      public static TemperatureUnit[] getImperialUnits() {
75          return new TemperatureUnit[]{
76                  FAHRENHEIT
77          };
78      }
79  
80      /**
81       * Indicates whether provided unit belongs to the metric unit system.
82       *
83       * @param unit temperature unit to be checked.
84       * @return true if unit belongs to metric unit system.
85       * @throws IllegalArgumentException if unit is null or not supported.
86       */
87      public static boolean isMetric(final TemperatureUnit unit) {
88          return getUnitSystem(unit) == UnitSystem.METRIC;
89      }
90  
91      /**
92       * Indicates whether provided unit belongs to the imperial unit system.
93       *
94       * @param unit temperature unit to be checked.
95       * @return true if unit belongs to imperial unit system.
96       * @throws IllegalArgumentException if unit is null or not supported.
97       */
98      public static boolean isImperial(final TemperatureUnit unit) {
99          return getUnitSystem(unit) == UnitSystem.IMPERIAL;
100     }
101 }