src/java.base/share/classes/java/util/concurrent/TimeUnit.java
changeset 50477 cb0efe0cc20e
parent 47307 6864969a78ad
child 50764 5637aca18f1d
equal deleted inserted replaced
50476:30d5bca69eae 50477:cb0efe0cc20e
    33  * http://creativecommons.org/publicdomain/zero/1.0/
    33  * http://creativecommons.org/publicdomain/zero/1.0/
    34  */
    34  */
    35 
    35 
    36 package java.util.concurrent;
    36 package java.util.concurrent;
    37 
    37 
       
    38 import java.time.Duration;
    38 import java.time.temporal.ChronoUnit;
    39 import java.time.temporal.ChronoUnit;
    39 import java.util.Objects;
    40 import java.util.Objects;
    40 
    41 
    41 /**
    42 /**
    42  * A {@code TimeUnit} represents time durations at a given unit of
    43  * A {@code TimeUnit} represents time durations at a given unit of
   190         default: return cvt(sourceDuration, scale, sourceUnit.scale);
   191         default: return cvt(sourceDuration, scale, sourceUnit.scale);
   191         }
   192         }
   192     }
   193     }
   193 
   194 
   194     /**
   195     /**
       
   196      * Converts the given time duration to this unit.
       
   197      *
       
   198      * <p>For any TimeUnit {@code unit},
       
   199      * {@code unit.convert(Duration.ofNanos(n))}
       
   200      * is equivalent to
       
   201      * {@code unit.convert(n, NANOSECONDS)}, and
       
   202      * {@code unit.convert(Duration.of(n, unit.toChronoUnit()))}
       
   203      * is equivalent to {@code n} (in the absence of overflow).
       
   204      *
       
   205      * @param duration the time duration
       
   206      * @return the converted duration in this unit,
       
   207      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
       
   208      * or {@code Long.MAX_VALUE} if it would positively overflow.
       
   209      * @throws NullPointerException if {@code duration} is null
       
   210      * @see Duration#of(long,TemporalUnit)
       
   211      * @since 11
       
   212      */
       
   213     public long convert(Duration duration) {
       
   214         long secs = duration.getSeconds();
       
   215         int nano = duration.getNano();
       
   216         if (secs < 0 && nano > 0) {
       
   217             // use representation compatible with integer division
       
   218             secs++;
       
   219             nano -= SECOND_SCALE;
       
   220         }
       
   221         final long s, nanoVal;
       
   222         // Optimize for the common case - NANOSECONDS without overflow
       
   223         if (this == NANOSECONDS)
       
   224             nanoVal = nano;
       
   225         else if ((s = scale) < SECOND_SCALE)
       
   226             nanoVal = nano / s;
       
   227         else if (this == SECONDS)
       
   228             return secs;
       
   229         else
       
   230             return secs / secRatio;
       
   231         long val = secs * secRatio + nanoVal;
       
   232         return ((secs < maxSecs && secs > -maxSecs) ||
       
   233                 (secs == maxSecs && val > 0) ||
       
   234                 (secs == -maxSecs && val < 0))
       
   235             ? val
       
   236             : (secs > 0) ? Long.MAX_VALUE : Long.MIN_VALUE;
       
   237     }
       
   238 
       
   239     /**
   195      * Equivalent to
   240      * Equivalent to
   196      * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
   241      * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
   197      * @param duration the duration
   242      * @param duration the duration
   198      * @return the converted duration,
   243      * @return the converted duration,
   199      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   244      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   219      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   264      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   220      * or {@code Long.MAX_VALUE} if it would positively overflow.
   265      * or {@code Long.MAX_VALUE} if it would positively overflow.
   221      */
   266      */
   222     public long toMicros(long duration) {
   267     public long toMicros(long duration) {
   223         long s, m;
   268         long s, m;
   224         if ((s = scale) == MICRO_SCALE)
   269         if ((s = scale) <= MICRO_SCALE)
   225             return duration;
   270             return (s == MICRO_SCALE) ? duration : duration / microRatio;
   226         else if (s < MICRO_SCALE)
       
   227             return duration / microRatio;
       
   228         else if (duration > (m = maxMicros))
   271         else if (duration > (m = maxMicros))
   229             return Long.MAX_VALUE;
   272             return Long.MAX_VALUE;
   230         else if (duration < -m)
   273         else if (duration < -m)
   231             return Long.MIN_VALUE;
   274             return Long.MIN_VALUE;
   232         else
   275         else
   241      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   284      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   242      * or {@code Long.MAX_VALUE} if it would positively overflow.
   285      * or {@code Long.MAX_VALUE} if it would positively overflow.
   243      */
   286      */
   244     public long toMillis(long duration) {
   287     public long toMillis(long duration) {
   245         long s, m;
   288         long s, m;
   246         if ((s = scale) == MILLI_SCALE)
   289         if ((s = scale) <= MILLI_SCALE)
   247             return duration;
   290             return (s == MILLI_SCALE) ? duration : duration / milliRatio;
   248         else if (s < MILLI_SCALE)
       
   249             return duration / milliRatio;
       
   250         else if (duration > (m = maxMillis))
   291         else if (duration > (m = maxMillis))
   251             return Long.MAX_VALUE;
   292             return Long.MAX_VALUE;
   252         else if (duration < -m)
   293         else if (duration < -m)
   253             return Long.MIN_VALUE;
   294             return Long.MIN_VALUE;
   254         else
   295         else
   263      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   304      * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
   264      * or {@code Long.MAX_VALUE} if it would positively overflow.
   305      * or {@code Long.MAX_VALUE} if it would positively overflow.
   265      */
   306      */
   266     public long toSeconds(long duration) {
   307     public long toSeconds(long duration) {
   267         long s, m;
   308         long s, m;
   268         if ((s = scale) == SECOND_SCALE)
   309         if ((s = scale) <= SECOND_SCALE)
   269             return duration;
   310             return (s == SECOND_SCALE) ? duration : duration / secRatio;
   270         else if (s < SECOND_SCALE)
       
   271             return duration / secRatio;
       
   272         else if (duration > (m = maxSecs))
   311         else if (duration > (m = maxSecs))
   273             return Long.MAX_VALUE;
   312             return Long.MAX_VALUE;
   274         else if (duration < -m)
   313         else if (duration < -m)
   275             return Long.MIN_VALUE;
   314             return Long.MIN_VALUE;
   276         else
   315         else