86 |
|
|
87 |
|
/* |
88 |
|
* Instances cache conversion ratios and saturation cutoffs for |
89 |
< |
* the units most commonly used in conversion results, in methods |
90 |
< |
* toNanos, toMillis and toSeconds. Other cases compute them, in |
89 |
> |
* the units up through SECONDS. Other cases compute them, in |
90 |
|
* method cvt. |
91 |
|
*/ |
92 |
|
|
93 |
|
private final long scale; |
94 |
|
private final long maxNanos; |
95 |
+ |
private final long maxMicros; |
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 mr = (scale >= MILLI_SCALE) |
105 |
< |
? (scale / MILLI_SCALE) |
106 |
< |
: (MILLI_SCALE / scale); |
98 |
> |
private final long microRatio; |
99 |
> |
private final int milliRatio; // fits in 32 bits |
100 |
> |
private final int secRatio; // fits in 32 bits |
101 |
> |
|
102 |
> |
TimeUnit(long s) { |
103 |
> |
this.scale = s; |
104 |
> |
this.maxNanos = Long.MAX_VALUE / s; |
105 |
> |
long ur = (s >= MICRO_SCALE) ? (s / MICRO_SCALE) : (MICRO_SCALE / s); |
106 |
> |
this.microRatio = ur; |
107 |
> |
this.maxMicros = Long.MAX_VALUE / ur; |
108 |
> |
long mr = (scale >= MILLI_SCALE) ? (s / MILLI_SCALE) : (MILLI_SCALE / s); |
109 |
|
this.milliRatio = (int)mr; |
110 |
|
this.maxMillis = Long.MAX_VALUE / mr; |
111 |
< |
long sr = (scale >= SECOND_SCALE) |
110 |
< |
? (scale / SECOND_SCALE) |
111 |
< |
: (SECOND_SCALE / scale); |
111 |
> |
long sr = (s >= SECOND_SCALE) ? (s / SECOND_SCALE) : (SECOND_SCALE / s); |
112 |
|
this.secRatio = (int)sr; |
113 |
|
this.maxSecs = Long.MAX_VALUE / sr; |
114 |
|
} |
153 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
154 |
|
*/ |
155 |
|
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
156 |
< |
return cvt(sourceDuration, scale, sourceUnit.scale); |
156 |
> |
switch (this) { |
157 |
> |
case NANOSECONDS: return sourceUnit.toNanos(sourceDuration); |
158 |
> |
case MICROSECONDS: return sourceUnit.toMicros(sourceDuration); |
159 |
> |
case MILLISECONDS: return sourceUnit.toMillis(sourceDuration); |
160 |
> |
case SECONDS: return sourceUnit.toSeconds(sourceDuration); |
161 |
> |
default: return cvt(sourceDuration, scale, sourceUnit.scale); |
162 |
> |
} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
191 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
192 |
|
*/ |
193 |
|
public long toMicros(long duration) { |
194 |
< |
return cvt(duration, MICRO_SCALE, scale); |
194 |
> |
long s, m; |
195 |
> |
if ((s = scale) == MICRO_SCALE) |
196 |
> |
return duration; |
197 |
> |
else if (s < MICRO_SCALE) |
198 |
> |
return duration / microRatio; |
199 |
> |
else if (duration > (m = maxMicros)) |
200 |
> |
return Long.MAX_VALUE; |
201 |
> |
else if (duration < -m) |
202 |
> |
return Long.MIN_VALUE; |
203 |
> |
else |
204 |
> |
return duration * microRatio; |
205 |
|
} |
206 |
|
|
207 |
|
/** |