ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.32
Committed: Sat Jun 18 12:03:05 2005 UTC (18 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.31: +33 -10 lines
Log Message:
Avoid "abstract" to ensure signature compatibility

File Contents

# User Rev Content
1 dl 1.2 /*
2 dl 1.22 * 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 dl 1.2
7 tim 1.1 package java.util.concurrent;
8    
9     /**
10 dl 1.27 * 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. 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. A nanosecond is defined as one
16     * thousandth of a microsecond, a microsecond as one thousandth of a
17     * millisecond, a millisecond as one thousandth of a second, a minute
18     * as sixty seconds, an hour as sixty minutes, and a day as twenty four
19     * hours.
20     *
21     * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
22     * how a given timing parameter should be interpreted. For example,
23     * the following code will timeout in 50 milliseconds if the {@link
24     * java.util.concurrent.locks.Lock lock} is not available:
25     *
26     * <pre> Lock lock = ...;
27     * if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
28     * </pre>
29     * while this code will timeout in 50 seconds:
30     * <pre>
31     * Lock lock = ...;
32     * if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
33     * </pre>
34     *
35     * Note however, that there is no guarantee that a particular timeout
36     * implementation will be able to notice the passage of time at the
37     * same granularity as the given <tt>TimeUnit</tt>.
38     *
39     * @since 1.5
40     * @author Doug Lea
41     */
42 dl 1.20 public enum TimeUnit {
43 dl 1.27 NANOSECONDS (0) {
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); }
47     public long toSeconds(long d) { return d/(C3/C0); }
48     public long toMinutes(long d) { return d/(C4/C0); }
49     public long toHours(long d) { return d/(C5/C0); }
50     public long toDays(long d) { return d/(C6/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) {
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); }
58     public long toSeconds(long d) { return d/(C3/C1); }
59     public long toMinutes(long d) { return d/(C4/C1); }
60     public long toHours(long d) { return d/(C5/C1); }
61     public long toDays(long d) { return d/(C6/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) {
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; }
69     public long toSeconds(long d) { return d/(C3/C2); }
70     public long toMinutes(long d) { return d/(C4/C2); }
71     public long toHours(long d) { return d/(C5/C2); }
72     public long toDays(long d) { return d/(C6/C2); }
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) {
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)); }
80     public long toSeconds(long d) { return d; }
81     public long toMinutes(long d) { return d/(C4/C3); }
82     public long toHours(long d) { return d/(C5/C3); }
83     public long toDays(long d) { return d/(C6/C3); }
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) {
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)); }
91     public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
92     public long toMinutes(long d) { return d; }
93     public long toHours(long d) { return d/(C5/C4); }
94     public long toDays(long d) { return d/(C6/C4); }
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) {
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)); }
102     public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
103     public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
104     public long toHours(long d) { return d; }
105     public long toDays(long d) { return d/(C6/C5); }
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) {
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)); }
113     public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
114     public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
115     public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); }
116     public long toDays(long d) { return d; }
117     public long convert(long d, TimeUnit u) { return u.toDays(d); }
118     int excessNanos(long d, long m) { return 0; }
119     };
120 jsr166 1.29
121 dl 1.27 /**
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 jsr166 1.29
128 dl 1.27 /** Internal constructor */
129     TimeUnit(int index) {
130     this.index = index;
131     }
132 jsr166 1.29
133 dl 1.27 // Handy constants for conversion methods
134     static final long C0 = 1L;
135     static final long C1 = C0 * 1000L;
136     static final long C2 = C1 * 1000L;
137     static final long C3 = C2 * 1000L;
138     static final long C4 = C3 * 60L;
139     static final long C5 = C4 * 60L;
140     static final long C6 = C5 * 24L;
141 jsr166 1.29
142 dl 1.27 static final long MAX = Long.MAX_VALUE;
143 jsr166 1.29
144 dl 1.27 /**
145     * Scale d by m, checking for overflow.
146     * This has a short name to make above code more readable.
147     */
148     static long x(long d, long m, long over) {
149     if (d > over) return Long.MAX_VALUE;
150     if (d < -over) return Long.MIN_VALUE;
151     return d * m;
152     }
153 jsr166 1.29
154 dl 1.32 // To maintain full signature compatibility with 1.5, method
155     // convert etc are not declared abstract but otherwise act as
156     // abstract methods.
157    
158 dl 1.27 /**
159     * Convert the given time duration in the given unit to this
160     * unit. Conversions from finer to coarser granularities
161     * truncate, so lose precision. For example converting
162     * <tt>999</tt> milliseconds to seconds results in
163     * <tt>0</tt>. Conversions from coarser to finer granularities
164     * with arguments that would numerically overflow saturate to
165     * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
166     * if positive.
167 dl 1.32 * <p>
168     * For example, to convert 10 minutes to milliseconds, use:
169     * <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
170 dl 1.27 *
171 dl 1.32 * @param sourceDuration the time duration in the given <tt>unit</tt>
172     * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
173 dl 1.27 * @return the converted duration in this unit,
174     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
175     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
176     */
177 dl 1.32 public long convert(long sourceDuration, TimeUnit sourceUnit) {
178     throw new AbstractMethodError();
179     }
180 jsr166 1.29
181 dl 1.27 /**
182     * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
183     * @param duration the duration
184     * @return the converted duration,
185     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
186     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
187     * @see #convert
188     */
189 dl 1.32 public long toNanos(long duration) {
190     throw new AbstractMethodError();
191     }
192 jsr166 1.29
193 dl 1.27 /**
194     * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
195     * @param duration the duration
196     * @return the converted duration,
197     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
198     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
199     * @see #convert
200     */
201 dl 1.32 public long toMicros(long duration) {
202     throw new AbstractMethodError();
203     }
204 jsr166 1.29
205 dl 1.27 /**
206     * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
207     * @param duration the duration
208     * @return the converted duration,
209     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
210     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
211     * @see #convert
212     */
213 dl 1.32 public long toMillis(long duration) {
214     throw new AbstractMethodError();
215     }
216 jsr166 1.29
217 dl 1.27 /**
218     * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
219     * @param duration the duration
220     * @return the converted duration,
221     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
222     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
223     * @see #convert
224     */
225 dl 1.32 public long toSeconds(long duration) {
226     throw new AbstractMethodError();
227     }
228 jsr166 1.29
229 dl 1.27 /**
230     * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
231     * @param duration the duration
232     * @return the converted duration,
233     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
234     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
235     * @see #convert
236 dl 1.31 * @since 1.6
237 dl 1.27 */
238 dl 1.32 public long toMinutes(long duration) {
239     throw new AbstractMethodError();
240     }
241 jsr166 1.29
242 dl 1.27 /**
243     * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
244     * @param duration the duration
245     * @return the converted duration,
246     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
247     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
248     * @see #convert
249 dl 1.31 * @since 1.6
250 dl 1.27 */
251 dl 1.32 public long toHours(long duration) {
252     throw new AbstractMethodError();
253     }
254 jsr166 1.29
255 dl 1.27 /**
256     * Equivalent to <tt>DAYS.convert(duration, this)</tt>.
257     * @param duration the duration
258     * @return the converted duration
259     * @see #convert
260 dl 1.31 * @since 1.6
261 dl 1.27 */
262 dl 1.32 public long toDays(long duration) {
263     throw new AbstractMethodError();
264     }
265 jsr166 1.29
266 dl 1.27 /**
267     * Utility to compute the excess-nanosecond argument to wait,
268     * sleep, join.
269     * @param d the duration
270     * @param m the number of milliseconds
271     * @return the number of nanoseconds
272     */
273     abstract int excessNanos(long d, long m);
274 jsr166 1.29
275 dl 1.27 /**
276 jsr166 1.29 * Performs a timed <tt>Object.wait</tt> using this time unit.
277 dl 1.27 * This is a convenience method that converts timeout arguments
278     * into the form required by the <tt>Object.wait</tt> method.
279     *
280     * <p>For example, you could implement a blocking <tt>poll</tt>
281     * method (see {@link BlockingQueue#poll BlockingQueue.poll})
282     * using:
283     *
284 jsr166 1.30 * <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
285 dl 1.27 * while (empty) {
286     * unit.timedWait(this, timeout);
287     * ...
288     * }
289     * }</pre>
290     *
291     * @param obj the object to wait on
292 dl 1.28 * @param timeout the maximum time to wait. If less than
293     * or equal to zero, do not wait at all.
294 dl 1.27 * @throws InterruptedException if interrupted while waiting.
295     * @see Object#wait(long, int)
296     */
297     public void timedWait(Object obj, long timeout)
298     throws InterruptedException {
299     if (timeout > 0) {
300     long ms = toMillis(timeout);
301     int ns = excessNanos(timeout, ms);
302     obj.wait(ms, ns);
303     }
304     }
305 jsr166 1.29
306 dl 1.27 /**
307 jsr166 1.29 * Performs a timed <tt>Thread.join</tt> using this time unit.
308 dl 1.27 * This is a convenience method that converts time arguments into the
309     * form required by the <tt>Thread.join</tt> method.
310     * @param thread the thread to wait for
311 dl 1.28 * @param timeout the maximum time to wait. If less than
312     * or equal to zero, do not wait at all.
313 dl 1.27 * @throws InterruptedException if interrupted while waiting.
314     * @see Thread#join(long, int)
315     */
316     public void timedJoin(Thread thread, long timeout)
317     throws InterruptedException {
318     if (timeout > 0) {
319     long ms = toMillis(timeout);
320     int ns = excessNanos(timeout, ms);
321     thread.join(ms, ns);
322     }
323     }
324 jsr166 1.29
325 dl 1.27 /**
326 jsr166 1.29 * Performs a <tt>Thread.sleep</tt> using this unit.
327 dl 1.27 * This is a convenience method that converts time arguments into the
328     * form required by the <tt>Thread.sleep</tt> method.
329 dl 1.28 * @param timeout the minimum time to sleep. If less than
330     * or equal to zero, do not sleep at all.
331 dl 1.27 * @throws InterruptedException if interrupted while sleeping.
332     * @see Thread#sleep
333     */
334     public void sleep(long timeout) throws InterruptedException {
335     if (timeout > 0) {
336     long ms = toMillis(timeout);
337     int ns = excessNanos(timeout, ms);
338     Thread.sleep(ms, ns);
339     }
340     }
341 jsr166 1.29
342 tim 1.1 }