46 |
|
/** |
47 |
|
* Time unit representing one thousandth of a microsecond. |
48 |
|
*/ |
49 |
< |
NANOSECONDS(1L), // (cannot use symbolic scale names here) |
49 |
> |
NANOSECONDS(TimeUnit.NANO_SCALE), |
50 |
|
/** |
51 |
|
* Time unit representing one thousandth of a millisecond. |
52 |
|
*/ |
53 |
< |
MICROSECONDS(1000L), |
53 |
> |
MICROSECONDS(TimeUnit.MICRO_SCALE), |
54 |
|
/** |
55 |
|
* Time unit representing one thousandth of a second. |
56 |
|
*/ |
57 |
< |
MILLISECONDS(1000L * 1000L), |
57 |
> |
MILLISECONDS(TimeUnit.MILLI_SCALE), |
58 |
|
/** |
59 |
|
* Time unit representing one second. |
60 |
|
*/ |
61 |
< |
SECONDS(1000L * 1000L * 1000L), |
61 |
> |
SECONDS(TimeUnit.SECOND_SCALE), |
62 |
|
/** |
63 |
|
* Time unit representing sixty seconds. |
64 |
|
* @since 1.6 |
65 |
|
*/ |
66 |
< |
MINUTES(1000L * 1000L * 1000L * 60L), |
66 |
> |
MINUTES(TimeUnit.MINUTE_SCALE), |
67 |
|
/** |
68 |
|
* Time unit representing sixty minutes. |
69 |
|
* @since 1.6 |
70 |
|
*/ |
71 |
< |
HOURS(1000L * 1000L * 1000L * 60L * 60L), |
71 |
> |
HOURS(TimeUnit.HOUR_SCALE), |
72 |
|
/** |
73 |
|
* Time unit representing twenty four hours. |
74 |
|
* @since 1.6 |
75 |
|
*/ |
76 |
< |
DAYS(1000L * 1000L * 1000L * 60L * 60L * 24L); |
76 |
> |
DAYS(TimeUnit.DAY_SCALE); |
77 |
|
|
78 |
|
// Scales as constants |
79 |
|
private static final long NANO_SCALE = 1L; |
86 |
|
|
87 |
|
/* |
88 |
|
* Instances cache conversion ratios and saturation cutoffs for |
89 |
< |
* the units most commonly used in conversion results, in methods |
90 |
< |
* toNanos, toMillis and toSeconds. Other cases compute them, in |
89 |
> |
* the units up through SECONDS. Other cases compute them, in |
90 |
|
* method cvt. |
91 |
|
*/ |
92 |
|
|
93 |
|
private final long scale; |
94 |
|
private final long maxNanos; |
95 |
+ |
private final long maxMicros; |
96 |
|
private final long maxMillis; |
97 |
|
private final long maxSecs; |
98 |
< |
private final int milliRatio; |
99 |
< |
private final int secRatio; |
100 |
< |
|
101 |
< |
TimeUnit(long scale) { |
102 |
< |
this.scale = scale; |
103 |
< |
this.maxNanos = Long.MAX_VALUE / scale; |
104 |
< |
long mr = (scale >= MILLI_SCALE) |
105 |
< |
? (scale / MILLI_SCALE) |
106 |
< |
: (MILLI_SCALE / scale); |
98 |
> |
private final long microRatio; |
99 |
> |
private final int milliRatio; // fits in 32 bits |
100 |
> |
private final int secRatio; // fits in 32 bits |
101 |
> |
|
102 |
> |
TimeUnit(long s) { |
103 |
> |
this.scale = s; |
104 |
> |
this.maxNanos = Long.MAX_VALUE / s; |
105 |
> |
long ur = (s >= MICRO_SCALE) ? (s / MICRO_SCALE) : (MICRO_SCALE / s); |
106 |
> |
this.microRatio = ur; |
107 |
> |
this.maxMicros = Long.MAX_VALUE / ur; |
108 |
> |
long mr = (s >= MILLI_SCALE) ? (s / MILLI_SCALE) : (MILLI_SCALE / s); |
109 |
|
this.milliRatio = (int)mr; |
110 |
|
this.maxMillis = Long.MAX_VALUE / mr; |
111 |
< |
long sr = (scale >= SECOND_SCALE) |
110 |
< |
? (scale / SECOND_SCALE) |
111 |
< |
: (SECOND_SCALE / scale); |
111 |
> |
long sr = (s >= SECOND_SCALE) ? (s / SECOND_SCALE) : (SECOND_SCALE / s); |
112 |
|
this.secRatio = (int)sr; |
113 |
|
this.maxSecs = Long.MAX_VALUE / sr; |
114 |
|
} |
117 |
|
* General conversion utility. |
118 |
|
* |
119 |
|
* @param d duration |
120 |
< |
* @param dst result scale unit |
121 |
< |
* @param src source scale unit |
120 |
> |
* @param dst result unit scale |
121 |
> |
* @param src source unit scale |
122 |
|
*/ |
123 |
|
private static long cvt(long d, long dst, long src) { |
124 |
|
long r, m; |
149 |
|
* @param sourceDuration the time duration in the given {@code sourceUnit} |
150 |
|
* @param sourceUnit the unit of the {@code sourceDuration} argument |
151 |
|
* @return the converted duration in this unit, |
152 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
153 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
152 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
153 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
154 |
|
*/ |
155 |
|
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
156 |
< |
return cvt(sourceDuration, scale, sourceUnit.scale); |
156 |
> |
switch (this) { |
157 |
> |
case NANOSECONDS: return sourceUnit.toNanos(sourceDuration); |
158 |
> |
case MICROSECONDS: return sourceUnit.toMicros(sourceDuration); |
159 |
> |
case MILLISECONDS: return sourceUnit.toMillis(sourceDuration); |
160 |
> |
case SECONDS: return sourceUnit.toSeconds(sourceDuration); |
161 |
> |
default: return cvt(sourceDuration, scale, sourceUnit.scale); |
162 |
> |
} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
167 |
|
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
168 |
|
* @param duration the duration |
169 |
|
* @return the converted duration, |
170 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
171 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
170 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
171 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
172 |
|
*/ |
173 |
|
public long toNanos(long duration) { |
174 |
|
long s, m; |
187 |
|
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}. |
188 |
|
* @param duration the duration |
189 |
|
* @return the converted duration, |
190 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
191 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
190 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
191 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
192 |
|
*/ |
193 |
|
public long toMicros(long duration) { |
194 |
< |
return cvt(duration, MICRO_SCALE, scale); |
194 |
> |
long s, m; |
195 |
> |
if ((s = scale) == MICRO_SCALE) |
196 |
> |
return duration; |
197 |
> |
else if (s < MICRO_SCALE) |
198 |
> |
return duration / microRatio; |
199 |
> |
else if (duration > (m = maxMicros)) |
200 |
> |
return Long.MAX_VALUE; |
201 |
> |
else if (duration < -m) |
202 |
> |
return Long.MIN_VALUE; |
203 |
> |
else |
204 |
> |
return duration * microRatio; |
205 |
|
} |
206 |
|
|
207 |
|
/** |
209 |
|
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}. |
210 |
|
* @param duration the duration |
211 |
|
* @return the converted duration, |
212 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
213 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
212 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
213 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
214 |
|
*/ |
215 |
|
public long toMillis(long duration) { |
216 |
|
long s, m; |
231 |
|
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}. |
232 |
|
* @param duration the duration |
233 |
|
* @return the converted duration, |
234 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
235 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
234 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
235 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
236 |
|
*/ |
237 |
|
public long toSeconds(long duration) { |
238 |
|
long s, m; |
253 |
|
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}. |
254 |
|
* @param duration the duration |
255 |
|
* @return the converted duration, |
256 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
257 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
256 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
257 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
258 |
|
* @since 1.6 |
259 |
|
*/ |
260 |
|
public long toMinutes(long duration) { |
266 |
|
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}. |
267 |
|
* @param duration the duration |
268 |
|
* @return the converted duration, |
269 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
270 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
269 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
270 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
271 |
|
* @since 1.6 |
272 |
|
*/ |
273 |
|
public long toHours(long duration) { |