1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
*/ |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
|
|
40 |
|
* @author Doug Lea |
41 |
|
*/ |
42 |
|
public enum TimeUnit { |
43 |
< |
NANOSECONDS (0) { |
43 |
> |
NANOSECONDS { |
44 |
|
public long toNanos(long d) { return d; } |
45 |
|
public long toMicros(long d) { return d/(C1/C0); } |
46 |
|
public long toMillis(long d) { return d/(C2/C0); } |
51 |
|
public long convert(long d, TimeUnit u) { return u.toNanos(d); } |
52 |
|
int excessNanos(long d, long m) { return (int)(d - (m*C2)); } |
53 |
|
}, |
54 |
< |
MICROSECONDS (1) { |
54 |
> |
MICROSECONDS { |
55 |
|
public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); } |
56 |
|
public long toMicros(long d) { return d; } |
57 |
|
public long toMillis(long d) { return d/(C2/C1); } |
62 |
|
public long convert(long d, TimeUnit u) { return u.toMicros(d); } |
63 |
|
int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); } |
64 |
|
}, |
65 |
< |
MILLISECONDS (2) { |
65 |
> |
MILLISECONDS { |
66 |
|
public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } |
67 |
|
public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); } |
68 |
|
public long toMillis(long d) { return d; } |
73 |
|
public long convert(long d, TimeUnit u) { return u.toMillis(d); } |
74 |
|
int excessNanos(long d, long m) { return 0; } |
75 |
|
}, |
76 |
< |
SECONDS (3) { |
76 |
> |
SECONDS { |
77 |
|
public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); } |
78 |
|
public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); } |
79 |
|
public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); } |
84 |
|
public long convert(long d, TimeUnit u) { return u.toSeconds(d); } |
85 |
|
int excessNanos(long d, long m) { return 0; } |
86 |
|
}, |
87 |
< |
MINUTES (4) { |
87 |
> |
MINUTES { |
88 |
|
public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); } |
89 |
|
public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); } |
90 |
|
public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); } |
95 |
|
public long convert(long d, TimeUnit u) { return u.toMinutes(d); } |
96 |
|
int excessNanos(long d, long m) { return 0; } |
97 |
|
}, |
98 |
< |
HOURS (5) { |
98 |
> |
HOURS { |
99 |
|
public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); } |
100 |
|
public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); } |
101 |
|
public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); } |
106 |
|
public long convert(long d, TimeUnit u) { return u.toHours(d); } |
107 |
|
int excessNanos(long d, long m) { return 0; } |
108 |
|
}, |
109 |
< |
DAYS (6) { |
109 |
> |
DAYS { |
110 |
|
public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); } |
111 |
|
public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); } |
112 |
|
public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); } |
118 |
|
int excessNanos(long d, long m) { return 0; } |
119 |
|
}; |
120 |
|
|
121 |
– |
/** |
122 |
– |
* The index of this unit. This value is no longer used in this |
123 |
– |
* version of this class, but is retained for serialization |
124 |
– |
* compatibility with previous version. |
125 |
– |
*/ |
126 |
– |
private final int index; |
127 |
– |
|
128 |
– |
/** Internal constructor */ |
129 |
– |
TimeUnit(int index) { |
130 |
– |
this.index = index; |
131 |
– |
} |
132 |
– |
|
121 |
|
// Handy constants for conversion methods |
122 |
|
static final long C0 = 1L; |
123 |
|
static final long C1 = C0 * 1000L; |
139 |
|
return d * m; |
140 |
|
} |
141 |
|
|
142 |
+ |
// To maintain full signature compatibility with 1.5, and to improve the |
143 |
+ |
// clarity of the generated javadoc (see 6287639: Abstract methods in |
144 |
+ |
// enum classes should not be listed as abstract), method convert |
145 |
+ |
// etc. are not declared abstract but otherwise act as abstract methods. |
146 |
+ |
|
147 |
|
/** |
148 |
|
* Convert the given time duration in the given unit to this |
149 |
|
* unit. Conversions from finer to coarser granularities |
154 |
|
* <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt> |
155 |
|
* if positive. |
156 |
|
* |
157 |
< |
* @param duration the time duration in the given <tt>unit</tt> |
158 |
< |
* @param unit the unit of the <tt>duration</tt> argument |
157 |
> |
* <p>For example, to convert 10 minutes to milliseconds, use: |
158 |
> |
* <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt> |
159 |
> |
* |
160 |
> |
* @param sourceDuration the time duration in the given <tt>sourceUnit</tt> |
161 |
> |
* @param sourceUnit the unit of the <tt>sourceDuration</tt> argument |
162 |
|
* @return the converted duration in this unit, |
163 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
164 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
165 |
|
*/ |
166 |
< |
public abstract long convert(long duration, TimeUnit unit); |
166 |
> |
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
167 |
> |
throw new AbstractMethodError(); |
168 |
> |
} |
169 |
|
|
170 |
|
/** |
171 |
|
* Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>. |
175 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
176 |
|
* @see #convert |
177 |
|
*/ |
178 |
< |
public abstract long toNanos(long duration); |
178 |
> |
public long toNanos(long duration) { |
179 |
> |
throw new AbstractMethodError(); |
180 |
> |
} |
181 |
|
|
182 |
|
/** |
183 |
|
* Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>. |
187 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
188 |
|
* @see #convert |
189 |
|
*/ |
190 |
< |
public abstract long toMicros(long duration); |
190 |
> |
public long toMicros(long duration) { |
191 |
> |
throw new AbstractMethodError(); |
192 |
> |
} |
193 |
|
|
194 |
|
/** |
195 |
|
* Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>. |
199 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
200 |
|
* @see #convert |
201 |
|
*/ |
202 |
< |
public abstract long toMillis(long duration); |
202 |
> |
public long toMillis(long duration) { |
203 |
> |
throw new AbstractMethodError(); |
204 |
> |
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Equivalent to <tt>SECONDS.convert(duration, this)</tt>. |
211 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
212 |
|
* @see #convert |
213 |
|
*/ |
214 |
< |
public abstract long toSeconds(long duration); |
214 |
> |
public long toSeconds(long duration) { |
215 |
> |
throw new AbstractMethodError(); |
216 |
> |
} |
217 |
|
|
218 |
|
/** |
219 |
|
* Equivalent to <tt>MINUTES.convert(duration, this)</tt>. |
222 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
223 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
224 |
|
* @see #convert |
225 |
+ |
* @since 1.6 |
226 |
|
*/ |
227 |
< |
public abstract long toMinutes(long duration); |
227 |
> |
public long toMinutes(long duration) { |
228 |
> |
throw new AbstractMethodError(); |
229 |
> |
} |
230 |
|
|
231 |
|
/** |
232 |
|
* Equivalent to <tt>HOURS.convert(duration, this)</tt>. |
235 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
236 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
237 |
|
* @see #convert |
238 |
+ |
* @since 1.6 |
239 |
|
*/ |
240 |
< |
public abstract long toHours(long duration); |
240 |
> |
public long toHours(long duration) { |
241 |
> |
throw new AbstractMethodError(); |
242 |
> |
} |
243 |
|
|
244 |
|
/** |
245 |
|
* Equivalent to <tt>DAYS.convert(duration, this)</tt>. |
246 |
|
* @param duration the duration |
247 |
|
* @return the converted duration |
248 |
|
* @see #convert |
249 |
+ |
* @since 1.6 |
250 |
|
*/ |
251 |
< |
public abstract long toDays(long duration); |
251 |
> |
public long toDays(long duration) { |
252 |
> |
throw new AbstractMethodError(); |
253 |
> |
} |
254 |
|
|
255 |
|
/** |
256 |
|
* Utility to compute the excess-nanosecond argument to wait, |
270 |
|
* method (see {@link BlockingQueue#poll BlockingQueue.poll}) |
271 |
|
* using: |
272 |
|
* |
273 |
< |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
273 |
> |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
274 |
|
* while (empty) { |
275 |
|
* unit.timedWait(this, timeout); |
276 |
|
* ... |