Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8204376

Add TimeUnit#convert(Duration)

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      New method added to enum type.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      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) {

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                martin Martin Buchholz
                Reporter:
                martin Martin Buchholz
                Reviewed By:
                David Holmes, Doug Lea, Roger Riggs
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: