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

Comparing jsr166/src/main/java/util/concurrent/TimeUnit.java (file contents):
Revision 1.51 by jsr166, Fri Mar 25 03:14:56 2016 UTC vs.
Revision 1.52 by dl, Fri Mar 25 16:16:02 2016 UTC

# Line 86 | Line 86 | public enum TimeUnit {
86  
87      /*
88       * Instances cache conversion ratios and saturation cutoffs for
89 <     * the two units commonly used for JDK timing, used in methods
90 <     * toNanos and toMillis. Other cases compute them, in method cvt.
89 >     * the units most commonly used in conversion results, in methods
90 >     * toNanos, toMillis and toSeconds. Other cases compute them, in
91 >     * method cvt.
92       */
93  
94      private final long scale;
95      private final long maxNanos;
95    private final long millisRatio;
96      private final long maxMillis;
97 +    private final long maxSecs;
98 +    private final int milliRatio;
99 +    private final int secRatio;
100  
101      TimeUnit(long scale) {
102          this.scale = scale;
103          this.maxNanos = Long.MAX_VALUE / scale;
104 <        long r = (scale >= MILLI_SCALE)
105 <            ? scale / MILLI_SCALE
106 <            : MILLI_SCALE / scale;
107 <        this.millisRatio = r;
108 <        this.maxMillis = Long.MAX_VALUE / r;
104 >        long mr = (scale >= MILLI_SCALE)
105 >            ? (scale / MILLI_SCALE)
106 >            : (MILLI_SCALE / scale);
107 >        this.milliRatio = (int)mr;
108 >        this.maxMillis = Long.MAX_VALUE / mr;
109 >        long sr = (scale >= SECOND_SCALE)
110 >            ? (scale / SECOND_SCALE)
111 >            : (SECOND_SCALE / scale);
112 >        this.secRatio = (int)sr;
113 >        this.maxSecs = Long.MAX_VALUE / sr;
114      }
115  
116      /**
# Line 193 | Line 201 | public enum TimeUnit {
201          if ((s = scale) == MILLI_SCALE)
202              return duration;
203          else if (s < MILLI_SCALE)
204 <            return duration / millisRatio;
204 >            return duration / milliRatio;
205          else if (duration > (m = maxMillis))
206              return Long.MAX_VALUE;
207          else if (duration < -m)
208              return Long.MIN_VALUE;
209          else
210 <            return duration * millisRatio;
210 >            return duration * milliRatio;
211      }
212  
213      /**
# Line 211 | Line 219 | public enum TimeUnit {
219       * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
220       */
221      public long toSeconds(long duration) {
222 <        return cvt(duration, SECOND_SCALE, scale);
222 >        long s, m;
223 >        if ((s = scale) == SECOND_SCALE)
224 >            return duration;
225 >        else if (s < SECOND_SCALE)
226 >            return duration / secRatio;
227 >        else if (duration > (m = maxSecs))
228 >            return Long.MAX_VALUE;
229 >        else if (duration < -m)
230 >            return Long.MIN_VALUE;
231 >        else
232 >            return duration * secRatio;
233      }
234  
235      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines