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 time units.
20 */
21 public enum TimeUnit {
22 /**
23 * Nanosecond time unit. This belongs to the International System of units.
24 */
25 NANOSECOND,
26
27 /**
28 * Microsecond time unit. This belongs to the International System of units.
29 */
30 MICROSECOND,
31
32 /**
33 * Millisecond time unit. This belongs to the International System of units.
34 */
35 MILLISECOND,
36
37 /**
38 * Second time unit. This belongs to the International System of units.
39 */
40 SECOND,
41
42 /**
43 * Minute time unit.
44 */
45 MINUTE,
46
47 /**
48 * Hour time unit.
49 */
50 HOUR,
51
52 /**
53 * Day time unit.
54 */
55 DAY,
56
57 /**
58 * Week time unit.
59 */
60 WEEK,
61
62 /**
63 * Month time unit (considered as 30 days).
64 */
65 MONTH,
66
67 /**
68 * Year time unit (considered as 365 days).
69 */
70 YEAR,
71
72 /**
73 * Century time unit (considered as 100 years).
74 */
75 CENTURY;
76
77 /**
78 * Returns unit system for provided time unit.
79 *
80 * @param unit time unit to be checked.
81 * @return returns metric system only for units belonging to the International
82 * System of units.
83 * @throws IllegalArgumentException if unit is null or does not belong to the
84 * international system of units.
85 */
86 public static UnitSystem getUnitSystem(final TimeUnit unit) {
87 if (unit == null) {
88 throw new IllegalArgumentException();
89 }
90
91 return switch (unit) {
92 case NANOSECOND, MICROSECOND, MILLISECOND, SECOND -> UnitSystem.METRIC;
93 default -> throw new IllegalArgumentException();
94 };
95 }
96
97 /**
98 * Gets all supported metric time units.
99 *
100 * @return all supported metric time units.
101 */
102 public static TimeUnit[] getMetricUnits() {
103 return new TimeUnit[]{
104 NANOSECOND,
105 MICROSECOND,
106 MILLISECOND,
107 SECOND
108 };
109 }
110
111 /**
112 * Gets all supported units not belonging to the International System
113 * of Units.
114 *
115 * @return all units not belonging to the International System of Units.
116 */
117 public static TimeUnit[] getNonInternationalSystemUnits() {
118 return new TimeUnit[]{
119 MINUTE,
120 HOUR,
121 DAY,
122 WEEK,
123 MONTH,
124 YEAR,
125 CENTURY
126 };
127 }
128
129 /**
130 * Indicates whether provided unit belongs to the metric unit system.
131 *
132 * @param unit time unit to be checked.
133 * @return true if unit belongs to metric unit system.
134 * @throws IllegalArgumentException if unit is null or not supported.
135 */
136 public static boolean isMetric(final TimeUnit unit) {
137 if (unit == null) {
138 throw new IllegalArgumentException();
139 }
140
141 try {
142 return getUnitSystem(unit) == UnitSystem.METRIC;
143 } catch (IllegalArgumentException e) {
144 return false;
145 }
146 }
147
148 /**
149 * Indicates whether provided unit belongs to the International System of
150 * units.
151 *
152 * @param unit time unit to be checked.
153 * @return true if unit does not belong to the International System of units,
154 * false otherwise.
155 * @throws IllegalArgumentException if unit is null or not supported.
156 */
157 public static boolean isNonInternationalSystem(final TimeUnit unit) {
158 if (unit == null) {
159 throw new IllegalArgumentException();
160 }
161
162 return !isMetric(unit);
163 }
164 }