117 |
|
public long convert(long d, TimeUnit u) { return u.toDays(d); } |
118 |
|
int excessNanos(long d, long m) { return 0; } |
119 |
|
}; |
120 |
< |
|
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 |
< |
|
127 |
> |
|
128 |
|
/** Internal constructor */ |
129 |
|
TimeUnit(int index) { |
130 |
|
this.index = index; |
131 |
|
} |
132 |
< |
|
132 |
> |
|
133 |
|
// Handy constants for conversion methods |
134 |
|
static final long C0 = 1L; |
135 |
|
static final long C1 = C0 * 1000L; |
138 |
|
static final long C4 = C3 * 60L; |
139 |
|
static final long C5 = C4 * 60L; |
140 |
|
static final long C6 = C5 * 24L; |
141 |
< |
|
141 |
> |
|
142 |
|
static final long MAX = Long.MAX_VALUE; |
143 |
< |
|
143 |
> |
|
144 |
|
/** |
145 |
|
* Scale d by m, checking for overflow. |
146 |
|
* This has a short name to make above code more readable. |
150 |
|
if (d < -over) return Long.MIN_VALUE; |
151 |
|
return d * m; |
152 |
|
} |
153 |
< |
|
153 |
> |
|
154 |
> |
// To maintain full signature compatibility with 1.5, and to improve the |
155 |
> |
// clarity of the generated javadoc (see 6287639: Abstract methods in |
156 |
> |
// enum classes should not be listed as abstract), method convert |
157 |
> |
// etc. are not declared abstract but otherwise act as abstract methods. |
158 |
> |
|
159 |
|
/** |
160 |
|
* Convert the given time duration in the given unit to this |
161 |
|
* unit. Conversions from finer to coarser granularities |
166 |
|
* <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt> |
167 |
|
* if positive. |
168 |
|
* |
169 |
< |
* @param duration the time duration in the given <tt>unit</tt> |
170 |
< |
* @param unit the unit of the <tt>duration</tt> argument |
169 |
> |
* <p>For example, to convert 10 minutes to milliseconds, use: |
170 |
> |
* <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt> |
171 |
> |
* |
172 |
> |
* @param sourceDuration the time duration in the given <tt>sourceUnit</tt> |
173 |
> |
* @param sourceUnit the unit of the <tt>sourceDuration</tt> argument |
174 |
|
* @return the converted duration in this unit, |
175 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
176 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
177 |
|
*/ |
178 |
< |
public abstract long convert(long duration, TimeUnit unit); |
179 |
< |
|
178 |
> |
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
179 |
> |
throw new AbstractMethodError(); |
180 |
> |
} |
181 |
> |
|
182 |
|
/** |
183 |
|
* Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>. |
184 |
|
* @param duration the duration |
187 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
188 |
|
* @see #convert |
189 |
|
*/ |
190 |
< |
public abstract long toNanos(long duration); |
191 |
< |
|
190 |
> |
public long toNanos(long duration) { |
191 |
> |
throw new AbstractMethodError(); |
192 |
> |
} |
193 |
> |
|
194 |
|
/** |
195 |
|
* Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>. |
196 |
|
* @param duration the duration |
199 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
200 |
|
* @see #convert |
201 |
|
*/ |
202 |
< |
public abstract long toMicros(long duration); |
203 |
< |
|
202 |
> |
public long toMicros(long duration) { |
203 |
> |
throw new AbstractMethodError(); |
204 |
> |
} |
205 |
> |
|
206 |
|
/** |
207 |
|
* Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>. |
208 |
|
* @param duration the duration |
211 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
212 |
|
* @see #convert |
213 |
|
*/ |
214 |
< |
public abstract long toMillis(long duration); |
215 |
< |
|
214 |
> |
public long toMillis(long duration) { |
215 |
> |
throw new AbstractMethodError(); |
216 |
> |
} |
217 |
> |
|
218 |
|
/** |
219 |
|
* Equivalent to <tt>SECONDS.convert(duration, this)</tt>. |
220 |
|
* @param duration the duration |
223 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
224 |
|
* @see #convert |
225 |
|
*/ |
226 |
< |
public abstract long toSeconds(long duration); |
227 |
< |
|
226 |
> |
public long toSeconds(long duration) { |
227 |
> |
throw new AbstractMethodError(); |
228 |
> |
} |
229 |
> |
|
230 |
|
/** |
231 |
|
* Equivalent to <tt>MINUTES.convert(duration, this)</tt>. |
232 |
|
* @param duration the duration |
234 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
235 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
236 |
|
* @see #convert |
237 |
+ |
* @since 1.6 |
238 |
|
*/ |
239 |
< |
public abstract long toMinutes(long duration); |
240 |
< |
|
239 |
> |
public long toMinutes(long duration) { |
240 |
> |
throw new AbstractMethodError(); |
241 |
> |
} |
242 |
> |
|
243 |
|
/** |
244 |
|
* Equivalent to <tt>HOURS.convert(duration, this)</tt>. |
245 |
|
* @param duration the duration |
247 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
248 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
249 |
|
* @see #convert |
250 |
+ |
* @since 1.6 |
251 |
|
*/ |
252 |
< |
public abstract long toHours(long duration); |
253 |
< |
|
252 |
> |
public long toHours(long duration) { |
253 |
> |
throw new AbstractMethodError(); |
254 |
> |
} |
255 |
> |
|
256 |
|
/** |
257 |
|
* Equivalent to <tt>DAYS.convert(duration, this)</tt>. |
258 |
|
* @param duration the duration |
259 |
|
* @return the converted duration |
260 |
|
* @see #convert |
261 |
+ |
* @since 1.6 |
262 |
|
*/ |
263 |
< |
public abstract long toDays(long duration); |
264 |
< |
|
263 |
> |
public long toDays(long duration) { |
264 |
> |
throw new AbstractMethodError(); |
265 |
> |
} |
266 |
> |
|
267 |
|
/** |
268 |
|
* Utility to compute the excess-nanosecond argument to wait, |
269 |
|
* sleep, join. |
272 |
|
* @return the number of nanoseconds |
273 |
|
*/ |
274 |
|
abstract int excessNanos(long d, long m); |
275 |
< |
|
275 |
> |
|
276 |
|
/** |
277 |
< |
* Perform a timed <tt>Object.wait</tt> using this time unit. |
277 |
> |
* Performs a timed <tt>Object.wait</tt> using this time unit. |
278 |
|
* This is a convenience method that converts timeout arguments |
279 |
|
* into the form required by the <tt>Object.wait</tt> method. |
280 |
|
* |
282 |
|
* method (see {@link BlockingQueue#poll BlockingQueue.poll}) |
283 |
|
* using: |
284 |
|
* |
285 |
< |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
285 |
> |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
286 |
|
* while (empty) { |
287 |
|
* unit.timedWait(this, timeout); |
288 |
|
* ... |
290 |
|
* }</pre> |
291 |
|
* |
292 |
|
* @param obj the object to wait on |
293 |
< |
* @param timeout the maximum time to wait. |
293 |
> |
* @param timeout the maximum time to wait. If less than |
294 |
> |
* or equal to zero, do not wait at all. |
295 |
|
* @throws InterruptedException if interrupted while waiting. |
296 |
|
* @see Object#wait(long, int) |
297 |
|
*/ |
303 |
|
obj.wait(ms, ns); |
304 |
|
} |
305 |
|
} |
306 |
< |
|
306 |
> |
|
307 |
|
/** |
308 |
< |
* Perform a timed <tt>Thread.join</tt> using this time unit. |
308 |
> |
* Performs a timed <tt>Thread.join</tt> using this time unit. |
309 |
|
* This is a convenience method that converts time arguments into the |
310 |
|
* form required by the <tt>Thread.join</tt> method. |
311 |
|
* @param thread the thread to wait for |
312 |
< |
* @param timeout the maximum time to wait |
312 |
> |
* @param timeout the maximum time to wait. If less than |
313 |
> |
* or equal to zero, do not wait at all. |
314 |
|
* @throws InterruptedException if interrupted while waiting. |
315 |
|
* @see Thread#join(long, int) |
316 |
|
*/ |
322 |
|
thread.join(ms, ns); |
323 |
|
} |
324 |
|
} |
325 |
< |
|
325 |
> |
|
326 |
|
/** |
327 |
< |
* Perform a <tt>Thread.sleep</tt> using this unit. |
327 |
> |
* Performs a <tt>Thread.sleep</tt> using this unit. |
328 |
|
* This is a convenience method that converts time arguments into the |
329 |
|
* form required by the <tt>Thread.sleep</tt> method. |
330 |
< |
* @param timeout the minimum time to sleep |
330 |
> |
* @param timeout the minimum time to sleep. If less than |
331 |
> |
* or equal to zero, do not sleep at all. |
332 |
|
* @throws InterruptedException if interrupted while sleeping. |
333 |
|
* @see Thread#sleep |
334 |
|
*/ |
339 |
|
Thread.sleep(ms, ns); |
340 |
|
} |
341 |
|
} |
342 |
< |
|
342 |
> |
|
343 |
|
} |