6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
|
|
9 |
+ |
import java.time.Duration; |
10 |
|
import java.time.temporal.ChronoUnit; |
11 |
|
import java.util.Objects; |
12 |
|
|
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
+ |
* Converts the given time duration to this unit. |
168 |
+ |
* |
169 |
+ |
* <p>For any TimeUnit {@code unit}, |
170 |
+ |
* {@code unit.convert(Duration.ofNanos(n))} |
171 |
+ |
* is equivalent to |
172 |
+ |
* {@code unit.convert(n, NANOSECONDS)}, and |
173 |
+ |
* {@code unit.convert(Duration.of(n, unit.toChronoUnit()))} |
174 |
+ |
* is equivalent to {@code n} (in the absence of overflow). |
175 |
+ |
* |
176 |
+ |
* @param duration the time duration |
177 |
+ |
* @return the converted duration in this unit, |
178 |
+ |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
179 |
+ |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
180 |
+ |
* @throws NullPointerException if {@code duration} is null |
181 |
+ |
* @see Duration#of(long,TemporalUnit) |
182 |
+ |
* @since 11 |
183 |
+ |
*/ |
184 |
+ |
public long convert(Duration duration) { |
185 |
+ |
long secs = duration.getSeconds(); |
186 |
+ |
int nano = duration.getNano(); |
187 |
+ |
if (secs < 0 && nano > 0) { |
188 |
+ |
// use representation compatible with integer division |
189 |
+ |
secs++; |
190 |
+ |
nano -= SECOND_SCALE; |
191 |
+ |
} |
192 |
+ |
final long s, nanoVal; |
193 |
+ |
// Optimize for the common case - NANOSECONDS without overflow |
194 |
+ |
if (this == NANOSECONDS) |
195 |
+ |
nanoVal = nano; |
196 |
+ |
else if ((s = scale) < SECOND_SCALE) |
197 |
+ |
nanoVal = nano / s; |
198 |
+ |
else if (this == SECONDS) |
199 |
+ |
return secs; |
200 |
+ |
else |
201 |
+ |
return secs / secRatio; |
202 |
+ |
long val = secs * secRatio + nanoVal; |
203 |
+ |
return ((secs < maxSecs && secs > -maxSecs) || |
204 |
+ |
(secs == maxSecs && val > 0) || |
205 |
+ |
(secs == -maxSecs && val < 0)) |
206 |
+ |
? val |
207 |
+ |
: (secs > 0) ? Long.MAX_VALUE : Long.MIN_VALUE; |
208 |
+ |
} |
209 |
+ |
|
210 |
+ |
/** |
211 |
|
* Equivalent to |
212 |
|
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
213 |
|
* @param duration the duration |
237 |
|
*/ |
238 |
|
public long toMicros(long duration) { |
239 |
|
long s, m; |
240 |
< |
if ((s = scale) == MICRO_SCALE) |
241 |
< |
return duration; |
197 |
< |
else if (s < MICRO_SCALE) |
198 |
< |
return duration / microRatio; |
240 |
> |
if ((s = scale) <= MICRO_SCALE) |
241 |
> |
return (s == MICRO_SCALE) ? duration : duration / microRatio; |
242 |
|
else if (duration > (m = maxMicros)) |
243 |
|
return Long.MAX_VALUE; |
244 |
|
else if (duration < -m) |
257 |
|
*/ |
258 |
|
public long toMillis(long duration) { |
259 |
|
long s, m; |
260 |
< |
if ((s = scale) == MILLI_SCALE) |
261 |
< |
return duration; |
219 |
< |
else if (s < MILLI_SCALE) |
220 |
< |
return duration / milliRatio; |
260 |
> |
if ((s = scale) <= MILLI_SCALE) |
261 |
> |
return (s == MILLI_SCALE) ? duration : duration / milliRatio; |
262 |
|
else if (duration > (m = maxMillis)) |
263 |
|
return Long.MAX_VALUE; |
264 |
|
else if (duration < -m) |
277 |
|
*/ |
278 |
|
public long toSeconds(long duration) { |
279 |
|
long s, m; |
280 |
< |
if ((s = scale) == SECOND_SCALE) |
281 |
< |
return duration; |
241 |
< |
else if (s < SECOND_SCALE) |
242 |
< |
return duration / secRatio; |
280 |
> |
if ((s = scale) <= SECOND_SCALE) |
281 |
> |
return (s == SECOND_SCALE) ? duration : duration / secRatio; |
282 |
|
else if (duration > (m = maxSecs)) |
283 |
|
return Long.MAX_VALUE; |
284 |
|
else if (duration < -m) |