46 |
|
/** |
47 |
|
* Time unit representing one thousandth of a microsecond. |
48 |
|
*/ |
49 |
< |
NANOSECONDS(1L), // (cannot use symbolic scale names here) |
49 |
> |
NANOSECONDS(TimeUnit.NANO_SCALE), |
50 |
|
/** |
51 |
|
* Time unit representing one thousandth of a millisecond. |
52 |
|
*/ |
53 |
< |
MICROSECONDS(1000L), |
53 |
> |
MICROSECONDS(TimeUnit.MICRO_SCALE), |
54 |
|
/** |
55 |
|
* Time unit representing one thousandth of a second. |
56 |
|
*/ |
57 |
< |
MILLISECONDS(1000L * 1000L), |
57 |
> |
MILLISECONDS(TimeUnit.MILLI_SCALE), |
58 |
|
/** |
59 |
|
* Time unit representing one second. |
60 |
|
*/ |
61 |
< |
SECONDS(1000L * 1000L * 1000L), |
61 |
> |
SECONDS(TimeUnit.SECOND_SCALE), |
62 |
|
/** |
63 |
|
* Time unit representing sixty seconds. |
64 |
|
* @since 1.6 |
65 |
|
*/ |
66 |
< |
MINUTES(1000L * 1000L * 1000L * 60L), |
66 |
> |
MINUTES(TimeUnit.MINUTE_SCALE), |
67 |
|
/** |
68 |
|
* Time unit representing sixty minutes. |
69 |
|
* @since 1.6 |
70 |
|
*/ |
71 |
< |
HOURS(1000L * 1000L * 1000L * 60L * 60L), |
71 |
> |
HOURS(TimeUnit.HOUR_SCALE), |
72 |
|
/** |
73 |
|
* Time unit representing twenty four hours. |
74 |
|
* @since 1.6 |
75 |
|
*/ |
76 |
< |
DAYS(1000L * 1000L * 1000L * 60L * 60L * 24L); |
76 |
> |
DAYS(TimeUnit.DAY_SCALE); |
77 |
|
|
78 |
|
// Scales as constants |
79 |
|
private static final long NANO_SCALE = 1L; |
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); |
108 |
> |
long mr = (s >= MILLI_SCALE) ? (s / MILLI_SCALE) : (MILLI_SCALE / s); |
109 |
|
this.milliRatio = (int)mr; |
110 |
|
this.maxMillis = Long.MAX_VALUE / mr; |
111 |
|
long sr = (s >= SECOND_SCALE) ? (s / SECOND_SCALE) : (SECOND_SCALE / s); |
117 |
|
* General conversion utility. |
118 |
|
* |
119 |
|
* @param d duration |
120 |
< |
* @param dst result scale unit |
121 |
< |
* @param src source scale unit |
120 |
> |
* @param dst result unit scale |
121 |
> |
* @param src source unit scale |
122 |
|
*/ |
123 |
|
private static long cvt(long d, long dst, long src) { |
124 |
|
long r, m; |
149 |
|
* @param sourceDuration the time duration in the given {@code sourceUnit} |
150 |
|
* @param sourceUnit the unit of the {@code sourceDuration} argument |
151 |
|
* @return the converted duration in this unit, |
152 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
153 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
152 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
153 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
154 |
|
*/ |
155 |
|
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
156 |
|
switch (this) { |
157 |
< |
case NANOSECONDS: return sourceUnit.toNanos(sourceDuration); |
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); |
160 |
> |
case SECONDS: return sourceUnit.toSeconds(sourceDuration); |
161 |
|
default: return cvt(sourceDuration, scale, sourceUnit.scale); |
162 |
|
} |
163 |
|
} |
167 |
|
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
168 |
|
* @param duration the duration |
169 |
|
* @return the converted duration, |
170 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
171 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
170 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
171 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
172 |
|
*/ |
173 |
|
public long toNanos(long duration) { |
174 |
|
long s, m; |
187 |
|
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}. |
188 |
|
* @param duration the duration |
189 |
|
* @return the converted duration, |
190 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
191 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
190 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
191 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
192 |
|
*/ |
193 |
|
public long toMicros(long duration) { |
194 |
|
long s, m; |
209 |
|
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}. |
210 |
|
* @param duration the duration |
211 |
|
* @return the converted duration, |
212 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
213 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
212 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
213 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
214 |
|
*/ |
215 |
|
public long toMillis(long duration) { |
216 |
|
long s, m; |
231 |
|
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}. |
232 |
|
* @param duration the duration |
233 |
|
* @return the converted duration, |
234 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
235 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
234 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
235 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
236 |
|
*/ |
237 |
|
public long toSeconds(long duration) { |
238 |
|
long s, m; |
253 |
|
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}. |
254 |
|
* @param duration the duration |
255 |
|
* @return the converted duration, |
256 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
257 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
256 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
257 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
258 |
|
* @since 1.6 |
259 |
|
*/ |
260 |
|
public long toMinutes(long duration) { |
266 |
|
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}. |
267 |
|
* @param duration the duration |
268 |
|
* @return the converted duration, |
269 |
< |
* or {@code Long.MIN_VALUE} if conversion would negatively |
270 |
< |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
269 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively overflow, |
270 |
> |
* or {@code Long.MAX_VALUE} if it would positively overflow. |
271 |
|
* @since 1.6 |
272 |
|
*/ |
273 |
|
public long toHours(long duration) { |