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

Add TimeUnit#convert(Duration)

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P4
    • 11
    • core-libs
    • None
    • minimal
    • New method added to enum type.
    • Java API
    • 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

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

              Dates

                Created:
                Updated:
                Resolved: