author | dl |
Fri, 03 Mar 2017 10:45:38 -0800 | |
changeset 44039 | 058585425bb7 |
parent 36732 | 7e6686d3f98a |
child 45937 | 646816090183 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
35640 | 38 |
import java.time.temporal.ChronoUnit; |
39 |
import java.util.Objects; |
|
40 |
||
2 | 41 |
/** |
18768 | 42 |
* A {@code TimeUnit} represents time durations at a given unit of |
2 | 43 |
* granularity and provides utility methods to convert across units, |
44 |
* and to perform timing and delay operations in these units. A |
|
18768 | 45 |
* {@code TimeUnit} does not maintain time information, but only |
2 | 46 |
* helps organize and use time representations that may be maintained |
47 |
* separately across various contexts. A nanosecond is defined as one |
|
48 |
* thousandth of a microsecond, a microsecond as one thousandth of a |
|
49 |
* millisecond, a millisecond as one thousandth of a second, a minute |
|
50 |
* as sixty seconds, an hour as sixty minutes, and a day as twenty four |
|
51 |
* hours. |
|
52 |
* |
|
18768 | 53 |
* <p>A {@code TimeUnit} is mainly used to inform time-based methods |
2 | 54 |
* how a given timing parameter should be interpreted. For example, |
55 |
* the following code will timeout in 50 milliseconds if the {@link |
|
56 |
* java.util.concurrent.locks.Lock lock} is not available: |
|
57 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
58 |
* <pre> {@code |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
59 |
* Lock lock = ...; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
60 |
* if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre> |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
61 |
* |
2 | 62 |
* while this code will timeout in 50 seconds: |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
63 |
* <pre> {@code |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
64 |
* Lock lock = ...; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
65 |
* if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</pre> |
2 | 66 |
* |
67 |
* Note however, that there is no guarantee that a particular timeout |
|
68 |
* implementation will be able to notice the passage of time at the |
|
18768 | 69 |
* same granularity as the given {@code TimeUnit}. |
2 | 70 |
* |
71 |
* @since 1.5 |
|
72 |
* @author Doug Lea |
|
73 |
*/ |
|
74 |
public enum TimeUnit { |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
75 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
76 |
* Time unit representing one thousandth of a microsecond. |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
77 |
*/ |
36732 | 78 |
NANOSECONDS(TimeUnit.NANO_SCALE), |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
79 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
80 |
* Time unit representing one thousandth of a millisecond. |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
81 |
*/ |
36732 | 82 |
MICROSECONDS(TimeUnit.MICRO_SCALE), |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
83 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
84 |
* Time unit representing one thousandth of a second. |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
85 |
*/ |
36732 | 86 |
MILLISECONDS(TimeUnit.MILLI_SCALE), |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
87 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
88 |
* Time unit representing one second. |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
89 |
*/ |
36732 | 90 |
SECONDS(TimeUnit.SECOND_SCALE), |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
91 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
92 |
* Time unit representing sixty seconds. |
28777 | 93 |
* @since 1.6 |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
94 |
*/ |
36732 | 95 |
MINUTES(TimeUnit.MINUTE_SCALE), |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
96 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
97 |
* Time unit representing sixty minutes. |
28777 | 98 |
* @since 1.6 |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
99 |
*/ |
36732 | 100 |
HOURS(TimeUnit.HOUR_SCALE), |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
101 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
102 |
* Time unit representing twenty four hours. |
28777 | 103 |
* @since 1.6 |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
104 |
*/ |
36732 | 105 |
DAYS(TimeUnit.DAY_SCALE); |
106 |
||
107 |
// Scales as constants |
|
108 |
private static final long NANO_SCALE = 1L; |
|
109 |
private static final long MICRO_SCALE = 1000L * NANO_SCALE; |
|
110 |
private static final long MILLI_SCALE = 1000L * MICRO_SCALE; |
|
111 |
private static final long SECOND_SCALE = 1000L * MILLI_SCALE; |
|
112 |
private static final long MINUTE_SCALE = 60L * SECOND_SCALE; |
|
113 |
private static final long HOUR_SCALE = 60L * MINUTE_SCALE; |
|
114 |
private static final long DAY_SCALE = 24L * HOUR_SCALE; |
|
115 |
||
116 |
/* |
|
117 |
* Instances cache conversion ratios and saturation cutoffs for |
|
118 |
* the units up through SECONDS. Other cases compute them, in |
|
119 |
* method cvt. |
|
120 |
*/ |
|
2 | 121 |
|
36732 | 122 |
private final long scale; |
123 |
private final long maxNanos; |
|
124 |
private final long maxMicros; |
|
125 |
private final long maxMillis; |
|
126 |
private final long maxSecs; |
|
127 |
private final long microRatio; |
|
128 |
private final int milliRatio; // fits in 32 bits |
|
129 |
private final int secRatio; // fits in 32 bits |
|
2 | 130 |
|
36732 | 131 |
private TimeUnit(long s) { |
132 |
this.scale = s; |
|
133 |
this.maxNanos = Long.MAX_VALUE / s; |
|
134 |
long ur = (s >= MICRO_SCALE) ? (s / MICRO_SCALE) : (MICRO_SCALE / s); |
|
135 |
this.microRatio = ur; |
|
136 |
this.maxMicros = Long.MAX_VALUE / ur; |
|
137 |
long mr = (s >= MILLI_SCALE) ? (s / MILLI_SCALE) : (MILLI_SCALE / s); |
|
138 |
this.milliRatio = (int)mr; |
|
139 |
this.maxMillis = Long.MAX_VALUE / mr; |
|
140 |
long sr = (s >= SECOND_SCALE) ? (s / SECOND_SCALE) : (SECOND_SCALE / s); |
|
141 |
this.secRatio = (int)sr; |
|
142 |
this.maxSecs = Long.MAX_VALUE / sr; |
|
143 |
} |
|
2 | 144 |
|
145 |
/** |
|
36732 | 146 |
* General conversion utility. |
147 |
* |
|
148 |
* @param d duration |
|
149 |
* @param dst result unit scale |
|
150 |
* @param src source unit scale |
|
2 | 151 |
*/ |
36732 | 152 |
private static long cvt(long d, long dst, long src) { |
153 |
long r, m; |
|
154 |
if (src == dst) |
|
155 |
return d; |
|
156 |
else if (src < dst) |
|
157 |
return d / (dst / src); |
|
158 |
else if (d > (m = Long.MAX_VALUE / (r = src / dst))) |
|
159 |
return Long.MAX_VALUE; |
|
160 |
else if (d < -m) |
|
161 |
return Long.MIN_VALUE; |
|
162 |
else |
|
163 |
return d * r; |
|
2 | 164 |
} |
165 |
||
166 |
/** |
|
18768 | 167 |
* Converts the given time duration in the given unit to this unit. |
168 |
* Conversions from finer to coarser granularities truncate, so |
|
169 |
* lose precision. For example, converting {@code 999} milliseconds |
|
170 |
* to seconds results in {@code 0}. Conversions from coarser to |
|
171 |
* finer granularities with arguments that would numerically |
|
172 |
* overflow saturate to {@code Long.MIN_VALUE} if negative or |
|
173 |
* {@code Long.MAX_VALUE} if positive. |
|
2 | 174 |
* |
175 |
* <p>For example, to convert 10 minutes to milliseconds, use: |
|
18768 | 176 |
* {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)} |
2 | 177 |
* |
18768 | 178 |
* @param sourceDuration the time duration in the given {@code sourceUnit} |
179 |
* @param sourceUnit the unit of the {@code sourceDuration} argument |
|
2 | 180 |
* @return the converted duration in this unit, |
36732 | 181 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
182 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 183 |
*/ |
184 |
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
|
36732 | 185 |
switch (this) { |
186 |
case NANOSECONDS: return sourceUnit.toNanos(sourceDuration); |
|
187 |
case MICROSECONDS: return sourceUnit.toMicros(sourceDuration); |
|
188 |
case MILLISECONDS: return sourceUnit.toMillis(sourceDuration); |
|
189 |
case SECONDS: return sourceUnit.toSeconds(sourceDuration); |
|
190 |
default: return cvt(sourceDuration, scale, sourceUnit.scale); |
|
191 |
} |
|
2 | 192 |
} |
193 |
||
194 |
/** |
|
18768 | 195 |
* Equivalent to |
196 |
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
|
2 | 197 |
* @param duration the duration |
198 |
* @return the converted duration, |
|
36732 | 199 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
200 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 201 |
*/ |
202 |
public long toNanos(long duration) { |
|
36732 | 203 |
long s, m; |
204 |
if ((s = scale) == NANO_SCALE) |
|
205 |
return duration; |
|
206 |
else if (duration > (m = maxNanos)) |
|
207 |
return Long.MAX_VALUE; |
|
208 |
else if (duration < -m) |
|
209 |
return Long.MIN_VALUE; |
|
210 |
else |
|
211 |
return duration * s; |
|
2 | 212 |
} |
213 |
||
214 |
/** |
|
18768 | 215 |
* Equivalent to |
216 |
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}. |
|
2 | 217 |
* @param duration the duration |
218 |
* @return the converted duration, |
|
36732 | 219 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
220 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 221 |
*/ |
222 |
public long toMicros(long duration) { |
|
36732 | 223 |
long s, m; |
224 |
if ((s = scale) == MICRO_SCALE) |
|
225 |
return duration; |
|
226 |
else if (s < MICRO_SCALE) |
|
227 |
return duration / microRatio; |
|
228 |
else if (duration > (m = maxMicros)) |
|
229 |
return Long.MAX_VALUE; |
|
230 |
else if (duration < -m) |
|
231 |
return Long.MIN_VALUE; |
|
232 |
else |
|
233 |
return duration * microRatio; |
|
2 | 234 |
} |
235 |
||
236 |
/** |
|
18768 | 237 |
* Equivalent to |
238 |
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}. |
|
2 | 239 |
* @param duration the duration |
240 |
* @return the converted duration, |
|
36732 | 241 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
242 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 243 |
*/ |
244 |
public long toMillis(long duration) { |
|
36732 | 245 |
long s, m; |
246 |
if ((s = scale) == MILLI_SCALE) |
|
247 |
return duration; |
|
248 |
else if (s < MILLI_SCALE) |
|
249 |
return duration / milliRatio; |
|
250 |
else if (duration > (m = maxMillis)) |
|
251 |
return Long.MAX_VALUE; |
|
252 |
else if (duration < -m) |
|
253 |
return Long.MIN_VALUE; |
|
254 |
else |
|
255 |
return duration * milliRatio; |
|
2 | 256 |
} |
257 |
||
258 |
/** |
|
18768 | 259 |
* Equivalent to |
260 |
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}. |
|
2 | 261 |
* @param duration the duration |
262 |
* @return the converted duration, |
|
36732 | 263 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
264 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 265 |
*/ |
266 |
public long toSeconds(long duration) { |
|
36732 | 267 |
long s, m; |
268 |
if ((s = scale) == SECOND_SCALE) |
|
269 |
return duration; |
|
270 |
else if (s < SECOND_SCALE) |
|
271 |
return duration / secRatio; |
|
272 |
else if (duration > (m = maxSecs)) |
|
273 |
return Long.MAX_VALUE; |
|
274 |
else if (duration < -m) |
|
275 |
return Long.MIN_VALUE; |
|
276 |
else |
|
277 |
return duration * secRatio; |
|
2 | 278 |
} |
279 |
||
280 |
/** |
|
18768 | 281 |
* Equivalent to |
282 |
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}. |
|
2 | 283 |
* @param duration the duration |
284 |
* @return the converted duration, |
|
36732 | 285 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
286 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 287 |
* @since 1.6 |
288 |
*/ |
|
289 |
public long toMinutes(long duration) { |
|
36732 | 290 |
return cvt(duration, MINUTE_SCALE, scale); |
2 | 291 |
} |
292 |
||
293 |
/** |
|
18768 | 294 |
* Equivalent to |
295 |
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}. |
|
2 | 296 |
* @param duration the duration |
297 |
* @return the converted duration, |
|
36732 | 298 |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
299 |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 300 |
* @since 1.6 |
301 |
*/ |
|
302 |
public long toHours(long duration) { |
|
36732 | 303 |
return cvt(duration, HOUR_SCALE, scale); |
2 | 304 |
} |
305 |
||
306 |
/** |
|
18768 | 307 |
* Equivalent to |
308 |
* {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}. |
|
2 | 309 |
* @param duration the duration |
310 |
* @return the converted duration |
|
311 |
* @since 1.6 |
|
312 |
*/ |
|
313 |
public long toDays(long duration) { |
|
36732 | 314 |
return cvt(duration, DAY_SCALE, scale); |
2 | 315 |
} |
316 |
||
317 |
/** |
|
318 |
* Utility to compute the excess-nanosecond argument to wait, |
|
319 |
* sleep, join. |
|
320 |
* @param d the duration |
|
321 |
* @param m the number of milliseconds |
|
322 |
* @return the number of nanoseconds |
|
323 |
*/ |
|
36732 | 324 |
private int excessNanos(long d, long m) { |
325 |
long s; |
|
326 |
if ((s = scale) == NANO_SCALE) |
|
327 |
return (int)(d - (m * MILLI_SCALE)); |
|
328 |
else if (s == MICRO_SCALE) |
|
329 |
return (int)((d * 1000L) - (m * MILLI_SCALE)); |
|
330 |
else |
|
331 |
return 0; |
|
332 |
} |
|
2 | 333 |
|
334 |
/** |
|
7518 | 335 |
* Performs a timed {@link Object#wait(long, int) Object.wait} |
336 |
* using this time unit. |
|
2 | 337 |
* This is a convenience method that converts timeout arguments |
18768 | 338 |
* into the form required by the {@code Object.wait} method. |
2 | 339 |
* |
18768 | 340 |
* <p>For example, you could implement a blocking {@code poll} |
2 | 341 |
* method (see {@link BlockingQueue#poll BlockingQueue.poll}) |
342 |
* using: |
|
343 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
28777
diff
changeset
|
344 |
* <pre> {@code |
7518 | 345 |
* public synchronized Object poll(long timeout, TimeUnit unit) |
346 |
* throws InterruptedException { |
|
347 |
* while (empty) { |
|
348 |
* unit.timedWait(this, timeout); |
|
349 |
* ... |
|
350 |
* } |
|
351 |
* }}</pre> |
|
2 | 352 |
* |
353 |
* @param obj the object to wait on |
|
354 |
* @param timeout the maximum time to wait. If less than |
|
355 |
* or equal to zero, do not wait at all. |
|
7518 | 356 |
* @throws InterruptedException if interrupted while waiting |
2 | 357 |
*/ |
358 |
public void timedWait(Object obj, long timeout) |
|
7518 | 359 |
throws InterruptedException { |
2 | 360 |
if (timeout > 0) { |
361 |
long ms = toMillis(timeout); |
|
362 |
int ns = excessNanos(timeout, ms); |
|
363 |
obj.wait(ms, ns); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
/** |
|
7518 | 368 |
* Performs a timed {@link Thread#join(long, int) Thread.join} |
369 |
* using this time unit. |
|
2 | 370 |
* This is a convenience method that converts time arguments into the |
18768 | 371 |
* form required by the {@code Thread.join} method. |
7518 | 372 |
* |
2 | 373 |
* @param thread the thread to wait for |
374 |
* @param timeout the maximum time to wait. If less than |
|
375 |
* or equal to zero, do not wait at all. |
|
7518 | 376 |
* @throws InterruptedException if interrupted while waiting |
2 | 377 |
*/ |
378 |
public void timedJoin(Thread thread, long timeout) |
|
7518 | 379 |
throws InterruptedException { |
2 | 380 |
if (timeout > 0) { |
381 |
long ms = toMillis(timeout); |
|
382 |
int ns = excessNanos(timeout, ms); |
|
383 |
thread.join(ms, ns); |
|
384 |
} |
|
385 |
} |
|
386 |
||
387 |
/** |
|
7518 | 388 |
* Performs a {@link Thread#sleep(long, int) Thread.sleep} using |
389 |
* this time unit. |
|
2 | 390 |
* This is a convenience method that converts time arguments into the |
18768 | 391 |
* form required by the {@code Thread.sleep} method. |
7518 | 392 |
* |
2 | 393 |
* @param timeout the minimum time to sleep. If less than |
394 |
* or equal to zero, do not sleep at all. |
|
7518 | 395 |
* @throws InterruptedException if interrupted while sleeping |
2 | 396 |
*/ |
397 |
public void sleep(long timeout) throws InterruptedException { |
|
398 |
if (timeout > 0) { |
|
399 |
long ms = toMillis(timeout); |
|
400 |
int ns = excessNanos(timeout, ms); |
|
401 |
Thread.sleep(ms, ns); |
|
402 |
} |
|
403 |
} |
|
404 |
||
35640 | 405 |
/** |
406 |
* Converts this {@code TimeUnit} to the equivalent {@code ChronoUnit}. |
|
407 |
* |
|
408 |
* @return the converted equivalent ChronoUnit |
|
409 |
* @since 9 |
|
410 |
*/ |
|
411 |
public ChronoUnit toChronoUnit() { |
|
412 |
switch (this) { |
|
413 |
case NANOSECONDS: return ChronoUnit.NANOS; |
|
414 |
case MICROSECONDS: return ChronoUnit.MICROS; |
|
415 |
case MILLISECONDS: return ChronoUnit.MILLIS; |
|
416 |
case SECONDS: return ChronoUnit.SECONDS; |
|
417 |
case MINUTES: return ChronoUnit.MINUTES; |
|
418 |
case HOURS: return ChronoUnit.HOURS; |
|
419 |
case DAYS: return ChronoUnit.DAYS; |
|
420 |
default: throw new AssertionError(); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* Converts a {@code ChronoUnit} to the equivalent {@code TimeUnit}. |
|
426 |
* |
|
427 |
* @param chronoUnit the ChronoUnit to convert |
|
428 |
* @return the converted equivalent TimeUnit |
|
429 |
* @throws IllegalArgumentException if {@code chronoUnit} has no |
|
430 |
* equivalent TimeUnit |
|
431 |
* @throws NullPointerException if {@code chronoUnit} is null |
|
432 |
* @since 9 |
|
433 |
*/ |
|
434 |
public static TimeUnit of(ChronoUnit chronoUnit) { |
|
435 |
switch (Objects.requireNonNull(chronoUnit, "chronoUnit")) { |
|
436 |
case NANOS: return TimeUnit.NANOSECONDS; |
|
437 |
case MICROS: return TimeUnit.MICROSECONDS; |
|
438 |
case MILLIS: return TimeUnit.MILLISECONDS; |
|
439 |
case SECONDS: return TimeUnit.SECONDS; |
|
440 |
case MINUTES: return TimeUnit.MINUTES; |
|
441 |
case HOURS: return TimeUnit.HOURS; |
|
442 |
case DAYS: return TimeUnit.DAYS; |
|
443 |
default: |
|
444 |
throw new IllegalArgumentException( |
|
445 |
"No TimeUnit equivalent for " + chronoUnit); |
|
446 |
} |
|
447 |
} |
|
448 |
||
2 | 449 |
} |