1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.base;
17
18 import java.io.Serializable;
19
20 import org.joda.time.Chronology;
21 import org.joda.time.DateTimeUtils;
22 import org.joda.time.Duration;
23 import org.joda.time.DurationFieldType;
24 import org.joda.time.MutablePeriod;
25 import org.joda.time.PeriodType;
26 import org.joda.time.ReadWritablePeriod;
27 import org.joda.time.ReadableDuration;
28 import org.joda.time.ReadableInstant;
29 import org.joda.time.ReadablePartial;
30 import org.joda.time.ReadablePeriod;
31 import org.joda.time.chrono.ISOChronology;
32 import org.joda.time.convert.ConverterManager;
33 import org.joda.time.convert.PeriodConverter;
34 import org.joda.time.field.FieldUtils;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public abstract class BasePeriod
51 extends AbstractPeriod
52 implements ReadablePeriod, Serializable {
53
54
55 private static final long serialVersionUID = -2110953284060001145L;
56
57 private static final ReadablePeriod DUMMY_PERIOD = new AbstractPeriod() {
58 public int getValue(int index) {
59 return 0;
60 }
61 public PeriodType getPeriodType() {
62 return PeriodType.time();
63 }
64 };
65
66
67 private final PeriodType iType;
68
69 private final int[] iValues;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 protected BasePeriod(int years, int months, int weeks, int days,
88 int hours, int minutes, int seconds, int millis,
89 PeriodType type) {
90 super();
91 type = checkPeriodType(type);
92 iType = type;
93 iValues = setPeriodInternal(years, months, weeks, days, hours, minutes, seconds, millis);
94 }
95
96
97
98
99
100
101
102
103
104
105 protected BasePeriod(long startInstant, long endInstant, PeriodType type, Chronology chrono) {
106 super();
107 type = checkPeriodType(type);
108 chrono = DateTimeUtils.getChronology(chrono);
109 iType = type;
110 iValues = chrono.get(this, startInstant, endInstant);
111 }
112
113
114
115
116
117
118
119
120
121 protected BasePeriod(ReadableInstant startInstant, ReadableInstant endInstant, PeriodType type) {
122 super();
123 type = checkPeriodType(type);
124 if (startInstant == null && endInstant == null) {
125 iType = type;
126 iValues = new int[size()];
127 } else {
128 long startMillis = DateTimeUtils.getInstantMillis(startInstant);
129 long endMillis = DateTimeUtils.getInstantMillis(endInstant);
130 Chronology chrono = DateTimeUtils.getIntervalChronology(startInstant, endInstant);
131 iType = type;
132 iValues = chrono.get(this, startMillis, endMillis);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 protected BasePeriod(ReadablePartial start, ReadablePartial end, PeriodType type) {
155 super();
156 if (start == null || end == null) {
157 throw new IllegalArgumentException("ReadablePartial objects must not be null");
158 }
159 if (start instanceof BaseLocal && end instanceof BaseLocal && start.getClass() == end.getClass()) {
160
161 type = checkPeriodType(type);
162 long startMillis = ((BaseLocal) start).getLocalMillis();
163 long endMillis = ((BaseLocal) end).getLocalMillis();
164 Chronology chrono = start.getChronology();
165 chrono = DateTimeUtils.getChronology(chrono);
166 iType = type;
167 iValues = chrono.get(this, startMillis, endMillis);
168 } else {
169 if (start.size() != end.size()) {
170 throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
171 }
172 for (int i = 0, isize = start.size(); i < isize; i++) {
173 if (start.getFieldType(i) != end.getFieldType(i)) {
174 throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
175 }
176 }
177 if (DateTimeUtils.isContiguous(start) == false) {
178 throw new IllegalArgumentException("ReadablePartial objects must be contiguous");
179 }
180 iType = checkPeriodType(type);
181 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC();
182 iValues = chrono.get(this, chrono.set(start, 0L), chrono.set(end, 0L));
183 }
184 }
185
186
187
188
189
190
191
192
193 protected BasePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) {
194 super();
195 type = checkPeriodType(type);
196 long startMillis = DateTimeUtils.getInstantMillis(startInstant);
197 long durationMillis = DateTimeUtils.getDurationMillis(duration);
198 long endMillis = FieldUtils.safeAdd(startMillis, durationMillis);
199 Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
200 iType = type;
201 iValues = chrono.get(this, startMillis, endMillis);
202 }
203
204
205
206
207
208
209
210
211 protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
212 super();
213 type = checkPeriodType(type);
214 long durationMillis = DateTimeUtils.getDurationMillis(duration);
215 long endMillis = DateTimeUtils.getInstantMillis(endInstant);
216 long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis);
217 Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
218 iType = type;
219 iValues = chrono.get(this, startMillis, endMillis);
220 }
221
222
223
224
225
226
227
228
229
230 protected BasePeriod(long duration) {
231 super();
232
233
234
235 iType = PeriodType.standard();
236 int[] values = ISOChronology.getInstanceUTC().get(DUMMY_PERIOD, duration);
237 iValues = new int[8];
238 System.arraycopy(values, 0, iValues, 4, 4);
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253 protected BasePeriod(long duration, PeriodType type, Chronology chrono) {
254 super();
255 type = checkPeriodType(type);
256 chrono = DateTimeUtils.getChronology(chrono);
257 iType = type;
258 iValues = chrono.get(this, duration);
259 }
260
261
262
263
264
265
266
267
268
269
270 protected BasePeriod(Object period, PeriodType type, Chronology chrono) {
271 super();
272 PeriodConverter converter = ConverterManager.getInstance().getPeriodConverter(period);
273 type = (type == null ? converter.getPeriodType(period) : type);
274 type = checkPeriodType(type);
275 iType = type;
276 if (this instanceof ReadWritablePeriod) {
277 iValues = new int[size()];
278 chrono = DateTimeUtils.getChronology(chrono);
279 converter.setInto((ReadWritablePeriod) this, period, chrono);
280 } else {
281 iValues = new MutablePeriod(period, type, chrono).getValues();
282 }
283 }
284
285
286
287
288
289
290
291
292 protected BasePeriod(int[] values, PeriodType type) {
293 super();
294 iType = type;
295 iValues = values;
296 }
297
298
299
300
301
302
303
304
305
306
307 protected PeriodType checkPeriodType(PeriodType type) {
308 return DateTimeUtils.getPeriodType(type);
309 }
310
311
312
313
314
315
316
317 public PeriodType getPeriodType() {
318 return iType;
319 }
320
321
322
323
324
325
326
327
328 public int getValue(int index) {
329 return iValues[index];
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 public Duration toDurationFrom(ReadableInstant startInstant) {
350 long startMillis = DateTimeUtils.getInstantMillis(startInstant);
351 Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
352 long endMillis = chrono.add(this, startMillis, 1);
353 return new Duration(startMillis, endMillis);
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 public Duration toDurationTo(ReadableInstant endInstant) {
374 long endMillis = DateTimeUtils.getInstantMillis(endInstant);
375 Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
376 long startMillis = chrono.add(this, endMillis, -1);
377 return new Duration(startMillis, endMillis);
378 }
379
380
381
382
383
384
385
386
387
388
389 private void checkAndUpdate(DurationFieldType type, int[] values, int newValue) {
390 int index = indexOf(type);
391 if (index == -1) {
392 if (newValue != 0) {
393 throw new IllegalArgumentException(
394 "Period does not support field '" + type.getName() + "'");
395 }
396 } else {
397 values[index] = newValue;
398 }
399 }
400
401
402
403
404
405
406
407
408 protected void setPeriod(ReadablePeriod period) {
409 if (period == null) {
410 setValues(new int[size()]);
411 } else {
412 setPeriodInternal(period);
413 }
414 }
415
416
417
418
419 private void setPeriodInternal(ReadablePeriod period) {
420 int[] newValues = new int[size()];
421 for (int i = 0, isize = period.size(); i < isize; i++) {
422 DurationFieldType type = period.getFieldType(i);
423 int value = period.getValue(i);
424 checkAndUpdate(type, newValues, value);
425 }
426 setValues(newValues);
427 }
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442 protected void setPeriod(int years, int months, int weeks, int days,
443 int hours, int minutes, int seconds, int millis) {
444 int[] newValues = setPeriodInternal(years, months, weeks, days, hours, minutes, seconds, millis);
445 setValues(newValues);
446 }
447
448
449
450
451 private int[] setPeriodInternal(int years, int months, int weeks, int days,
452 int hours, int minutes, int seconds, int millis) {
453 int[] newValues = new int[size()];
454 checkAndUpdate(DurationFieldType.years(), newValues, years);
455 checkAndUpdate(DurationFieldType.months(), newValues, months);
456 checkAndUpdate(DurationFieldType.weeks(), newValues, weeks);
457 checkAndUpdate(DurationFieldType.days(), newValues, days);
458 checkAndUpdate(DurationFieldType.hours(), newValues, hours);
459 checkAndUpdate(DurationFieldType.minutes(), newValues, minutes);
460 checkAndUpdate(DurationFieldType.seconds(), newValues, seconds);
461 checkAndUpdate(DurationFieldType.millis(), newValues, millis);
462 return newValues;
463 }
464
465
466
467
468
469
470
471
472
473 protected void setField(DurationFieldType field, int value) {
474 setFieldInto(iValues, field, value);
475 }
476
477
478
479
480
481
482
483
484
485 protected void setFieldInto(int[] values, DurationFieldType field, int value) {
486 int index = indexOf(field);
487 if (index == -1) {
488 if (value != 0 || field == null) {
489 throw new IllegalArgumentException(
490 "Period does not support field '" + field + "'");
491 }
492 } else {
493 values[index] = value;
494 }
495 }
496
497
498
499
500
501
502
503
504 protected void addField(DurationFieldType field, int value) {
505 addFieldInto(iValues, field, value);
506 }
507
508
509
510
511
512
513
514
515
516 protected void addFieldInto(int[] values, DurationFieldType field, int value) {
517 int index = indexOf(field);
518 if (index == -1) {
519 if (value != 0 || field == null) {
520 throw new IllegalArgumentException(
521 "Period does not support field '" + field + "'");
522 }
523 } else {
524 values[index] = FieldUtils.safeAdd(values[index], value);
525 }
526 }
527
528
529
530
531
532
533
534 protected void mergePeriod(ReadablePeriod period) {
535 if (period != null) {
536 setValues(mergePeriodInto(getValues(), period));
537 }
538 }
539
540
541
542
543
544
545
546
547
548 protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
549 for (int i = 0, isize = period.size(); i < isize; i++) {
550 DurationFieldType type = period.getFieldType(i);
551 int value = period.getValue(i);
552 checkAndUpdate(type, values, value);
553 }
554 return values;
555 }
556
557
558
559
560
561
562
563 protected void addPeriod(ReadablePeriod period) {
564 if (period != null) {
565 setValues(addPeriodInto(getValues(), period));
566 }
567 }
568
569
570
571
572
573
574
575
576
577 protected int[] addPeriodInto(int[] values, ReadablePeriod period) {
578 for (int i = 0, isize = period.size(); i < isize; i++) {
579 DurationFieldType type = period.getFieldType(i);
580 int value = period.getValue(i);
581 if (value != 0) {
582 int index = indexOf(type);
583 if (index == -1) {
584 throw new IllegalArgumentException(
585 "Period does not support field '" + type.getName() + "'");
586 } else {
587 values[index] = FieldUtils.safeAdd(getValue(index), value);
588 }
589 }
590 }
591 return values;
592 }
593
594
595
596
597
598
599
600
601
602 protected void setValue(int index, int value) {
603 iValues[index] = value;
604 }
605
606
607
608
609
610
611
612
613
614
615 protected void setValues(int[] values) {
616 System.arraycopy(values, 0, iValues, 0, iValues.length);
617 }
618
619 }