File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\TimeUnit2.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 /*
25 * Written by Doug Lea with assistance from members of JCP JSR-166
26 * Expert Group and released to the public domain, as explained at
27 * http://creativecommons.org/licenses/publicdomain
28 */
29
30 package hudson.util;
31
32 import java.util.concurrent.TimeUnit;
33
34 /**
35 * A <tt>TimeUnit</tt> represents time durations at a given unit of
36 * granularity and provides utility methods to convert across units,
37 * and to perform timing and delay operations in these units. A
38 * <tt>TimeUnit</tt> does not maintain time information, but only
39 * helps organize and use time representations that may be maintained
40 * separately across various contexts. A nanosecond is defined as one
41 * thousandth of a microsecond, a microsecond as one thousandth of a
42 * millisecond, a millisecond as one thousandth of a second, a minute
43 * as sixty seconds, an hour as sixty minutes, and a day as twenty four
44 * hours.
45 *
46 * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
47 * how a given timing parameter should be interpreted. For example,
48 * the following code will timeout in 50 milliseconds if the {@link
49 * java.util.concurrent.locks.Lock lock} is not available:
50 *
51 * <pre> Lock lock = ...;
52 * if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
53 * </pre>
54 * while this code will timeout in 50 seconds:
55 * <pre>
56 * Lock lock = ...;
57 * if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
58 * </pre>
59 *
60 * Note however, that there is no guarantee that a particular timeout
61 * implementation will be able to notice the passage of time at the
62 * same granularity as the given <tt>TimeUnit</tt>.
63 *
64 * @since 1.5
65 * @author Doug Lea
66 */
67 public enum TimeUnit2 {
68 NANOSECONDS {
69 @Override public long toNanos(long d) { return d; }
70 @Override public long toMicros(long d) { return d/(C1/C0); }
71 @Override public long toMillis(long d) { return d/(C2/C0); }
72 @Override public long toSeconds(long d) { return d/(C3/C0); }
73 @Override public long toMinutes(long d) { return d/(C4/C0); }
74 @Override public long toHours(long d) { return d/(C5/C0); }
75 @Override public long toDays(long d) { return d/(C6/C0); }
76 @Override public long convert(long d, TimeUnit2 u) { return u.toNanos(d); }
77 @Override public long convert(long d, TimeUnit u) { return u.toNanos(d); }
78 int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
79 },
80 MICROSECONDS {
81 @Override public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); }
82 @Override public long toMicros(long d) { return d; }
83 @Override public long toMillis(long d) { return d/(C2/C1); }
84 @Override public long toSeconds(long d) { return d/(C3/C1); }
85 @Override public long toMinutes(long d) { return d/(C4/C1); }
86 @Override public long toHours(long d) { return d/(C5/C1); }
87 @Override public long toDays(long d) { return d/(C6/C1); }
88 @Override public long convert(long d, TimeUnit2 u) { return u.toMicros(d); }
89 @Override public long convert(long d, TimeUnit u) { return u.toMicros(d); }
90 int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
91 },
92 MILLISECONDS {
93 @Override public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); }
94 @Override public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); }
95 @Override public long toMillis(long d) { return d; }
96 @Override public long toSeconds(long d) { return d/(C3/C2); }
97 @Override public long toMinutes(long d) { return d/(C4/C2); }
98 @Override public long toHours(long d) { return d/(C5/C2); }
99 @Override public long toDays(long d) { return d/(C6/C2); }
100 @Override public long convert(long d, TimeUnit2 u) { return u.toMillis(d); }
101 @Override public long convert(long d, TimeUnit u) { return u.toMillis(d); }
102 int excessNanos(long d, long m) { return 0; }
103 },
104 SECONDS {
105 @Override public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); }
106 @Override public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); }
107 @Override public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); }
108 @Override public long toSeconds(long d) { return d; }
109 @Override public long toMinutes(long d) { return d/(C4/C3); }
110 @Override public long toHours(long d) { return d/(C5/C3); }
111 @Override public long toDays(long d) { return d/(C6/C3); }
112 @Override public long convert(long d, TimeUnit2 u) { return u.toSeconds(d); }
113 @Override public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
114 int excessNanos(long d, long m) { return 0; }
115 },
116 MINUTES {
117 @Override public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); }
118 @Override public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); }
119 @Override public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); }
120 @Override public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
121 @Override public long toMinutes(long d) { return d; }
122 @Override public long toHours(long d) { return d/(C5/C4); }
123 @Override public long toDays(long d) { return d/(C6/C4); }
124 @Override public long convert(long d, TimeUnit2 u) { return u.toMinutes(d); }
125 @Override public long convert(long d, TimeUnit u) { return SECONDS.toMinutes(u.toSeconds(d)); }
126 int excessNanos(long d, long m) { return 0; }
127 },
128 HOURS {
129 @Override public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); }
130 @Override public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); }
131 @Override public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); }
132 @Override public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
133 @Override public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
134 @Override public long toHours(long d) { return d; }
135 @Override public long toDays(long d) { return d/(C6/C5); }
136 @Override public long convert(long d, TimeUnit2 u) { return u.toHours(d); }
137 @Override public long convert(long d, TimeUnit u) { return SECONDS.toHours(u.toSeconds(d)); }
138 int excessNanos(long d, long m) { return 0; }
139 },
140 DAYS {
141 @Override public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); }
142 @Override public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); }
143 @Override public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); }
144 @Override public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
145 @Override public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
146 @Override public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); }
147 @Override public long toDays(long d) { return d; }
148 @Override public long convert(long d, TimeUnit2 u) { return u.toDays(d); }
149 @Override public long convert(long d, TimeUnit u) { return SECONDS.toDays(u.toSeconds(d)); }
150 int excessNanos(long d, long m) { return 0; }
151 };
152
153 // Handy constants for conversion methods
154 static final long C0 = 1L;
155 static final long C1 = C0 * 1000L;
156 static final long C2 = C1 * 1000L;
157 static final long C3 = C2 * 1000L;
158 static final long C4 = C3 * 60L;
159 static final long C5 = C4 * 60L;
160 static final long C6 = C5 * 24L;
161
162 static final long MAX = Long.MAX_VALUE;
163
164 /**
165 * Scale d by m, checking for overflow.
166 * This has a short name to make above code more readable.
167 */
168 static long x(long d, long m, long over) {
169 if (d > over) return Long.MAX_VALUE;
170 if (d < -over) return Long.MIN_VALUE;
171 return d * m;
172 }
173
174 // To maintain full signature compatibility with 1.5, and to improve the
175 // clarity of the generated javadoc (see 6287639: Abstract methods in
176 // enum classes should not be listed as abstract), method convert
177 // etc. are not declared abstract but otherwise act as abstract methods.
178
179 /**
180 * Convert the given time duration in the given unit to this
181 * unit. Conversions from finer to coarser granularities
182 * truncate, so lose precision. For example converting
183 * <tt>999</tt> milliseconds to seconds results in
184 * <tt>0</tt>. Conversions from coarser to finer granularities
185 * with arguments that would numerically overflow saturate to
186 * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
187 * if positive.
188 *
189 * <p>For example, to convert 10 minutes to milliseconds, use:
190 * <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
191 *
192 * @param sourceDuration the time duration in the given <tt>sourceUnit</tt>
193 * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
194 * @return the converted duration in this unit,
195 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
196 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
197 */
198 public long convert(long sourceDuration, TimeUnit2 sourceUnit) {
199 throw new AbstractMethodError();
200 }
201
202 /**
203 * Convert the given time duration in the given unit to this
204 * unit. Conversions from finer to coarser granularities
205 * truncate, so lose precision. For example converting
206 * <tt>999</tt> milliseconds to seconds results in
207 * <tt>0</tt>. Conversions from coarser to finer granularities
208 * with arguments that would numerically overflow saturate to
209 * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
210 * if positive.
211 *
212 * <p>For example, to convert 10 minutes to milliseconds, use:
213 * <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
214 *
215 * @param sourceDuration the time duration in the given <tt>sourceUnit</tt>
216 * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
217 * @return the converted duration in this unit,
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 */
221 public long convert(long sourceDuration, TimeUnit sourceUnit) {
222 throw new AbstractMethodError();
223 }
224
225 /**
226 * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
227 * @param duration the duration
228 * @return the converted duration,
229 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
230 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
231 * @see #convert
232 */
233 public long toNanos(long duration) {
234 throw new AbstractMethodError();
235 }
236
237 /**
238 * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
239 * @param duration the duration
240 * @return the converted duration,
241 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
242 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
243 * @see #convert
244 */
245 public long toMicros(long duration) {
246 throw new AbstractMethodError();
247 }
248
249 /**
250 * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
251 * @param duration the duration
252 * @return the converted duration,
253 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
254 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
255 * @see #convert
256 */
257 public long toMillis(long duration) {
258 throw new AbstractMethodError();
259 }
260
261 /**
262 * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
263 * @param duration the duration
264 * @return the converted duration,
265 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
266 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
267 * @see #convert
268 */
269 public long toSeconds(long duration) {
270 throw new AbstractMethodError();
271 }
272
273 /**
274 * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
275 * @param duration the duration
276 * @return the converted duration,
277 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
278 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
279 * @see #convert
280 * @since 1.6
281 */
282 public long toMinutes(long duration) {
283 throw new AbstractMethodError();
284 }
285
286 /**
287 * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
288 * @param duration the duration
289 * @return the converted duration,
290 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
291 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
292 * @see #convert
293 * @since 1.6
294 */
295 public long toHours(long duration) {
296 throw new AbstractMethodError();
297 }
298
299 /**
300 * Equivalent to <tt>DAYS.convert(duration, this)</tt>.
301 * @param duration the duration
302 * @return the converted duration
303 * @see #convert
304 * @since 1.6
305 */
306 public long toDays(long duration) {
307 throw new AbstractMethodError();
308 }
309
310 /**
311 * Utility to compute the excess-nanosecond argument to wait,
312 * sleep, join.
313 * @param d the duration
314 * @param m the number of milliseconds
315 * @return the number of nanoseconds
316 */
317 abstract int excessNanos(long d, long m);
318
319 /**
320 * Performs a timed <tt>Object.wait</tt> using this time unit.
321 * This is a convenience method that converts timeout arguments
322 * into the form required by the <tt>Object.wait</tt> method.
323 *
324 * <p>For example, you could implement a blocking <tt>poll</tt>
325 * method (see {@link java.util.concurrent.BlockingQueue#poll BlockingQueue.poll})
326 * using:
327 *
328 * <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
329 * while (empty) {
330 * unit.timedWait(this, timeout);
331 * ...
332 * }
333 * }</pre>
334 *
335 * @param obj the object to wait on
336 * @param timeout the maximum time to wait. If less than
337 * or equal to zero, do not wait at all.
338 * @throws InterruptedException if interrupted while waiting.
339 * @see Object#wait(long, int)
340 */
341 public void timedWait(Object obj, long timeout)
342 throws InterruptedException {
343 if (timeout > 0) {
344 long ms = toMillis(timeout);
345 int ns = excessNanos(timeout, ms);
346 obj.wait(ms, ns);
347 }
348 }
349
350 /**
351 * Performs a timed <tt>Thread.join</tt> using this time unit.
352 * This is a convenience method that converts time arguments into the
353 * form required by the <tt>Thread.join</tt> method.
354 * @param thread the thread to wait for
355 * @param timeout the maximum time to wait. If less than
356 * or equal to zero, do not wait at all.
357 * @throws InterruptedException if interrupted while waiting.
358 * @see Thread#join(long, int)
359 */
360 public void timedJoin(Thread thread, long timeout)
361 throws InterruptedException {
362 if (timeout > 0) {
363 long ms = toMillis(timeout);
364 int ns = excessNanos(timeout, ms);
365 thread.join(ms, ns);
366 }
367 }
368
369 /**
370 * Performs a <tt>Thread.sleep</tt> using this unit.
371 * This is a convenience method that converts time arguments into the
372 * form required by the <tt>Thread.sleep</tt> method.
373 * @param timeout the minimum time to sleep. If less than
374 * or equal to zero, do not sleep at all.
375 * @throws InterruptedException if interrupted while sleeping.
376 * @see Thread#sleep
377 */
378 public void sleep(long timeout) throws InterruptedException {
379 if (timeout > 0) {
380 long ms = toMillis(timeout);
381 int ns = excessNanos(timeout, ms);
382 Thread.sleep(ms, ns);
383 }
384 }
385
386 }