KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > concurrent > TimeUnit


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   */

6
7 package org.apache.beehive.netui.util.internal.concurrent;
8
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. 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   * org.apache.beehive.netui.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 abstract class TimeUnit implements java.io.Serializable JavaDoc {
43
44      public static final TimeUnit NANOSECONDS = new TimeUnit(0, "NANOSECONDS") {
45          public long toNanos(long d) { return d; }
46          public long toMicros(long d) { return d/(C1/C0); }
47          public long toMillis(long d) { return d/(C2/C0); }
48          public long toSeconds(long d) { return d/(C3/C0); }
49          public long toMinutes(long d) { return d/(C4/C0); }
50          public long toHours(long d) { return d/(C5/C0); }
51          public long toDays(long d) { return d/(C6/C0); }
52          public long convert(long d, TimeUnit u) { return u.toNanos(d); }
53          int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
54      };
55      public static final TimeUnit MICROSECONDS = new TimeUnit(1, "MICROSECONDS") {
56          public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); }
57          public long toMicros(long d) { return d; }
58          public long toMillis(long d) { return d/(C2/C1); }
59          public long toSeconds(long d) { return d/(C3/C1); }
60          public long toMinutes(long d) { return d/(C4/C1); }
61          public long toHours(long d) { return d/(C5/C1); }
62          public long toDays(long d) { return d/(C6/C1); }
63          public long convert(long d, TimeUnit u) { return u.toMicros(d); }
64          int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
65      };
66      public static final TimeUnit MILLISECONDS = new TimeUnit(2, "MILLISECONDS") {
67          public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); }
68          public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); }
69          public long toMillis(long d) { return d; }
70          public long toSeconds(long d) { return d/(C3/C2); }
71          public long toMinutes(long d) { return d/(C4/C2); }
72          public long toHours(long d) { return d/(C5/C2); }
73          public long toDays(long d) { return d/(C6/C2); }
74          public long convert(long d, TimeUnit u) { return u.toMillis(d); }
75          int excessNanos(long d, long m) { return 0; }
76      };
77      public static final TimeUnit SECONDS = new TimeUnit(3, "SECONDS") {
78          public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); }
79          public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); }
80          public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); }
81          public long toSeconds(long d) { return d; }
82          public long toMinutes(long d) { return d/(C4/C3); }
83          public long toHours(long d) { return d/(C5/C3); }
84          public long toDays(long d) { return d/(C6/C3); }
85          public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
86          int excessNanos(long d, long m) { return 0; }
87      };
88      public static final TimeUnit MINUTES = new TimeUnit(4, "MINUTES") {
89          public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); }
90          public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); }
91          public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); }
92          public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
93          public long toMinutes(long d) { return d; }
94          public long toHours(long d) { return d/(C5/C4); }
95          public long toDays(long d) { return d/(C6/C4); }
96          public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
97          int excessNanos(long d, long m) { return 0; }
98      };
99      public static final TimeUnit HOURS = new TimeUnit(5, "HOURS") {
100          public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); }
101          public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); }
102          public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); }
103          public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
104          public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
105          public long toHours(long d) { return d; }
106          public long toDays(long d) { return d/(C6/C5); }
107          public long convert(long d, TimeUnit u) { return u.toHours(d); }
108          int excessNanos(long d, long m) { return 0; }
109      };
110      public static final TimeUnit DAYS = new TimeUnit(6, "DAYS") {
111          public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); }
112          public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); }
113          public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); }
114          public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
115          public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
116          public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); }
117          public long toDays(long d) { return d; }
118          public long convert(long d, TimeUnit u) { return u.toDays(d); }
119          int excessNanos(long d, long m) { return 0; }
120      };
121
122      private static final TimeUnit[] values = new TimeUnit[]
123          { NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS };
124
125      public static TimeUnit[] values() {
126          return (TimeUnit[])values.clone();
127      }
128
129      /**
130       * The index of this unit. This value is no longer used in this
131       * version of this class, but is retained for serialization
132       * compatibility with previous version.
133       */

134      private final int index;
135
136      /** name of this unit */
137      private final String JavaDoc name;
138
139      /** Internal constructor */
140      TimeUnit(int index, String JavaDoc name) {
141          this.index = index;
142          this.name = name;
143      }
144
145      // Handy constants for conversion methods
146
static final long C0 = 1;
147      static final long C1 = C0 * 1000;
148      static final long C2 = C1 * 1000;
149      static final long C3 = C2 * 1000;
150      static final long C4 = C3 * 60;
151      static final long C5 = C4 * 60;
152      static final long C6 = C5 * 24;
153
154      static final long MAX = Long.MAX_VALUE;
155
156      /**
157       * Scale d by m, checking for overflow.
158       * This has a short name to make above code more readable.
159       */

160      static long x(long d, long m, long over) {
161          if (d > over) return Long.MAX_VALUE;
162          if (d < -over) return Long.MIN_VALUE;
163          return d * m;
164      }
165
166      /**
167       * Convert the given time duration in the given unit to this
168       * unit. Conversions from finer to coarser granularities
169       * truncate, so lose precision. For example converting
170       * <tt>999</tt> milliseconds to seconds results in
171       * <tt>0</tt>. Conversions from coarser to finer granularities
172       * with arguments that would numerically overflow saturate to
173       * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
174       * if positive.
175       *
176       * @param duration the time duration in the given <tt>unit</tt>
177       * @param unit the unit of the <tt>duration</tt> argument
178       * @return the converted duration in this unit,
179       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
180       * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
181       */

182      public abstract long convert(long duration, TimeUnit unit);
183
184      /**
185       * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
186       * @param duration the duration
187       * @return the converted duration,
188       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
189       * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
190       * @see #convert
191       */

192      public abstract long toNanos(long duration);
193
194      /**
195       * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
196       * @param duration the duration
197       * @return the converted duration,
198       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
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
204      /**
205       * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
206       * @param duration the duration
207       * @return the converted duration,
208       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
209       * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
210       * @see #convert
211       */

212      public abstract long toMillis(long duration);
213
214      /**
215       * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
216       * @param duration the duration
217       * @return the converted duration,
218       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
219       * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
220       * @see #convert
221       */

222      public abstract long toSeconds(long duration);
223
224      /**
225       * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
226       * @param duration the duration
227       * @return the converted duration,
228       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
229       * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
230       * @see #convert
231       */

232      public abstract long toMinutes(long duration);
233
234      /**
235       * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
236       * @param duration the duration
237       * @return the converted duration,
238       * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
239       * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
240       * @see #convert
241       */

242      public abstract long toHours(long duration);
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       */

250      public abstract long toDays(long duration);
251
252      /**
253       * Utility to compute the excess-nanosecond argument to wait,
254       * sleep, join.
255       * @param d the duration
256       * @param m the number of millisecondss
257       * @return the number of nanoseconds
258       */

259      abstract int excessNanos(long d, long m);
260
261      /**
262       * Perform a timed <tt>Object.wait</tt> using this time unit.
263       * This is a convenience method that converts timeout arguments
264       * into the form required by the <tt>Object.wait</tt> method.
265       *
266       * <p>For example, you could implement a blocking <tt>poll</tt>
267       * method (see {@link BlockingQueue#poll BlockingQueue.poll})
268       * using:
269       *
270       * <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
271       * while (empty) {
272       * unit.timedWait(this, timeout);
273       * ...
274       * }
275       * }</pre>
276       *
277       * @param obj the object to wait on
278       * @param timeout the maximum time to wait.
279       * @throws InterruptedException if interrupted while waiting.
280       * @see java.lang.Object#wait(long, int)
281       */

282      public void timedWait(Object JavaDoc obj, long timeout)
283          throws InterruptedException JavaDoc {
284          if (timeout > 0) {
285              long ms = toMillis(timeout);
286              int ns = excessNanos(timeout, ms);
287              obj.wait(ms, ns);
288          }
289      }
290
291      /**
292       * Perform a timed <tt>Thread.join</tt> using this time unit.
293       * This is a convenience method that converts time arguments into the
294       * form required by the <tt>Thread.join</tt> method.
295       * @param thread the thread to wait for
296       * @param timeout the maximum time to wait
297       * @throws InterruptedException if interrupted while waiting.
298       * @see java.lang.Thread#join(long, int)
299       */

300      public void timedJoin(Thread JavaDoc thread, long timeout)
301          throws InterruptedException JavaDoc {
302          if (timeout > 0) {
303              long ms = toMillis(timeout);
304              int ns = excessNanos(timeout, ms);
305              thread.join(ms, ns);
306          }
307      }
308
309      /**
310       * Perform a <tt>Thread.sleep</tt> using this unit.
311       * This is a convenience method that converts time arguments into the
312       * form required by the <tt>Thread.sleep</tt> method.
313       * @param timeout the minimum time to sleep
314       * @throws InterruptedException if interrupted while sleeping.
315       * @see java.lang.Thread#sleep
316       */

317      public void sleep(long timeout) throws InterruptedException JavaDoc {
318          if (timeout > 0) {
319              long ms = toMillis(timeout);
320              int ns = excessNanos(timeout, ms);
321              Thread.sleep(ms, ns);
322          }
323      }
324
325      public String JavaDoc toString() {
326          return name;
327      }
328 }
329
Popular Tags