-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutableInterval_245.java
More file actions
414 lines (382 loc) · 15.9 KB
/
MutableInterval_245.java
File metadata and controls
414 lines (382 loc) · 15.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
/*
* Copyright 2001-2005 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.field.FieldUtils;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.ISOPeriodFormat;
/**
* MutableInterval is the standard implementation of a mutable 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>
* If performing significant calculations on an interval, it may be faster to
* convert an Interval object to a MutableInterval one.
* <p>
* MutableInterval is mutable and not thread-safe, unless concurrent threads
* are not invoking mutator methods.
*
* @author Stephen Colebourne
* @author Brian S O'Neill
* @since 1.0
*/
public class MutableInterval
extends BaseInterval
implements ReadWritableInterval, Cloneable, Serializable {
/** Serialization version */
private static final long serialVersionUID = -5982824024992428470L;
//-----------------------------------------------------------------------
/**
* Parses a {@code MutableInterval} 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 MutableInterval parse(String str) {
return new MutableInterval(str);
}
//-----------------------------------------------------------------------
/**
* Constructs a zero length time interval from 1970-01-01 to 1970-01-01.
*/
public MutableInterval() {
super(0L, 0L, null);
}
/**
* Constructs an interval from a start and end instant with the ISO default chronology.
*
* @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 MutableInterval(long startInstant, long endInstant) {
super(startInstant, endInstant, null);
}
/**
* Constructs an interval from a start and end instant with a 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 MutableInterval(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 MutableInterval(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 MutableInterval(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 MutableInterval(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 MutableInterval(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 MutableInterval(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 MutableInterval(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 MutableInterval(Object interval, Chronology chronology) {
super(interval, chronology);
}
//-----------------------------------------------------------------------
/**
* Sets this interval from two millisecond instants retaining the chronology.
*
* @param startInstant the start of the time interval
* @param endInstant the start of the time interval
* @throws IllegalArgumentException if the end is before the start
*/
public void setInterval(long startInstant, long endInstant) {
super.setInterval(startInstant, endInstant, getChronology());
}
/**
* Sets this interval to be the same as another.
*
* @param interval the interval to copy
* @throws IllegalArgumentException if the interval is null
*/
public void setInterval(ReadableInterval interval) {
if (interval == null) {
throw new IllegalArgumentException("Interval must not be null");
}
long startMillis = interval.getStartMillis();
long endMillis = interval.getEndMillis();
Chronology chrono = interval.getChronology();
super.setInterval(startMillis, endMillis, chrono);
}
/**
* Sets this interval from two instants, replacing the chronology with
* that from the start instant.
*
* @param start the start of the time interval
* @param end the start of the time interval
* @throws IllegalArgumentException if the end is before the start
*/
public void setInterval(ReadableInstant start, ReadableInstant end) {
if (start == null || end == null) {
long now = DateTimeUtils.currentTimeMillis();
setInterval(now, now);
} else {
long startMillis = DateTimeUtils.getInstantMillis(start);
long endMillis = DateTimeUtils.getInstantMillis(end);
Chronology chrono = DateTimeUtils.getInstantChronology(start);
super.setInterval(startMillis, endMillis, chrono);
}
}
//-----------------------------------------------------------------------
/**
* Sets the chronology of this time interval.
*
* @param chrono the chronology to use, null means ISO default
*/
public void setChronology(Chronology chrono) {
super.setInterval(getStartMillis(), getEndMillis(), chrono);
}
/**
* Sets the start of this time interval.
*
* @param startInstant the start of the time interval,
* millisecond instant from 1970-01-01T00:00:00Z
* @throws IllegalArgumentException if the end is before the start
*/
public void setStartMillis(long startInstant) {
super.setInterval(startInstant, getEndMillis(), getChronology());
}
/**
* Sets the start of this time interval as an Instant.
*
* @param start the start of the time interval, null means now
* @throws IllegalArgumentException if the end is before the start
*/
public void setStart(ReadableInstant start) {
long startMillis = DateTimeUtils.getInstantMillis(start);
super.setInterval(startMillis, getEndMillis(), getChronology());
}
/**
* Sets the end of this time interval.
*
* @param endInstant the end of the time interval,
* millisecond instant from 1970-01-01T00:00:00Z
* @throws IllegalArgumentException if the end is before the start
*/
public void setEndMillis(long endInstant) {
super.setInterval(getStartMillis(), endInstant, getChronology());
}
/**
* Sets the end of this time interval as an Instant.
*
* @param end the end of the time interval, null means now
* @throws IllegalArgumentException if the end is before the start
*/
public void setEnd(ReadableInstant end) {
long endMillis = DateTimeUtils.getInstantMillis(end);
super.setInterval(getStartMillis(), endMillis, getChronology());
}
//-----------------------------------------------------------------------
/**
* Sets the duration of this time interval, preserving the start instant.
*
* @param duration new duration for interval
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the end instant exceeds the capacity of a long
*/
public void setDurationAfterStart(long duration) {
setEndMillis(FieldUtils.safeAdd(getStartMillis(), duration));
}
/**
* Sets the duration of this time interval, preserving the end instant.
*
* @param duration new duration for interval
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the start instant exceeds the capacity of a long
*/
public void setDurationBeforeEnd(long duration) {
setStartMillis(FieldUtils.safeAdd(getEndMillis(), -duration));
}
//-----------------------------------------------------------------------
/**
* Sets the duration of this time interval, preserving the start instant.
*
* @param duration new duration for 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 void setDurationAfterStart(ReadableDuration duration) {
long durationMillis = DateTimeUtils.getDurationMillis(duration);
setEndMillis(FieldUtils.safeAdd(getStartMillis(), durationMillis));
}
/**
* Sets the duration of this time interval, preserving the end instant.
*
* @param duration new duration for interval, null means zero length
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the start instant exceeds the capacity of a long
*/
public void setDurationBeforeEnd(ReadableDuration duration) {
long durationMillis = DateTimeUtils.getDurationMillis(duration);
setStartMillis(FieldUtils.safeAdd(getEndMillis(), -durationMillis));
}
//-----------------------------------------------------------------------
/**
* Sets the period of this time interval, preserving the start instant
* and using the ISOChronology in the default zone for calculations.
*
* @param period new period for 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 void setPeriodAfterStart(ReadablePeriod period) {
if (period == null) {
setEndMillis(getStartMillis());
} else {
setEndMillis(getChronology().add(period, getStartMillis(), 1));
}
}
/**
* Sets the period of this time interval, preserving the end instant
* and using the ISOChronology in the default zone for calculations.
*
* @param period new period for interval, null means zero length
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the start instant exceeds the capacity of a long
*/
public void setPeriodBeforeEnd(ReadablePeriod period) {
if (period == null) {
setStartMillis(getEndMillis());
} else {
setStartMillis(getChronology().add(period, getEndMillis(), -1));
}
}
//-----------------------------------------------------------------------
/**
* Clone this object without having to cast the returned object.
*
* @return a clone of the this object.
*/
public MutableInterval copy() {
return (MutableInterval) clone();
}
/**
* Clone this object.
*
* @return a clone of this object.
*/
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException ex) {
throw new InternalError("Clone error");
}
}
}