-
CSR
-
Resolution: Approved
-
P4
-
None
-
minimal
-
New method added to enum type.
-
Java API
-
SE
Summary
Add TimeUnit#convert(Duration)
Problem
We'd like to encourage people to use java.time.Duration. Some organizations like Google are trying to encourage migrations from raw primitives to typed time quantities like Duration or Instant.
Solution
TimeUnit#convert(Duration) will allow calls into j.u.concurrent methods that take a (long, TimeUnit) parameter pair. Typically users will call as
(NANOSECONDS.convert(duration), NANOSECONDS)
Static analysis tools can check that the units agree when literals are used, as we expect.
Specification
/**
* Converts the given time duration to this unit.
*
* <p>For any TimeUnit {@code unit},
* {@code unit.convert(Duration.ofNanos(n))}
* is equivalent to
* {@code unit.convert(n, NANOSECONDS)}, and
* {@code unit.convert(Duration.of(n, unit.toChronoUnit()))}
* is equivalent to {@code n} (in the absence of overflow).
*
* @param duration the time duration
* @return the converted duration in this unit,
* or {@code Long.MIN_VALUE} if conversion would negatively overflow,
* or {@code Long.MAX_VALUE} if it would positively overflow.
* @throws NullPointerException if {@code duration} is null
* @see Duration#of(long,TemporalUnit)
* @since 11
*/
public long convert(Duration duration) {
- csr of
-
JDK-8204375 Add TimeUnit#convert(Duration)
-
- Resolved
-