15 |
|
* use time representations that may be maintained separately across |
16 |
|
* various contexts. |
17 |
|
* |
18 |
< |
* <p>The <tt>TimeUnit</tt> class cannot be directly instantiated. |
19 |
< |
* Use the {@link #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, |
20 |
< |
* and {@link #NANOSECONDS} static instances that provide predefined |
21 |
< |
* units of precision. If you use these frequently, consider |
22 |
< |
* statically importing this class. |
18 |
> |
* <p>This class cannot be directly instantiated. Use the {@link |
19 |
> |
* #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, and {@link |
20 |
> |
* #NANOSECONDS} static instances that provide predefined units of |
21 |
> |
* precision. If you use these frequently, consider statically |
22 |
> |
* importing this class. |
23 |
|
* |
24 |
|
* <p>A <tt>TimeUnit</tt> is mainly used to inform blocking methods which |
25 |
|
* can timeout, how the timeout parameter should be interpreted. For example, |
38 |
|
* as the given <tt>TimeUnit</tt>. |
39 |
|
* |
40 |
|
* @since 1.5 |
41 |
– |
* @spec JSR-166 |
42 |
– |
* @revised $Date$ |
43 |
– |
* @editor $Author$ |
41 |
|
* @author Doug Lea |
42 |
|
*/ |
43 |
|
public final class TimeUnit implements java.io.Serializable { |
57 |
|
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
58 |
|
*/ |
59 |
|
public long convert(long duration, TimeUnit unit) { |
60 |
< |
if (unit == this) |
60 |
> |
int i = unit.index - index; |
61 |
> |
if (i == 0) |
62 |
|
return duration; |
63 |
< |
else if (index > unit.index) |
64 |
< |
return duration / multipliers[index - unit.index]; |
65 |
< |
else { |
66 |
< |
int i = unit.index - index; |
67 |
< |
if (duration > overflows[i]) |
68 |
< |
return Long.MAX_VALUE; |
69 |
< |
if (duration < -overflows[i]) |
72 |
< |
return Long.MIN_VALUE; |
73 |
< |
return duration * multipliers[i]; |
74 |
< |
} |
63 |
> |
if (i < 0) |
64 |
> |
return duration / multipliers[-i]; |
65 |
> |
if (duration > overflows[i]) |
66 |
> |
return Long.MAX_VALUE; |
67 |
> |
if (duration < -overflows[i]) |
68 |
> |
return Long.MIN_VALUE; |
69 |
> |
return duration * multipliers[i]; |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* Equivalent to <code>NANOSECONDS.convert(duration, this)</code>. |
73 |
> |
* Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>. |
74 |
|
* @param duration the duration |
75 |
|
* @return the converted duration. |
76 |
|
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |