40 |
|
* @author Doug Lea |
41 |
|
*/ |
42 |
|
public enum TimeUnit { |
43 |
+ |
/** |
44 |
+ |
* Time unit representing one thousandth of a microsecond |
45 |
+ |
*/ |
46 |
|
NANOSECONDS { |
47 |
|
public long toNanos(long d) { return d; } |
48 |
|
public long toMicros(long d) { return d/(C1/C0); } |
54 |
|
public long convert(long d, TimeUnit u) { return u.toNanos(d); } |
55 |
|
int excessNanos(long d, long m) { return (int)(d - (m*C2)); } |
56 |
|
}, |
57 |
+ |
|
58 |
+ |
/** |
59 |
+ |
* Time unit representing one thousandth of a millisecond |
60 |
+ |
*/ |
61 |
|
MICROSECONDS { |
62 |
|
public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); } |
63 |
|
public long toMicros(long d) { return d; } |
69 |
|
public long convert(long d, TimeUnit u) { return u.toMicros(d); } |
70 |
|
int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); } |
71 |
|
}, |
72 |
+ |
|
73 |
+ |
/** |
74 |
+ |
* Time unit representing one thousandth of a second |
75 |
+ |
*/ |
76 |
|
MILLISECONDS { |
77 |
|
public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } |
78 |
|
public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); } |
84 |
|
public long convert(long d, TimeUnit u) { return u.toMillis(d); } |
85 |
|
int excessNanos(long d, long m) { return 0; } |
86 |
|
}, |
87 |
+ |
|
88 |
+ |
/** |
89 |
+ |
* Time unit representing one second |
90 |
+ |
*/ |
91 |
|
SECONDS { |
92 |
|
public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); } |
93 |
|
public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); } |
99 |
|
public long convert(long d, TimeUnit u) { return u.toSeconds(d); } |
100 |
|
int excessNanos(long d, long m) { return 0; } |
101 |
|
}, |
102 |
+ |
|
103 |
+ |
/** |
104 |
+ |
* Time unit representing sixty seconds |
105 |
+ |
*/ |
106 |
|
MINUTES { |
107 |
|
public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); } |
108 |
|
public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); } |
114 |
|
public long convert(long d, TimeUnit u) { return u.toMinutes(d); } |
115 |
|
int excessNanos(long d, long m) { return 0; } |
116 |
|
}, |
117 |
+ |
|
118 |
+ |
/** |
119 |
+ |
* Time unit representing sixty minutes |
120 |
+ |
*/ |
121 |
|
HOURS { |
122 |
|
public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); } |
123 |
|
public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); } |
129 |
|
public long convert(long d, TimeUnit u) { return u.toHours(d); } |
130 |
|
int excessNanos(long d, long m) { return 0; } |
131 |
|
}, |
132 |
+ |
|
133 |
+ |
/** |
134 |
+ |
* Time unit representing twenty four hours |
135 |
+ |
*/ |
136 |
|
DAYS { |
137 |
|
public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); } |
138 |
|
public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); } |
172 |
|
// etc. are not declared abstract but otherwise act as abstract methods. |
173 |
|
|
174 |
|
/** |
175 |
< |
* Convert the given time duration in the given unit to this |
176 |
< |
* unit. Conversions from finer to coarser granularities |
177 |
< |
* truncate, so lose precision. For example converting |
178 |
< |
* {@code 999} milliseconds to seconds results in |
179 |
< |
* {@code 0}. Conversions from coarser to finer granularities |
180 |
< |
* with arguments that would numerically overflow saturate to |
181 |
< |
* {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE} |
155 |
< |
* if positive. |
175 |
> |
* Converts the given time duration in the given unit to this unit. |
176 |
> |
* Conversions from finer to coarser granularities truncate, so |
177 |
> |
* lose precision. For example, converting {@code 999} milliseconds |
178 |
> |
* to seconds results in {@code 0}. Conversions from coarser to |
179 |
> |
* finer granularities with arguments that would numerically |
180 |
> |
* overflow saturate to {@code Long.MIN_VALUE} if negative or |
181 |
> |
* {@code Long.MAX_VALUE} if positive. |
182 |
|
* |
183 |
|
* <p>For example, to convert 10 minutes to milliseconds, use: |
184 |
|
* {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)} |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
< |
* Equivalent to {@code NANOSECONDS.convert(duration, this)}. |
197 |
> |
* Equivalent to |
198 |
> |
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
199 |
|
* @param duration the duration |
200 |
|
* @return the converted duration, |
201 |
|
* or {@code Long.MIN_VALUE} if conversion would negatively |
202 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
176 |
– |
* @see #convert |
203 |
|
*/ |
204 |
|
public long toNanos(long duration) { |
205 |
|
throw new AbstractMethodError(); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* Equivalent to {@code MICROSECONDS.convert(duration, this)}. |
209 |
> |
* Equivalent to |
210 |
> |
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}. |
211 |
|
* @param duration the duration |
212 |
|
* @return the converted duration, |
213 |
|
* or {@code Long.MIN_VALUE} if conversion would negatively |
214 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
188 |
– |
* @see #convert |
215 |
|
*/ |
216 |
|
public long toMicros(long duration) { |
217 |
|
throw new AbstractMethodError(); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* Equivalent to {@code MILLISECONDS.convert(duration, this)}. |
221 |
> |
* Equivalent to |
222 |
> |
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}. |
223 |
|
* @param duration the duration |
224 |
|
* @return the converted duration, |
225 |
|
* or {@code Long.MIN_VALUE} if conversion would negatively |
226 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
200 |
– |
* @see #convert |
227 |
|
*/ |
228 |
|
public long toMillis(long duration) { |
229 |
|
throw new AbstractMethodError(); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
< |
* Equivalent to {@code SECONDS.convert(duration, this)}. |
233 |
> |
* Equivalent to |
234 |
> |
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}. |
235 |
|
* @param duration the duration |
236 |
|
* @return the converted duration, |
237 |
|
* or {@code Long.MIN_VALUE} if conversion would negatively |
238 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
212 |
– |
* @see #convert |
239 |
|
*/ |
240 |
|
public long toSeconds(long duration) { |
241 |
|
throw new AbstractMethodError(); |
242 |
|
} |
243 |
|
|
244 |
|
/** |
245 |
< |
* Equivalent to {@code MINUTES.convert(duration, this)}. |
245 |
> |
* Equivalent to |
246 |
> |
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}. |
247 |
|
* @param duration the duration |
248 |
|
* @return the converted duration, |
249 |
|
* or {@code Long.MIN_VALUE} if conversion would negatively |
250 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
224 |
– |
* @see #convert |
251 |
|
* @since 1.6 |
252 |
|
*/ |
253 |
|
public long toMinutes(long duration) { |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* Equivalent to {@code HOURS.convert(duration, this)}. |
258 |
> |
* Equivalent to |
259 |
> |
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}. |
260 |
|
* @param duration the duration |
261 |
|
* @return the converted duration, |
262 |
|
* or {@code Long.MIN_VALUE} if conversion would negatively |
263 |
|
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
237 |
– |
* @see #convert |
264 |
|
* @since 1.6 |
265 |
|
*/ |
266 |
|
public long toHours(long duration) { |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
< |
* Equivalent to {@code DAYS.convert(duration, this)}. |
271 |
> |
* Equivalent to |
272 |
> |
* {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}. |
273 |
|
* @param duration the duration |
274 |
|
* @return the converted duration |
248 |
– |
* @see #convert |
275 |
|
* @since 1.6 |
276 |
|
*/ |
277 |
|
public long toDays(long duration) { |