ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jdk8/java/util/concurrent/TimeUnit.java
(Generate patch)

Comparing jsr166/src/jdk8/java/util/concurrent/TimeUnit.java (file contents):
Revision 1.4 by jsr166, Tue Apr 5 01:02:44 2016 UTC vs.
Revision 1.5 by jsr166, Fri Jun 8 02:20:22 2018 UTC

# Line 6 | Line 6
6  
7   package java.util.concurrent;
8  
9 + import java.time.Duration;
10   import java.time.temporal.ChronoUnit;
11   import java.util.Objects;
12  
# Line 163 | Line 164 | public enum TimeUnit {
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
# Line 192 | Line 237 | public enum TimeUnit {
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)
# Line 214 | Line 257 | public enum TimeUnit {
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)
# Line 236 | Line 277 | public enum TimeUnit {
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)
# Line 308 | Line 347 | public enum TimeUnit {
347       * This is a convenience method that converts timeout arguments
348       * into the form required by the {@code Object.wait} method.
349       *
350 <     * <p>For example, you could implement a blocking {@code poll}
351 <     * method (see {@link BlockingQueue#poll BlockingQueue.poll})
350 >     * <p>For example, you could implement a blocking {@code poll} method
351 >     * (see {@link BlockingQueue#poll(long, TimeUnit) BlockingQueue.poll})
352       * using:
353       *
354       * <pre> {@code
355 <     * public synchronized Object poll(long timeout, TimeUnit unit)
355 >     * public E poll(long timeout, TimeUnit unit)
356       *     throws InterruptedException {
357 <     *   while (empty) {
358 <     *     unit.timedWait(this, timeout);
359 <     *     ...
357 >     *   synchronized (lock) {
358 >     *     while (isEmpty()) {
359 >     *       unit.timedWait(lock, timeout);
360 >     *       ...
361 >     *     }
362       *   }
363       * }}</pre>
364       *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines