KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > MutablePeriod


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.joda.time.base.BasePeriod;
21 import org.joda.time.field.FieldUtils;
22
23 /**
24  * Standard mutable time period implementation.
25  * <p>
26  * A time period is divided into a number of fields, such as hours and seconds.
27  * Which fields are supported is defined by the PeriodType class.
28  * The default is the standard period type, which supports years, months, weeks, days,
29  * hours, minutes, seconds and millis.
30  * <p>
31  * When this time period is added to an instant, the effect is of adding each field in turn.
32  * As a result, this takes into account daylight savings time.
33  * Adding a time period of 1 day to the day before daylight savings starts will only add
34  * 23 hours rather than 24 to ensure that the time remains the same.
35  * If this is not the behaviour you want, then see {@link Duration}.
36  * <p>
37  * The definition of a period also affects the equals method. A period of 1
38  * day is not equal to a period of 24 hours, nor 1 hour equal to 60 minutes.
39  * This is because periods represent an abstracted definition of a time period
40  * (eg. a day may not actually be 24 hours, it might be 23 or 25 at daylight
41  * savings boundary). To compare the actual duration of two periods, convert
42  * both to durations using toDuration, an operation that emphasises that the
43  * result may differ according to the date you choose.
44  * <p>
45  * MutablePeriod is mutable and not thread-safe, unless concurrent threads
46  * are not invoking mutator methods.
47  *
48  * @author Brian S O'Neill
49  * @author Stephen Colebourne
50  * @since 1.0
51  * @see Period
52  */

53 public class MutablePeriod
54         extends BasePeriod
55         implements ReadWritablePeriod, Cloneable JavaDoc, Serializable JavaDoc {
56
57     /** Serialization version */
58     private static final long serialVersionUID = 3436451121567212165L;
59
60     /**
61      * Creates a zero-length period using the standard period type.
62      */

63     public MutablePeriod() {
64         super(0L, null, null);
65     }
66
67     /**
68      * Creates a zero-length period using the specified period type.
69      *
70      * @param type which set of fields this period supports
71      */

72     public MutablePeriod(PeriodType type) {
73         super(0L, type, null);
74     }
75
76     /**
77      * Create a period from a set of field values using the standard set of fields.
78      *
79      * @param hours amount of hours in this period
80      * @param minutes amount of minutes in this period
81      * @param seconds amount of seconds in this period
82      * @param millis amount of milliseconds in this period
83      */

84     public MutablePeriod(int hours, int minutes, int seconds, int millis) {
85         super(0, 0, 0, 0, hours, minutes, seconds, millis, PeriodType.standard());
86     }
87
88     /**
89      * Create a period from a set of field values using the standard set of fields.
90      *
91      * @param years amount of years in this period
92      * @param months amount of months in this period
93      * @param weeks amount of weeks in this period
94      * @param days amount of days in this period
95      * @param hours amount of hours in this period
96      * @param minutes amount of minutes in this period
97      * @param seconds amount of seconds in this period
98      * @param millis amount of milliseconds in this period
99      */

100     public MutablePeriod(int years, int months, int weeks, int days,
101                   int hours, int minutes, int seconds, int millis) {
102         super(years, months, weeks, days, hours, minutes, seconds, millis, PeriodType.standard());
103     }
104
105     /**
106      * Create a period from a set of field values.
107      *
108      * @param years amount of years in this period, which must be zero if unsupported
109      * @param months amount of months in this period, which must be zero if unsupported
110      * @param weeks amount of weeks in this period, which must be zero if unsupported
111      * @param days amount of days in this period, which must be zero if unsupported
112      * @param hours amount of hours in this period, which must be zero if unsupported
113      * @param minutes amount of minutes in this period, which must be zero if unsupported
114      * @param seconds amount of seconds in this period, which must be zero if unsupported
115      * @param millis amount of milliseconds in this period, which must be zero if unsupported
116      * @param type which set of fields this period supports, null means AllType
117      * @throws IllegalArgumentException if an unsupported field's value is non-zero
118      */

119     public MutablePeriod(int years, int months, int weeks, int days,
120                     int hours, int minutes, int seconds, int millis, PeriodType type) {
121         super(years, months, weeks, days, hours, minutes, seconds, millis, type);
122     }
123
124     /**
125      * Creates a period from the given millisecond duration using the standard
126      * set of fields.
127      * <p>
128      * Only precise fields in the period type will be used.
129      * For the standard period type this is the time fields only.
130      * Thus the year, month, week and day fields will not be populated.
131      * <p>
132      * If the duration is small, less than one day, then this method will perform
133      * as you might expect and split the fields evenly.
134      * <p>
135      * If the duration is larger than one day then all the remaining duration will
136      * be stored in the largest available precise field, hours in this case.
137      * <p>
138      * For example, a duration equal to (365 + 60 + 5) days will be converted to
139      * ((365 + 60 + 5) * 24) hours by this constructor.
140      * <p>
141      * For more control over the conversion process, you have two options:
142      * <ul>
143      * <li>convert the duration to an {@link Interval}, and from there obtain the period
144      * <li>specify a period type that contains precise definitions of the day and larger
145      * fields, such as the UTC or precise types.
146      * </ul>
147      *
148      * @param duration the duration, in milliseconds
149      */

150     public MutablePeriod(long duration) {
151         super(duration, null, null);
152     }
153
154     /**
155      * Creates a period from the given millisecond duration.
156      * <p>
157      * Only precise fields in the period type will be used.
158      * Imprecise fields will not be populated.
159      * <p>
160      * If the duration is small then this method will perform
161      * as you might expect and split the fields evenly.
162      * <p>
163      * If the duration is large then all the remaining duration will
164      * be stored in the largest available precise field.
165      * For details as to which fields are precise, review the period type javadoc.
166      *
167      * @param duration the duration, in milliseconds
168      * @param type which set of fields this period supports, null means standard
169      */

170     public MutablePeriod(long duration, PeriodType type) {
171         super(duration, type, null);
172     }
173
174     /**
175      * Creates a period from the given millisecond duration using the standard
176      * set of fields.
177      * <p>
178      * Only precise fields in the period type will be used.
179      * Imprecise fields will not be populated.
180      * <p>
181      * If the duration is small then this method will perform
182      * as you might expect and split the fields evenly.
183      * <p>
184      * If the duration is large then all the remaining duration will
185      * be stored in the largest available precise field.
186      * For details as to which fields are precise, review the period type javadoc.
187      *
188      * @param duration the duration, in milliseconds
189      * @param chronology the chronology to use to split the duration, null means ISO default
190      */

191     public MutablePeriod(long duration, Chronology chronology) {
192         super(duration, null, chronology);
193     }
194
195     /**
196      * Creates a period from the given millisecond duration.
197      * <p>
198      * Only precise fields in the period type will be used.
199      * Imprecise fields will not be populated.
200      * <p>
201      * If the duration is small then this method will perform
202      * as you might expect and split the fields evenly.
203      * <p>
204      * If the duration is large then all the remaining duration will
205      * be stored in the largest available precise field.
206      * For details as to which fields are precise, review the period type javadoc.
207      *
208      * @param duration the duration, in milliseconds
209      * @param type which set of fields this period supports, null means standard
210      * @param chronology the chronology to use to split the duration, null means ISO default
211      */

212     public MutablePeriod(long duration, PeriodType type, Chronology chronology) {
213         super(duration, type, chronology);
214     }
215
216     /**
217      * Creates a period from the given interval endpoints using the standard
218      * set of fields.
219      *
220      * @param startInstant interval start, in milliseconds
221      * @param endInstant interval end, in milliseconds
222      */

223     public MutablePeriod(long startInstant, long endInstant) {
224         super(startInstant, endInstant, null, null);
225     }
226
227     /**
228      * Creates a period from the given interval endpoints.
229      *
230      * @param startInstant interval start, in milliseconds
231      * @param endInstant interval end, in milliseconds
232      * @param type which set of fields this period supports, null means standard
233      */

234     public MutablePeriod(long startInstant, long endInstant, PeriodType type) {
235         super(startInstant, endInstant, type, null);
236     }
237
238     /**
239      * Creates a period from the given interval endpoints using the standard
240      * set of fields.
241      *
242      * @param startInstant interval start, in milliseconds
243      * @param endInstant interval end, in milliseconds
244      * @param chrono the chronology to use, null means ISO in default zone
245      */

246     public MutablePeriod(long startInstant, long endInstant, Chronology chrono) {
247         super(startInstant, endInstant, null, chrono);
248     }
249
250     /**
251      * Creates a period from the given interval endpoints.
252      *
253      * @param startInstant interval start, in milliseconds
254      * @param endInstant interval end, in milliseconds
255      * @param type which set of fields this period supports, null means standard
256      * @param chrono the chronology to use, null means ISO in default zone
257      */

258     public MutablePeriod(long startInstant, long endInstant, PeriodType type, Chronology chrono) {
259         super(startInstant, endInstant, type, chrono);
260     }
261
262     /**
263      * Creates a period from the given interval endpoints using the standard
264      * set of fields.
265      * <p>
266      * The chronology of the start instant is used, unless that is null when the
267      * chronology of the end instant is used instead.
268      *
269      * @param startInstant interval start, null means now
270      * @param endInstant interval end, null means now
271      */

272     public MutablePeriod(ReadableInstant startInstant, ReadableInstant endInstant) {
273         super(startInstant, endInstant, null);
274     }
275
276     /**
277      * Creates a period from the given interval endpoints.
278      * <p>
279      * The chronology of the start instant is used, unless that is null when the
280      * chronology of the end instant is used instead.
281      *
282      * @param startInstant interval start, null means now
283      * @param endInstant interval end, null means now
284      * @param type which set of fields this period supports, null means AllType
285      */

286     public MutablePeriod(ReadableInstant startInstant, ReadableInstant endInstant, PeriodType type) {
287         super(startInstant, endInstant, type);
288     }
289
290     /**
291      * Creates a period from the given start point and the duration.
292      *
293      * @param startInstant the interval start, null means now
294      * @param duration the duration of the interval, null means zero-length
295      */

296     public MutablePeriod(ReadableInstant startInstant, ReadableDuration duration) {
297         super(startInstant, duration, null);
298     }
299
300     /**
301      * Creates a period from the given start point and the duration.
302      *
303      * @param startInstant the interval start, null means now
304      * @param duration the duration of the interval, null means zero-length
305      * @param type which set of fields this period supports, null means standard
306      */

307     public MutablePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) {
308         super(startInstant, duration, type);
309     }
310
311     /**
312      * Creates a period from the given duration and end point.
313      *
314      * @param duration the duration of the interval, null means zero-length
315      * @param endInstant the interval end, null means now
316      */

317     public MutablePeriod(ReadableDuration duration, ReadableInstant endInstant) {
318         super(duration, endInstant, null);
319     }
320
321     /**
322      * Creates a period from the given duration and end point.
323      *
324      * @param duration the duration of the interval, null means zero-length
325      * @param endInstant the interval end, null means now
326      * @param type which set of fields this period supports, null means standard
327      */

328     public MutablePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
329         super(duration, endInstant, type);
330     }
331
332     /**
333      * Creates a period from the specified object using the
334      * {@link org.joda.time.convert.ConverterManager ConverterManager}.
335      *
336      * @param period period to convert
337      * @throws IllegalArgumentException if period is invalid
338      * @throws UnsupportedOperationException if an unsupported field's value is non-zero
339      */

340     public MutablePeriod(Object JavaDoc period) {
341         super(period, null, null);
342     }
343
344     /**
345      * Creates a period from the specified object using the
346      * {@link org.joda.time.convert.ConverterManager ConverterManager}.
347      *
348      * @param period period to convert
349      * @param type which set of fields this period supports, null means use converter
350      * @throws IllegalArgumentException if period is invalid
351      * @throws UnsupportedOperationException if an unsupported field's value is non-zero
352      */

353     public MutablePeriod(Object JavaDoc period, PeriodType type) {
354         super(period, type, null);
355     }
356
357     /**
358      * Creates a period from the specified object using the
359      * {@link org.joda.time.convert.ConverterManager ConverterManager}.
360      *
361      * @param period period to convert
362      * @param chrono the chronology to use, null means ISO in default zone
363      * @throws IllegalArgumentException if period is invalid
364      * @throws UnsupportedOperationException if an unsupported field's value is non-zero
365      */

366     public MutablePeriod(Object JavaDoc period, Chronology chrono) {
367         super(period, null, chrono);
368     }
369
370     /**
371      * Creates a period from the specified object using the
372      * {@link org.joda.time.convert.ConverterManager ConverterManager}.
373      *
374      * @param period period to convert
375      * @param type which set of fields this period supports, null means use converter
376      * @param chrono the chronology to use, null means ISO in default zone
377      * @throws IllegalArgumentException if period is invalid
378      * @throws UnsupportedOperationException if an unsupported field's value is non-zero
379      */

380     public MutablePeriod(Object JavaDoc period, PeriodType type, Chronology chrono) {
381         super(period, type, chrono);
382     }
383
384     //-----------------------------------------------------------------------
385
/**
386      * Clears the period, setting all values back to zero.
387      */

388     public void clear() {
389         super.setValues(new int[size()]);
390     }
391
392     /**
393      * Sets the value of one of the fields by index.
394      *
395      * @param index the field index
396      * @param value the new value for the field
397      * @throws IndexOutOfBoundsException if the index is invalid
398      */

399     public void setValue(int index, int value) {
400         super.setValue(index, value);
401     }
402
403     /**
404      * Sets the value of one of the fields.
405      * <p>
406      * The field type specified must be one of those that is supported by the period.
407      *
408      * @param field a DurationFieldType instance that is supported by this period, not null
409      * @param value the new value for the field
410      * @throws IllegalArgumentException if the field is null or not supported
411      */

412     public void set(DurationFieldType field, int value) {
413         super.setField(field, value);
414     }
415
416     /**
417      * Sets all the fields in one go from another ReadablePeriod.
418      *
419      * @param period the period to set, null means zero length period
420      * @throws IllegalArgumentException if an unsupported field's value is non-zero
421      */

422     public void setPeriod(ReadablePeriod period) {
423         super.setPeriod(period);
424     }
425
426     /**
427      * Sets all the fields in one go.
428      *
429      * @param years amount of years in this period, which must be zero if unsupported
430      * @param months amount of months in this period, which must be zero if unsupported
431      * @param weeks amount of weeks in this period, which must be zero if unsupported
432      * @param days amount of days in this period, which must be zero if unsupported
433      * @param hours amount of hours in this period, which must be zero if unsupported
434      * @param minutes amount of minutes in this period, which must be zero if unsupported
435      * @param seconds amount of seconds in this period, which must be zero if unsupported
436      * @param millis amount of milliseconds in this period, which must be zero if unsupported
437      * @throws IllegalArgumentException if an unsupported field's value is non-zero
438      */

439     public void setPeriod(int years, int months, int weeks, int days,
440                           int hours, int minutes, int seconds, int millis) {
441         super.setPeriod(years, months, weeks, days, hours, minutes, seconds, millis);
442     }
443
444     /**
445      * Sets all the fields in one go from an interval using the ISO chronology
446      * and dividing the fields using the period type.
447      *
448      * @param interval the interval to set, null means zero length
449      * @throws ArithmeticException if the set exceeds the capacity of the period
450      */

451     public void setPeriod(ReadableInterval interval) {
452         if (interval == null) {
453             setPeriod(0L);
454         } else {
455             Chronology chrono = DateTimeUtils.getChronology(interval.getChronology());
456             setPeriod(interval.getStartMillis(), interval.getEndMillis(), chrono);
457         }
458     }
459
460     /**
461      * Sets all the fields in one go from two instants representing an interval.
462      * <p>
463      * The chronology of the start instant is used, unless that is null when the
464      * chronology of the end instant is used instead.
465      *
466      * @param start the start instant, null means now
467      * @param end the end instant, null means now
468      * @throws ArithmeticException if the set exceeds the capacity of the period
469      */

470     public void setPeriod(ReadableInstant start, ReadableInstant end) {
471         if (start == end) {
472             setPeriod(0L);
473         } else {
474             long startMillis = DateTimeUtils.getInstantMillis(start);
475             long endMillis = DateTimeUtils.getInstantMillis(end);
476             Chronology chrono = DateTimeUtils.getIntervalChronology(start, end);
477             setPeriod(startMillis, endMillis, chrono);
478         }
479     }
480
481     /**
482      * Sets all the fields in one go from a millisecond interval using ISOChronology
483      * and dividing the fields using the period type.
484      *
485      * @param startInstant interval start, in milliseconds
486      * @param endInstant interval end, in milliseconds
487      * @throws ArithmeticException if the set exceeds the capacity of the period
488      */

489     public void setPeriod(long startInstant, long endInstant) {
490         setPeriod(startInstant, endInstant, null);
491     }
492
493     /**
494      * Sets all the fields in one go from a millisecond interval.
495      *
496      * @param startInstant interval start, in milliseconds
497      * @param endInstant interval end, in milliseconds
498      * @param chrono the chronology to use, not null
499      * @throws ArithmeticException if the set exceeds the capacity of the period
500      */

501     public void setPeriod(long startInstant, long endInstant, Chronology chrono) {
502         chrono = DateTimeUtils.getChronology(chrono);
503         setValues(chrono.get(this, startInstant, endInstant));
504     }
505
506     /**
507      * Sets all the fields in one go from a duration dividing the
508      * fields using the period type.
509      * <p>
510      * When dividing the duration, only precise fields in the period type will be used.
511      * For large durations, all the remaining duration will be stored in the largest
512      * available precise field.
513      *
514      * @param duration the duration to set, null means zero length
515      * @throws ArithmeticException if the set exceeds the capacity of the period
516      */

517     public void setPeriod(ReadableDuration duration) {
518         setPeriod(duration, null);
519     }
520
521     /**
522      * Sets all the fields in one go from a duration dividing the
523      * fields using the period type.
524      * <p>
525      * When dividing the duration, only precise fields in the period type will be used.
526      * For large durations, all the remaining duration will be stored in the largest
527      * available precise field.
528      *
529      * @param duration the duration to set, null means zero length
530      * @param chrono the chronology to use, null means ISO default
531      * @throws ArithmeticException if the set exceeds the capacity of the period
532      */

533     public void setPeriod(ReadableDuration duration, Chronology chrono) {
534         long durationMillis = DateTimeUtils.getDurationMillis(duration);
535         setPeriod(durationMillis, chrono);
536     }
537
538     /**
539      * Sets all the fields in one go from a millisecond duration dividing the
540      * fields using the period type.
541      * <p>
542      * When dividing the duration, only precise fields in the period type will be used.
543      * For large durations, all the remaining duration will be stored in the largest
544      * available precise field.
545      *
546      * @param duration the duration, in milliseconds
547      * @throws ArithmeticException if the set exceeds the capacity of the period
548      */

549     public void setPeriod(long duration) {
550         setPeriod(duration, null);
551     }
552
553     /**
554      * Sets all the fields in one go from a millisecond duration.
555      * <p>
556      * When dividing the duration, only precise fields in the period type will be used.
557      * For large durations, all the remaining duration will be stored in the largest
558      * available precise field.
559      *
560      * @param duration the duration, in milliseconds
561      * @param chrono the chronology to use, not null
562      * @throws ArithmeticException if the set exceeds the capacity of the period
563      */

564     public void setPeriod(long duration, Chronology chrono) {
565         chrono = DateTimeUtils.getChronology(chrono);
566         setValues(chrono.get(this, duration));
567     }
568
569     //-----------------------------------------------------------------------
570
/**
571      * Adds to the value of one of the fields.
572      * <p>
573      * The field type specified must be one of those that is supported by the period.
574      *
575      * @param field a DurationFieldType instance that is supported by this period, not null
576      * @param value the value to add to the field
577      * @throws IllegalArgumentException if the field is null or not supported
578      */

579     public void add(DurationFieldType field, int value) {
580         super.addField(field, value);
581     }
582
583     /**
584      * Adds a period to this one by adding each field in turn.
585      *
586      * @param period the period to add, null means add nothing
587      * @throws IllegalArgumentException if the period being added contains a field
588      * not supported by this period
589      * @throws ArithmeticException if the addition exceeds the capacity of the period
590      */

591     public void add(ReadablePeriod period) {
592         super.addPeriod(period);
593     }
594
595     /**
596      * Adds to each field of this period.
597      *
598      * @param years amount of years to add to this period, which must be zero if unsupported
599      * @param months amount of months to add to this period, which must be zero if unsupported
600      * @param weeks amount of weeks to add to this period, which must be zero if unsupported
601      * @param days amount of days to add to this period, which must be zero if unsupported
602      * @param hours amount of hours to add to this period, which must be zero if unsupported
603      * @param minutes amount of minutes to add to this period, which must be zero if unsupported
604      * @param seconds amount of seconds to add to this period, which must be zero if unsupported
605      * @param millis amount of milliseconds to add to this period, which must be zero if unsupported
606      * @throws IllegalArgumentException if the period being added contains a field
607      * not supported by this period
608      * @throws ArithmeticException if the addition exceeds the capacity of the period
609      */

610     public void add(int years, int months, int weeks, int days,
611                        int hours, int minutes, int seconds, int millis) {
612         setPeriod(
613             FieldUtils.safeAdd(getYears(), years),
614             FieldUtils.safeAdd(getMonths(), months),
615             FieldUtils.safeAdd(getWeeks(), weeks),
616             FieldUtils.safeAdd(getDays(), days),
617             FieldUtils.safeAdd(getHours(), hours),
618             FieldUtils.safeAdd(getMinutes(), minutes),
619             FieldUtils.safeAdd(getSeconds(), seconds),
620             FieldUtils.safeAdd(getMillis(), millis)
621         );
622     }
623
624     /**
625      * Adds an interval to this one by dividing the interval into
626      * fields and calling {@link #add(ReadablePeriod)}.
627      *
628      * @param interval the interval to add, null means add nothing
629      * @throws ArithmeticException if the addition exceeds the capacity of the period
630      */

631     public void add(ReadableInterval interval) {
632         if (interval != null) {
633             add(interval.toPeriod(getPeriodType()));
634         }
635     }
636
637     /**
638      * Adds a duration to this one by dividing the duration into
639      * fields and calling {@link #add(ReadablePeriod)}.
640      *
641      * @param duration the duration to add, null means add nothing
642      * @throws ArithmeticException if the addition exceeds the capacity of the period
643      */

644     public void add(ReadableDuration duration) {
645         if (duration != null) {
646             add(new Period(duration.getMillis(), getPeriodType()));
647         }
648     }
649
650     /**
651      * Adds a millisecond duration to this one by dividing the duration into
652      * fields and calling {@link #add(ReadablePeriod)}.
653      * <p>
654      * When dividing the duration, only precise fields in the period type will be used.
655      * For large durations, all the remaining duration will be stored in the largest
656      * available precise field.
657      *
658      * @param duration the duration, in milliseconds
659      * @throws ArithmeticException if the addition exceeds the capacity of the period
660      */

661     public void add(long duration) {
662         add(new Period(duration, getPeriodType()));
663     }
664
665     /**
666      * Adds a millisecond duration to this one by dividing the duration into
667      * fields and calling {@link #add(ReadablePeriod)}.
668      * <p>
669      * When dividing the duration, only precise fields in the period type will be used.
670      * For large durations, all the remaining duration will be stored in the largest
671      * available precise field.
672      *
673      * @param duration the duration, in milliseconds
674      * @param chrono the chronology to use, null means ISO default
675      * @throws ArithmeticException if the addition exceeds the capacity of the period
676      */

677     public void add(long duration, Chronology chrono) {
678         add(new Period(duration, getPeriodType(), chrono));
679     }
680
681     //-----------------------------------------------------------------------
682
/**
683      * Merges all the fields from the specified period into this one.
684      * <p>
685      * Fields that are not present in the specified period are left unaltered.
686      *
687      * @param period the period to set, null ignored
688      * @throws IllegalArgumentException if an unsupported field's value is non-zero
689      */

690     public void mergePeriod(ReadablePeriod period) {
691         super.mergePeriod(period);
692     }
693
694     //-----------------------------------------------------------------------
695
/**
696      * Gets the years field part of the period.
697      *
698      * @return the number of years in the period, zero if unsupported
699      */

700     public int getYears() {
701         return getPeriodType().getIndexedField(this, PeriodType.YEAR_INDEX);
702     }
703
704     /**
705      * Gets the months field part of the period.
706      *
707      * @return the number of months in the period, zero if unsupported
708      */

709     public int getMonths() {
710         return getPeriodType().getIndexedField(this, PeriodType.MONTH_INDEX);
711     }
712
713     /**
714      * Gets the weeks field part of the period.
715      *
716      * @return the number of weeks in the period, zero if unsupported
717      */

718     public int getWeeks() {
719         return getPeriodType().getIndexedField(this, PeriodType.WEEK_INDEX);
720     }
721
722     /**
723      * Gets the days field part of the period.
724      *
725      * @return the number of days in the period, zero if unsupported
726      */

727     public int getDays() {
728         return getPeriodType().getIndexedField(this, PeriodType.DAY_INDEX);
729     }
730
731     //-----------------------------------------------------------------------
732
/**
733      * Gets the hours field part of the period.
734      *
735      * @return the number of hours in the period, zero if unsupported
736      */

737     public int getHours() {
738         return getPeriodType().getIndexedField(this, PeriodType.HOUR_INDEX);
739     }
740
741     /**
742      * Gets the minutes field part of the period.
743      *
744      * @return the number of minutes in the period, zero if unsupported
745      */

746     public int getMinutes() {
747         return getPeriodType().getIndexedField(this, PeriodType.MINUTE_INDEX);
748     }
749
750     /**
751      * Gets the seconds field part of the period.
752      *
753      * @return the number of seconds in the period, zero if unsupported
754      */

755     public int getSeconds() {
756         return getPeriodType().getIndexedField(this, PeriodType.SECOND_INDEX);
757     }
758
759     /**
760      * Gets the millis field part of the period.
761      *
762      * @return the number of millis in the period, zero if unsupported
763      */

764     public int getMillis() {
765         return getPeriodType().getIndexedField(this, PeriodType.MILLI_INDEX);
766     }
767
768     //-----------------------------------------------------------------------
769
/**
770      * Sets the number of years of the period.
771      *
772      * @param years the number of years
773      * @throws IllegalArgumentException if field is not supported and the value is non-zero
774      */

775     public void setYears(int years) {
776         super.setField(DurationFieldType.years(), years);
777     }
778
779     /**
780      * Adds the specified years to the number of years in the period.
781      *
782      * @param years the number of years
783      * @throws IllegalArgumentException if field is not supported and the value is non-zero
784      * @throws ArithmeticException if the addition exceeds the capacity of the period
785      */

786     public void addYears(int years) {
787         super.addField(DurationFieldType.years(), years);
788     }
789
790     //-----------------------------------------------------------------------
791
/**
792      * Sets the number of months of the period.
793      *
794      * @param months the number of months
795      * @throws IllegalArgumentException if field is not supported and the value is non-zero
796      */

797     public void setMonths(int months) {
798         super.setField(DurationFieldType.months(), months);
799     }
800
801     /**
802      * Adds the specified months to the number of months in the period.
803      *
804      * @param months the number of months
805      * @throws IllegalArgumentException if field is not supported and the value is non-zero
806      * @throws ArithmeticException if the addition exceeds the capacity of the period
807      */

808     public void addMonths(int months) {
809         super.addField(DurationFieldType.months(), months);
810     }
811
812     //-----------------------------------------------------------------------
813
/**
814      * Sets the number of weeks of the period.
815      *
816      * @param weeks the number of weeks
817      * @throws IllegalArgumentException if field is not supported and the value is non-zero
818      */

819     public void setWeeks(int weeks) {
820         super.setField(DurationFieldType.weeks(), weeks);
821     }
822
823     /**
824      * Adds the specified weeks to the number of weeks in the period.
825      *
826      * @param weeks the number of weeks
827      * @throws IllegalArgumentException if field is not supported and the value is non-zero
828      * @throws ArithmeticException if the addition exceeds the capacity of the period
829      */

830     public void addWeeks(int weeks) {
831         super.addField(DurationFieldType.weeks(), weeks);
832     }
833
834     //-----------------------------------------------------------------------
835
/**
836      * Sets the number of days of the period.
837      *
838      * @param days the number of days
839      * @throws IllegalArgumentException if field is not supported and the value is non-zero
840      */

841     public void setDays(int days) {
842         super.setField(DurationFieldType.days(), days);
843     }
844
845     /**
846      * Adds the specified days to the number of days in the period.
847      *
848      * @param days the number of days
849      * @throws IllegalArgumentException if field is not supported and the value is non-zero
850      * @throws ArithmeticException if the addition exceeds the capacity of the period
851      */

852     public void addDays(int days) {
853         super.addField(DurationFieldType.days(), days);
854     }
855
856     //-----------------------------------------------------------------------
857
/**
858      * Sets the number of hours of the period.
859      *
860      * @param hours the number of hours
861      * @throws IllegalArgumentException if field is not supported and the value is non-zero
862      */

863     public void setHours(int hours) {
864         super.setField(DurationFieldType.hours(), hours);
865     }
866
867     /**
868      * Adds the specified hours to the number of hours in the period.
869      *
870      * @param hours the number of hours
871      * @throws IllegalArgumentException if field is not supported and the value is non-zero
872      * @throws ArithmeticException if the addition exceeds the capacity of the period
873      */

874     public void addHours(int hours) {
875         super.addField(DurationFieldType.hours(), hours);
876     }
877
878     //-----------------------------------------------------------------------
879
/**
880      * Sets the number of minutes of the period.
881      *
882      * @param minutes the number of minutes
883      * @throws IllegalArgumentException if field is not supported and the value is non-zero
884      */

885     public void setMinutes(int minutes) {
886         super.setField(DurationFieldType.minutes(), minutes);
887     }
888
889     /**
890      * Adds the specified minutes to the number of minutes in the period.
891      *
892      * @param minutes the number of minutes
893      * @throws IllegalArgumentException if field is not supported and the value is non-zero
894      * @throws ArithmeticException if the addition exceeds the capacity of the period
895      */

896     public void addMinutes(int minutes) {
897         super.addField(DurationFieldType.minutes(), minutes);
898     }
899
900     //-----------------------------------------------------------------------
901
/**
902      * Sets the number of seconds of the period.
903      *
904      * @param seconds the number of seconds
905      * @throws IllegalArgumentException if field is not supported and the value is non-zero
906      */

907     public void setSeconds(int seconds) {
908         super.setField(DurationFieldType.seconds(), seconds);
909     }
910
911     /**
912      * Adds the specified seconds to the number of seconds in the period.
913      *
914      * @param seconds the number of seconds
915      * @throws IllegalArgumentException if field is not supported and the value is non-zero
916      * @throws ArithmeticException if the addition exceeds the capacity of the period
917      */

918     public void addSeconds(int seconds) {
919         super.addField(DurationFieldType.seconds(), seconds);
920     }
921
922     //-----------------------------------------------------------------------
923
/**
924      * Sets the number of millis of the period.
925      *
926      * @param millis the number of millis
927      * @throws IllegalArgumentException if field is not supported and the value is non-zero
928      */

929     public void setMillis(int millis) {
930         super.setField(DurationFieldType.millis(), millis);
931     }
932
933     /**
934      * Adds the specified millis to the number of millis in the period.
935      *
936      * @param millis the number of millis
937      * @throws IllegalArgumentException if field is not supported and the value is non-zero
938      * @throws ArithmeticException if the addition exceeds the capacity of the period
939      */

940     public void addMillis(int millis) {
941         super.addField(DurationFieldType.millis(), millis);
942     }
943
944     // Misc
945
//-----------------------------------------------------------------------
946
/**
947      * Clone this object without having to cast the returned object.
948      *
949      * @return a clone of the this object.
950      */

951     public MutablePeriod copy() {
952         return (MutablePeriod) clone();
953     }
954
955     /**
956      * Clone this object.
957      *
958      * @return a clone of this object.
959      */

960     public Object JavaDoc clone() {
961         try {
962             return super.clone();
963         } catch (CloneNotSupportedException JavaDoc ex) {
964             throw new InternalError JavaDoc("Clone error");
965         }
966     }
967
968 }
969
Popular Tags