9 |
|
/** |
10 |
|
* A <tt>TimeUnit</tt> represents time durations at a given unit of |
11 |
|
* granularity and provides utility methods to convert across units, |
12 |
< |
* and to perform timing and delay operations in these units. |
13 |
< |
* <tt>TimeUnit</tt> is a "featherweight" class. |
14 |
< |
* It does not maintain time information, but only helps organize and |
15 |
< |
* use time representations that may be maintained separately across |
16 |
< |
* various contexts. |
12 |
> |
* and to perform timing and delay operations in these units. A |
13 |
> |
* <tt>TimeUnit</tt> does not maintain time information, but only |
14 |
> |
* helps organize and use time representations that may be maintained |
15 |
> |
* separately across various contexts. |
16 |
|
* |
17 |
< |
* <p>This class cannot be directly instantiated. Use the {@link |
18 |
< |
* #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, and {@link |
19 |
< |
* #NANOSECONDS} static instances that provide predefined units of |
20 |
< |
* precision. If you use these frequently, consider statically |
22 |
< |
* importing this class. |
23 |
< |
* |
24 |
< |
* <p>A <tt>TimeUnit</tt> is mainly used to inform blocking timeout |
25 |
< |
* methods how the timeout parameter should be interpreted. For |
26 |
< |
* example, the following code will timeout in 50 milliseconds if the |
27 |
< |
* {@link java.util.concurrent.locks.Lock lock} is not available: |
17 |
> |
* <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods |
18 |
> |
* how a given timing parameter should be interpreted. For example, |
19 |
> |
* the following code will timeout in 50 milliseconds if the {@link |
20 |
> |
* java.util.concurrent.locks.Lock lock} is not available: |
21 |
|
* |
22 |
|
* <pre> Lock lock = ...; |
23 |
|
* if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... |
35 |
|
* @since 1.5 |
36 |
|
* @author Doug Lea |
37 |
|
*/ |
38 |
< |
public final class TimeUnit implements java.io.Serializable { |
39 |
< |
/* ordered indices for each time unit */ |
47 |
< |
private static final int NS = 0; |
48 |
< |
private static final int US = 1; |
49 |
< |
private static final int MS = 2; |
50 |
< |
private static final int S = 3; |
51 |
< |
|
52 |
< |
/** quick lookup table for conversion factors */ |
53 |
< |
private static final int[] multipliers = { 1, |
54 |
< |
1000, |
55 |
< |
1000*1000, |
56 |
< |
1000*1000*1000 }; |
57 |
< |
|
58 |
< |
/** lookup table to check saturation */ |
59 |
< |
private static final long[] overflows = { |
60 |
< |
// Note that because we are dividing these down anyway, |
61 |
< |
// we don't have to deal with asymmetry of MIN/MAX values. |
62 |
< |
0, // unused |
63 |
< |
Long.MAX_VALUE / 1000, |
64 |
< |
Long.MAX_VALUE / (1000 * 1000), |
65 |
< |
Long.MAX_VALUE / (1000 * 1000 * 1000) }; |
38 |
> |
public enum TimeUnit { |
39 |
> |
NANOSECONDS(0), MICROSECONDS(1), MILLISECONDS(2), SECONDS(3); |
40 |
|
|
41 |
|
/** the index of this unit */ |
42 |
< |
private int index; |
69 |
< |
/** Common name for unit */ |
70 |
< |
private final String unitName; |
42 |
> |
private final int index; |
43 |
|
|
44 |
< |
/** private constructor */ |
45 |
< |
TimeUnit(int index, String name) { |
44 |
> |
/** Internal constructor */ |
45 |
> |
TimeUnit(int index) { |
46 |
|
this.index = index; |
75 |
– |
this.unitName = name; |
47 |
|
} |
48 |
|
|
49 |
< |
/** Unit for one-second granularities. */ |
50 |
< |
public static final TimeUnit SECONDS = new TimeUnit(S, "seconds"); |
51 |
< |
/** Unit for one-millisecond granularities. */ |
52 |
< |
public static final TimeUnit MILLISECONDS = new TimeUnit(MS, "milliseconds"); |
53 |
< |
/** Unit for one-microsecond granularities. */ |
54 |
< |
public static final TimeUnit MICROSECONDS = new TimeUnit(US, "microseconds"); |
55 |
< |
/** Unit for one-nanosecond granularities. */ |
56 |
< |
public static final TimeUnit NANOSECONDS = new TimeUnit(NS, "nanoseconds"); |
57 |
< |
|
58 |
< |
/** |
59 |
< |
* Utility method to compute the excess-nanosecond argument to |
60 |
< |
* wait, sleep, join. |
49 |
> |
/** Lookup table for conversion factors */ |
50 |
> |
private static final int[] multipliers = { |
51 |
> |
1, |
52 |
> |
1000, |
53 |
> |
1000 * 1000, |
54 |
> |
1000 * 1000 * 1000 |
55 |
> |
}; |
56 |
> |
|
57 |
> |
/** |
58 |
> |
* Lookup table to check saturation. Note that because we are |
59 |
> |
* dividing these down, we don't have to deal with asymmetry of |
60 |
> |
* MIN/MAX values. |
61 |
|
*/ |
62 |
< |
private int excessNanos(long time, long ms) { |
63 |
< |
if (index == NS) |
64 |
< |
return (int) (time - (ms * multipliers[MS-NS])); |
65 |
< |
else if (index == US) |
66 |
< |
return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS])); |
67 |
< |
else |
97 |
< |
return 0; |
98 |
< |
} |
62 |
> |
private static final long[] overflows = { |
63 |
> |
0, // unused |
64 |
> |
Long.MAX_VALUE / 1000, |
65 |
> |
Long.MAX_VALUE / (1000 * 1000), |
66 |
> |
Long.MAX_VALUE / (1000 * 1000 * 1000) |
67 |
> |
}; |
68 |
|
|
69 |
|
/** |
70 |
|
* Perform conversion based on given delta representing the |
126 |
|
* @see #convert |
127 |
|
*/ |
128 |
|
public long toMicros(long duration) { |
129 |
< |
return doConvert(index - US, duration); |
129 |
> |
return doConvert(index - MICROSECONDS.index, duration); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
138 |
|
* @see #convert |
139 |
|
*/ |
140 |
|
public long toMillis(long duration) { |
141 |
< |
return doConvert(index - MS, duration); |
141 |
> |
return doConvert(index - MILLISECONDS.index, duration); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
148 |
|
* @see #convert |
149 |
|
*/ |
150 |
|
public long toSeconds(long duration) { |
151 |
< |
return doConvert(index - S, duration); |
151 |
> |
return doConvert(index - SECONDS.index, duration); |
152 |
> |
} |
153 |
> |
|
154 |
> |
|
155 |
> |
/** |
156 |
> |
* Utility method to compute the excess-nanosecond argument to |
157 |
> |
* wait, sleep, join. |
158 |
> |
*/ |
159 |
> |
private int excessNanos(long time, long ms) { |
160 |
> |
if (this == NANOSECONDS) |
161 |
> |
return (int) (time - (ms * 1000 * 1000)); |
162 |
> |
if (this == MICROSECONDS) |
163 |
> |
return (int) ((time * 1000) - (ms * 1000 * 1000)); |
164 |
> |
return 0; |
165 |
|
} |
166 |
|
|
167 |
|
/** |
228 |
|
} |
229 |
|
} |
230 |
|
|
249 |
– |
/** |
250 |
– |
* Return the common name for this unit. |
251 |
– |
*/ |
252 |
– |
public String toString() { |
253 |
– |
return unitName; |
254 |
– |
} |
255 |
– |
|
256 |
– |
/** |
257 |
– |
* Resolves instances being deserialized to a single instance per |
258 |
– |
* unit. |
259 |
– |
*/ |
260 |
– |
private Object readResolve() { |
261 |
– |
switch(index) { |
262 |
– |
case NS: return NANOSECONDS; |
263 |
– |
case US: return MICROSECONDS; |
264 |
– |
case MS: return MILLISECONDS; |
265 |
– |
case S: return SECONDS; |
266 |
– |
default: assert(false); return null; |
267 |
– |
} |
268 |
– |
} |
231 |
|
} |