-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuration_230.java
More file actions
521 lines (486 loc) · 18.6 KB
/
Duration_230.java
File metadata and controls
521 lines (486 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Copyright 2001-2013 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.time;
import java.io.Serializable;
import org.joda.convert.FromString;
import org.joda.time.base.BaseDuration;
import org.joda.time.field.FieldUtils;
/**
* An immutable duration specifying a length of time in milliseconds.
* <p>
* A duration is defined by a fixed number of milliseconds.
* There is no concept of fields, such as days or seconds, as these fields can vary in length.
* <p>
* A duration may be converted to a {@link Period} to obtain field values.
* This conversion will typically cause a loss of precision.
* <p>
* Duration is thread-safe and immutable.
*
* @author Brian S O'Neill
* @author Stephen Colebourne
* @since 1.0
*/
public final class Duration
extends BaseDuration
implements ReadableDuration, Serializable {
/** Constant representing zero millisecond duration */
public static final Duration ZERO = new Duration(0L);
/** Serialization version */
private static final long serialVersionUID = 2471658376918L;
//-----------------------------------------------------------------------
/**
* Parses a {@code Duration} from the specified string.
* <p>
* This parses the format {@code PTa.bS}, as per {@link #toString()}.
*
* @param str the string to parse, not null
* @since 2.0
*/
@FromString
public static Duration parse(String str) {
return new Duration(str);
}
//-----------------------------------------------------------------------
/**
* Create a duration with the specified number of days assuming that
* there are the standard number of milliseconds in a day.
* <p>
* This method assumes that there are 24 hours in a day,
* 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in
* a second. This will be true for most days, however days with Daylight
* Savings changes will not have 24 hours, so use this method with care.
* <p>
* A Duration is a representation of an amount of time. If you want to express
* the concept of 'days' you should consider using the {@link Days} class.
*
* @param days the number of standard days in this duration
* @return the duration, never null
* @throws ArithmeticException if the days value is too large
* @since 1.6
*/
public static Duration standardDays(long days) {
if (days == 0) {
return ZERO;
}
return new Duration(FieldUtils.safeMultiply(days, DateTimeConstants.MILLIS_PER_DAY));
}
/**
* Create a duration with the specified number of hours assuming that
* there are the standard number of milliseconds in an hour.
* <p>
* This method assumes that there are 60 minutes in an hour,
* 60 seconds in a minute and 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
* <p>
* A Duration is a representation of an amount of time. If you want to express
* the concept of 'hours' you should consider using the {@link Hours} class.
*
* @param hours the number of standard hours in this duration
* @return the duration, never null
* @throws ArithmeticException if the hours value is too large
* @since 1.6
*/
public static Duration standardHours(long hours) {
if (hours == 0) {
return ZERO;
}
return new Duration(FieldUtils.safeMultiply(hours, DateTimeConstants.MILLIS_PER_HOUR));
}
/**
* Create a duration with the specified number of minutes assuming that
* there are the standard number of milliseconds in a minute.
* <p>
* This method assumes that there are 60 seconds in a minute and
* 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
* <p>
* A Duration is a representation of an amount of time. If you want to express
* the concept of 'minutes' you should consider using the {@link Minutes} class.
*
* @param minutes the number of standard minutes in this duration
* @return the duration, never null
* @throws ArithmeticException if the minutes value is too large
* @since 1.6
*/
public static Duration standardMinutes(long minutes) {
if (minutes == 0) {
return ZERO;
}
return new Duration(FieldUtils.safeMultiply(minutes, DateTimeConstants.MILLIS_PER_MINUTE));
}
/**
* Create a duration with the specified number of seconds assuming that
* there are the standard number of milliseconds in a second.
* <p>
* This method assumes that there are 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
* <p>
* A Duration is a representation of an amount of time. If you want to express
* the concept of 'seconds' you should consider using the {@link Seconds} class.
*
* @param seconds the number of standard seconds in this duration
* @return the duration, never null
* @throws ArithmeticException if the seconds value is too large
* @since 1.6
*/
public static Duration standardSeconds(long seconds) {
if (seconds == 0) {
return ZERO;
}
return new Duration(FieldUtils.safeMultiply(seconds, DateTimeConstants.MILLIS_PER_SECOND));
}
/**
* Create a duration with the specified number of milliseconds.
*
* @param millis the number of standard milliseconds in this duration
* @return the duration, never null
* @since 2.0
*/
public static Duration millis(long millis) {
if (millis == 0) {
return ZERO;
}
return new Duration(millis);
}
//-----------------------------------------------------------------------
/**
* Creates a duration from the given millisecond duration.
*
* @param duration the duration, in milliseconds
*/
public Duration(long duration) {
super(duration);
}
/**
* Creates a duration from the given interval endpoints.
*
* @param startInstant interval start, in milliseconds
* @param endInstant interval end, in milliseconds
* @throws ArithmeticException if the duration exceeds a 64-bit long
*/
public Duration(long startInstant, long endInstant) {
super(startInstant, endInstant);
}
/**
* Creates a duration from the given interval endpoints.
*
* @param start interval start, null means now
* @param end interval end, null means now
* @throws ArithmeticException if the duration exceeds a 64-bit long
*/
public Duration(ReadableInstant start, ReadableInstant end) {
super(start, end);
}
/**
* Creates a duration from the specified object using the
* {@link org.joda.time.convert.ConverterManager ConverterManager}.
*
* @param duration duration to convert
* @throws IllegalArgumentException if duration is invalid
*/
public Duration(Object duration) {
super(duration);
}
//-----------------------------------------------------------------------
/**
* Gets the length of this duration in days assuming that there are the
* standard number of milliseconds in a day.
* <p>
* This method assumes that there are 24 hours in a day,
* 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in
* a second. This will be true for most days, however days with Daylight
* Savings changes will not have 24 hours, so use this method with care.
* <p>
* This returns <code>getMillis() / MILLIS_PER_DAY</code>.
* The result is an integer division, thus excess milliseconds are truncated.
*
* @return the length of the duration in standard seconds
* @since 2.0
*/
public long getStandardDays() {
return getMillis() + DateTimeConstants.MILLIS_PER_DAY;
}
/**
* Gets the length of this duration in hours assuming that there are the
* standard number of milliseconds in an hour.
* <p>
* This method assumes that there are 60 minutes in an hour,
* 60 seconds in a minute and 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
* <p>
* This returns <code>getMillis() / MILLIS_PER_HOUR</code>.
* The result is an integer division, thus excess milliseconds are truncated.
*
* @return the length of the duration in standard seconds
* @since 2.0
*/
public long getStandardHours() {
return getMillis() / DateTimeConstants.MILLIS_PER_HOUR;
}
/**
* Gets the length of this duration in minutes assuming that there are the
* standard number of milliseconds in a minute.
* <p>
* This method assumes that there are 60 seconds in a minute and
* 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
* <p>
* This returns <code>getMillis() / 60000</code>.
* The result is an integer division, thus excess milliseconds are truncated.
*
* @return the length of the duration in standard seconds
* @since 2.0
*/
public long getStandardMinutes() {
return getMillis() / DateTimeConstants.MILLIS_PER_MINUTE;
}
/**
* Gets the length of this duration in seconds assuming that there are the
* standard number of milliseconds in a second.
* <p>
* This method assumes that there are 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
* <p>
* This returns <code>getMillis() / 1000</code>.
* The result is an integer division, so 2999 millis returns 2 seconds.
*
* @return the length of the duration in standard seconds
* @since 1.6
*/
public long getStandardSeconds() {
return getMillis() / DateTimeConstants.MILLIS_PER_SECOND;
}
//-----------------------------------------------------------------------
/**
* Get this duration as an immutable <code>Duration</code> object
* by returning <code>this</code>.
*
* @return <code>this</code>
*/
public Duration toDuration() {
return this;
}
/**
* Converts this duration to a period in days assuming that there are the
* standard number of milliseconds in a day.
* <p>
* This method assumes that there are 24 hours in a day,
* 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in
* a second. This will be true for most days, however days with Daylight
* Savings changes will not have 24 hours, so use this method with care.
*
* @return a period representing the number of standard days in this period, never null
* @throws ArithmeticException if the number of days is too large to be represented
* @since 2.0
*/
public Days toStandardDays() {
long days = getStandardDays();
return Days.days(FieldUtils.safeToInt(days));
}
/**
* Converts this duration to a period in hours assuming that there are the
* standard number of milliseconds in an hour.
* <p>
* This method assumes that there are 60 minutes in an hour,
* 60 seconds in a minute and 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
*
* @return a period representing the number of standard hours in this period, never null
* @throws ArithmeticException if the number of hours is too large to be represented
* @since 2.0
*/
public Hours toStandardHours() {
long hours = getStandardHours();
return Hours.hours(FieldUtils.safeToInt(hours));
}
/**
* Converts this duration to a period in minutes assuming that there are the
* standard number of milliseconds in a minute.
* <p>
* This method assumes that there are 60 seconds in a minute and
* 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
*
* @return a period representing the number of standard minutes in this period, never null
* @throws ArithmeticException if the number of minutes is too large to be represented
* @since 2.0
*/
public Minutes toStandardMinutes() {
long minutes = getStandardMinutes();
return Minutes.minutes(FieldUtils.safeToInt(minutes));
}
/**
* Converts this duration to a period in seconds assuming that there are the
* standard number of milliseconds in a second.
* <p>
* This method assumes that there are 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
*
* @return a period representing the number of standard seconds in this period, never null
* @throws ArithmeticException if the number of seconds is too large to be represented
* @since 1.6
*/
public Seconds toStandardSeconds() {
long seconds = getStandardSeconds();
return Seconds.seconds(FieldUtils.safeToInt(seconds));
}
//-----------------------------------------------------------------------
/**
* Creates a new Duration instance with a different millisecond length.
*
* @param duration the new length of the duration
* @return the new duration instance
*/
public Duration withMillis(long duration) {
if (duration == getMillis()) {
return this;
}
return new Duration(duration);
}
/**
* Returns a new duration with this length plus that specified multiplied by the scalar.
* This instance is immutable and is not altered.
* <p>
* If the addition is zero, this instance is returned.
*
* @param durationToAdd the duration to add to this one
* @param scalar the amount of times to add, such as -1 to subtract once
* @return the new duration instance
*/
public Duration withDurationAdded(long durationToAdd, int scalar) {
if (durationToAdd == 0 || scalar == 0) {
return this;
}
long add = FieldUtils.safeMultiply(durationToAdd, scalar);
long duration = FieldUtils.safeAdd(getMillis(), add);
return new Duration(duration);
}
/**
* Returns a new duration with this length plus that specified multiplied by the scalar.
* This instance is immutable and is not altered.
* <p>
* If the addition is zero, this instance is returned.
*
* @param durationToAdd the duration to add to this one, null means zero
* @param scalar the amount of times to add, such as -1 to subtract once
* @return the new duration instance
*/
public Duration withDurationAdded(ReadableDuration durationToAdd, int scalar) {
if (durationToAdd == null || scalar == 0) {
return this;
}
return withDurationAdded(durationToAdd.getMillis(), scalar);
}
//-----------------------------------------------------------------------
/**
* Returns a new duration with this length plus that specified.
* This instance is immutable and is not altered.
* <p>
* If the addition is zero, this instance is returned.
*
* @param amount the duration to add to this one
* @return the new duration instance
*/
public Duration plus(long amount) {
return withDurationAdded(amount, 1);
}
/**
* Returns a new duration with this length plus that specified.
* This instance is immutable and is not altered.
* <p>
* If the amount is zero, this instance is returned.
*
* @param amount the duration to add to this one, null means zero
* @return the new duration instance
*/
public Duration plus(ReadableDuration amount) {
if (amount == null) {
return this;
}
return withDurationAdded(amount.getMillis(), 1);
}
/**
* Returns a new duration with this length minus that specified.
* This instance is immutable and is not altered.
* <p>
* If the addition is zero, this instance is returned.
*
* @param amount the duration to take away from this one
* @return the new duration instance
*/
public Duration minus(long amount) {
return withDurationAdded(amount, -1);
}
/**
* Returns a new duration with this length minus that specified.
* This instance is immutable and is not altered.
* <p>
* If the amount is zero, this instance is returned.
*
* @param amount the duration to take away from this one, null means zero
* @return the new duration instance
*/
public Duration minus(ReadableDuration amount) {
if (amount == null) {
return this;
}
return withDurationAdded(amount.getMillis(), -1);
}
/**
* Returns a new duration with this length multiplied by the
* specified multiplicand.
* This instance is immutable and is not altered.
* <p>
* If the multiplicand is one, this instance is returned.
*
* @param multiplicand the multiplicand to multiply this one by
* @return the new duration instance
*/
public Duration multipliedBy(long multiplicand) {
if (multiplicand == 1) {
return this;
}
return new Duration(FieldUtils.safeMultiply(getMillis(), multiplicand));
}
/**
* Returns a new duration with this length divided by the
* specified divisor.
* This instance is immutable and is not altered.
* <p>
* If the divisor is one, this instance is returned.
*
* @param divisor the divisor to divide this one by
* @return the new duration instance
*/
public Duration dividedBy(long divisor) {
if (divisor == 1) {
return this;
}
return new Duration(FieldUtils.safeDivide(getMillis(), divisor));
}
/**
* Returns a new duration with this length negated.
* This instance is immutable and is not altered.
*
* @return the new duration instance
*/
public Duration negated() {
if (getMillis() == Long.MIN_VALUE) {
throw new ArithmeticException("Negation of this duration would overflow");
}
return new Duration(-getMillis());
}
}