-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterval_291.java
More file actions
485 lines (456 loc) · 19.9 KB
/
Interval_291.java
File metadata and controls
485 lines (456 loc) · 19.9 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
/*
* Copyright 2001-2006 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.time.base.BaseInterval;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.ISOPeriodFormat;
/**
* Interval is the standard implementation of an immutable time interval.
* <p>
* A time interval represents a period of time between two instants.
* Intervals are inclusive of the start instant and exclusive of the end.
* The end instant is always greater than or equal to the start instant.
* <p>
* Intervals have a fixed millisecond duration.
* This is the difference between the start and end instants.
* The duration is represented separately by {@link ReadableDuration}.
* As a result, intervals are not comparable.
* To compare the length of two intervals, you should compare their durations.
* <p>
* An interval can also be converted to a {@link ReadablePeriod}.
* This represents the difference between the start and end points in terms of fields
* such as years and days.
* <p>
* Interval is thread-safe and immutable.
*
* @author Brian S O'Neill
* @author Sean Geoghegan
* @author Stephen Colebourne
* @author Julen Parra
* @since 1.0
*/
public final class Interval
extends BaseInterval
implements ReadableInterval, Serializable {
/** Serialization version */
private static final long serialVersionUID = 4922451897541386752L;
//-----------------------------------------------------------------------
/**
* Parses a {@code Interval} from the specified string.
* <p>
* The String formats are described by {@link ISODateTimeFormat#dateTimeParser()}
* and {@link ISOPeriodFormat#standard()}, and may be 'datetime/datetime',
* 'datetime/period' or 'period/datetime'.
*
* @param str the string to parse, not null
* @since 2.0
*/
public static Interval parse(String str) {
return new Interval(str);
}
//-----------------------------------------------------------------------
/**
* Constructs an interval from a start and end instant with the ISO
* default chronology in the default time zone.
*
* @param startInstant start of this interval, as milliseconds from 1970-01-01T00:00:00Z.
* @param endInstant end of this interval, as milliseconds from 1970-01-01T00:00:00Z.
* @throws IllegalArgumentException if the end is before the start
*/
public Interval(long startInstant, long endInstant) {
super(startInstant, endInstant, null);
}
/**
* Constructs an interval from a start and end instant with the ISO
* default chronology in the specified time zone.
*
* @param startInstant start of this interval, as milliseconds from 1970-01-01T00:00:00Z.
* @param endInstant end of this interval, as milliseconds from 1970-01-01T00:00:00Z.
* @param zone the time zone to use, null means default zone
* @throws IllegalArgumentException if the end is before the start
* @since 1.5
*/
public Interval(long startInstant, long endInstant, DateTimeZone zone) {
super(startInstant, endInstant, ISOChronology.getInstance(zone));
}
/**
* Constructs an interval from a start and end instant with the
* specified chronology.
*
* @param chronology the chronology to use, null is ISO default
* @param startInstant start of this interval, as milliseconds from 1970-01-01T00:00:00Z.
* @param endInstant end of this interval, as milliseconds from 1970-01-01T00:00:00Z.
* @throws IllegalArgumentException if the end is before the start
*/
public Interval(long startInstant, long endInstant, Chronology chronology) {
super(startInstant, endInstant, chronology);
}
/**
* Constructs an interval from a start and end instant.
* <p>
* The chronology used is that of the start instant.
*
* @param start start of this interval, null means now
* @param end end of this interval, null means now
* @throws IllegalArgumentException if the end is before the start
*/
public Interval(ReadableInstant start, ReadableInstant end) {
super(start, end);
}
/**
* Constructs an interval from a start instant and a duration.
*
* @param start start of this interval, null means now
* @param duration the duration of this interval, null means zero length
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the end instant exceeds the capacity of a long
*/
public Interval(ReadableInstant start, ReadableDuration duration) {
super(start, duration);
}
/**
* Constructs an interval from a millisecond duration and an end instant.
*
* @param duration the duration of this interval, null means zero length
* @param end end of this interval, null means now
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the start instant exceeds the capacity of a long
*/
public Interval(ReadableDuration duration, ReadableInstant end) {
super(duration, end);
}
/**
* Constructs an interval from a start instant and a time period.
* <p>
* When forming the interval, the chronology from the instant is used
* if present, otherwise the chronology of the period is used.
*
* @param start start of this interval, null means now
* @param period the period of this interval, null means zero length
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the end instant exceeds the capacity of a long
*/
public Interval(ReadableInstant start, ReadablePeriod period) {
super(start, period);
}
/**
* Constructs an interval from a time period and an end instant.
* <p>
* When forming the interval, the chronology from the instant is used
* if present, otherwise the chronology of the period is used.
*
* @param period the period of this interval, null means zero length
* @param end end of this interval, null means now
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the start instant exceeds the capacity of a long
*/
public Interval(ReadablePeriod period, ReadableInstant end) {
super(period, end);
}
/**
* Constructs a time interval by converting or copying from another object.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadableInterval and String.
* The String formats are described by {@link ISODateTimeFormat#dateTimeParser()}
* and {@link ISOPeriodFormat#standard()}, and may be 'datetime/datetime',
* 'datetime/period' or 'period/datetime'.
*
* @param interval the time interval to copy
* @throws IllegalArgumentException if the interval is invalid
*/
public Interval(Object interval) {
super(interval, null);
}
/**
* Constructs a time interval by converting or copying from another object,
* overriding the chronology.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadableInterval and String.
* The String formats are described by {@link ISODateTimeFormat#dateTimeParser()}
* and {@link ISOPeriodFormat#standard()}, and may be 'datetime/datetime',
* 'datetime/period' or 'period/datetime'.
*
* @param interval the time interval to copy
* @param chronology the chronology to use, null means ISO default
* @throws IllegalArgumentException if the interval is invalid
*/
public Interval(Object interval, Chronology chronology) {
super(interval, chronology);
}
//-----------------------------------------------------------------------
/**
* Get this interval as an immutable <code>Interval</code> object
* by returning <code>this</code>.
*
* @return <code>this</code>
*/
public Interval toInterval() {
return this;
}
//-----------------------------------------------------------------------
/**
* Gets the overlap between this interval and another interval.
* <p>
* Intervals are inclusive of the start instant and exclusive of the end.
* An interval overlaps another if it shares some common part of the
* datetime continuum. This method returns the amount of the overlap,
* only if the intervals actually do overlap.
* If the intervals do not overlap, then null is returned.
* <p>
* When two intervals are compared the result is one of three states:
* (a) they abut, (b) there is a gap between them, (c) they overlap.
* The abuts state takes precedence over the other two, thus a zero duration
* interval at the start of a larger interval abuts and does not overlap.
* <p>
* The chronology of the returned interval is the same as that of
* this interval (the chronology of the interval parameter is not used).
* Note that the use of the chronology was only correctly implemented
* in version 1.3.
*
* @param interval the interval to examine, null means now
* @return the overlap interval, null if no overlap
* @since 1.1
*/
public Interval overlap(ReadableInterval interval) {
interval = DateTimeUtils.getReadableInterval(interval);
if (overlaps(interval) == false) {
return null;
}
long start = Math.max(getStartMillis(), interval.getStartMillis());
long end = Math.min(getEndMillis(), interval.getEndMillis());
return new Interval(start, end, getChronology());
}
//-----------------------------------------------------------------------
/**
* Gets the gap between this interval and another interval.
* The other interval can be either before or after this interval.
* <p>
* Intervals are inclusive of the start instant and exclusive of the end.
* An interval has a gap to another interval if there is a non-zero
* duration between them. This method returns the amount of the gap only
* if the intervals do actually have a gap between them.
* If the intervals overlap or abut, then null is returned.
* <p>
* When two intervals are compared the result is one of three states:
* (a) they abut, (b) there is a gap between them, (c) they overlap.
* The abuts state takes precedence over the other two, thus a zero duration
* interval at the start of a larger interval abuts and does not overlap.
* <p>
* The chronology of the returned interval is the same as that of
* this interval (the chronology of the interval parameter is not used).
* Note that the use of the chronology was only correctly implemented
* in version 1.3.
*
* @param interval the interval to examine, null means now
* @return the gap interval, null if no gap
* @since 1.1
*/
public Interval gap(ReadableInterval interval) {
interval = DateTimeUtils.getReadableInterval(interval);
long otherStart = interval.getStartMillis();
long otherEnd = interval.getEndMillis();
long thisStart = getStartMillis();
long thisEnd = getEndMillis();
if (thisStart <= otherEnd) {
return new Interval(otherEnd, thisStart, getChronology());
} else if (otherStart > thisEnd) {
return new Interval(thisEnd, otherStart, getChronology());
} else {
return null;
}
}
//-----------------------------------------------------------------------
/**
* Does this interval abut with the interval specified.
* <p>
* Intervals are inclusive of the start instant and exclusive of the end.
* An interval abuts if it starts immediately after, or ends immediately
* before this interval without overlap.
* A zero duration interval abuts with itself.
* <p>
* When two intervals are compared the result is one of three states:
* (a) they abut, (b) there is a gap between them, (c) they overlap.
* The abuts state takes precedence over the other two, thus a zero duration
* interval at the start of a larger interval abuts and does not overlap.
* <p>
* For example:
* <pre>
* [09:00 to 10:00) abuts [08:00 to 08:30) = false (completely before)
* [09:00 to 10:00) abuts [08:00 to 09:00) = true
* [09:00 to 10:00) abuts [08:00 to 09:01) = false (overlaps)
*
* [09:00 to 10:00) abuts [09:00 to 09:00) = true
* [09:00 to 10:00) abuts [09:00 to 09:01) = false (overlaps)
*
* [09:00 to 10:00) abuts [10:00 to 10:00) = true
* [09:00 to 10:00) abuts [10:00 to 10:30) = true
*
* [09:00 to 10:00) abuts [10:30 to 11:00) = false (completely after)
*
* [14:00 to 14:00) abuts [14:00 to 14:00) = true
* [14:00 to 14:00) abuts [14:00 to 15:00) = true
* [14:00 to 14:00) abuts [13:00 to 14:00) = true
* </pre>
*
* @param interval the interval to examine, null means now
* @return true if the interval abuts
* @since 1.1
*/
public boolean abuts(ReadableInterval interval) {
if (interval == null) {
long now = DateTimeUtils.currentTimeMillis();
return (getStartMillis() == now || getEndMillis() == now);
} else {
return (interval.getEndMillis() == getStartMillis() ||
getEndMillis() == interval.getStartMillis());
}
}
//-----------------------------------------------------------------------
/**
* Creates a new interval with the same start and end, but a different chronology.
*
* @param chronology the chronology to use, null means ISO default
* @return an interval with a different chronology
*/
public Interval withChronology(Chronology chronology) {
if (getChronology() == chronology) {
return this;
}
return new Interval(getStartMillis(), getEndMillis(), chronology);
}
/**
* Creates a new interval with the specified start millisecond instant.
*
* @param startInstant the start instant for the new interval
* @return an interval with the end from this interval and the specified start
* @throws IllegalArgumentException if the resulting interval has end before start
*/
public Interval withStartMillis(long startInstant) {
if (startInstant == getStartMillis()) {
return this;
}
return new Interval(startInstant, getEndMillis(), getChronology());
}
/**
* Creates a new interval with the specified start instant.
*
* @param start the start instant for the new interval, null means now
* @return an interval with the end from this interval and the specified start
* @throws IllegalArgumentException if the resulting interval has end before start
*/
public Interval withStart(ReadableInstant start) {
long startMillis = DateTimeUtils.getInstantMillis(start);
return withStartMillis(startMillis);
}
/**
* Creates a new interval with the specified start millisecond instant.
*
* @param endInstant the end instant for the new interval
* @return an interval with the start from this interval and the specified end
* @throws IllegalArgumentException if the resulting interval has end before start
*/
public Interval withEndMillis(long endInstant) {
if (endInstant == getEndMillis()) {
return this;
}
return new Interval(getStartMillis(), endInstant, getChronology());
}
/**
* Creates a new interval with the specified end instant.
*
* @param end the end instant for the new interval, null means now
* @return an interval with the start from this interval and the specified end
* @throws IllegalArgumentException if the resulting interval has end before start
*/
public Interval withEnd(ReadableInstant end) {
long endMillis = DateTimeUtils.getInstantMillis(end);
return withEndMillis(endMillis);
}
//-----------------------------------------------------------------------
/**
* Creates a new interval with the specified duration after the start instant.
*
* @param duration the duration to add to the start to get the new end instant, null means zero
* @return an interval with the start from this interval and a calculated end
* @throws IllegalArgumentException if the duration is negative
*/
public Interval withDurationAfterStart(ReadableDuration duration) {
long durationMillis = DateTimeUtils.getDurationMillis(duration);
if (durationMillis == toDurationMillis()) {
return this;
}
Chronology chrono = getChronology();
long startMillis = getStartMillis();
long endMillis = chrono.add(startMillis, durationMillis, 1);
return new Interval(startMillis, endMillis, chrono);
}
/**
* Creates a new interval with the specified duration before the end instant.
*
* @param duration the duration to add to the start to get the new end instant, null means zero
* @return an interval with the start from this interval and a calculated end
* @throws IllegalArgumentException if the duration is negative
*/
public Interval withDurationBeforeEnd(ReadableDuration duration) {
long durationMillis = DateTimeUtils.getDurationMillis(duration);
if (durationMillis == toDurationMillis()) {
return this;
}
Chronology chrono = getChronology();
long endMillis = getEndMillis();
long startMillis = chrono.add(endMillis, durationMillis, -1);
return new Interval(startMillis, endMillis, chrono);
}
//-----------------------------------------------------------------------
/**
* Creates a new interval with the specified period after the start instant.
*
* @param period the period to add to the start to get the new end instant, null means zero
* @return an interval with the start from this interval and a calculated end
* @throws IllegalArgumentException if the period is negative
*/
public Interval withPeriodAfterStart(ReadablePeriod period) {
if (period == null) {
return withDurationAfterStart(null);
}
Chronology chrono = getChronology();
long startMillis = getStartMillis();
long endMillis = chrono.add(period, startMillis, 1);
return new Interval(startMillis, endMillis, chrono);
}
/**
* Creates a new interval with the specified period before the end instant.
*
* @param period the period to add to the start to get the new end instant, null means zero
* @return an interval with the start from this interval and a calculated end
* @throws IllegalArgumentException if the period is negative
*/
public Interval withPeriodBeforeEnd(ReadablePeriod period) {
if (period == null) {
return withDurationBeforeEnd(null);
}
Chronology chrono = getChronology();
long endMillis = getEndMillis();
long startMillis = chrono.add(period, endMillis, -1);
return new Interval(startMillis, endMillis, chrono);
}
}