KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > base > AbstractPeriod


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.base;
17
18 import org.joda.time.DurationFieldType;
19 import org.joda.time.MutablePeriod;
20 import org.joda.time.Period;
21 import org.joda.time.ReadablePeriod;
22 import org.joda.time.format.ISOPeriodFormat;
23
24 /**
25  * AbstractPeriod provides the common behaviour for period classes.
26  * <p>
27  * This class should generally not be used directly by API users. The
28  * {@link ReadablePeriod} interface should be used when different
29  * kinds of periods are to be referenced.
30  * <p>
31  * AbstractPeriod subclasses may be mutable and not thread-safe.
32  *
33  * @author Brian S O'Neill
34  * @author Stephen Colebourne
35  * @since 1.0
36  */

37 public abstract class AbstractPeriod implements ReadablePeriod {
38
39     /**
40      * Constructor.
41      */

42     protected AbstractPeriod() {
43         super();
44     }
45
46     //-----------------------------------------------------------------------
47
/**
48      * Gets an array of the field types that this period supports.
49      * <p>
50      * The fields are returned largest to smallest, for example Hours, Minutes, Seconds.
51      *
52      * @return the fields supported in an array that may be altered, largest to smallest
53      */

54     public DurationFieldType[] getFieldTypes() {
55         DurationFieldType[] result = new DurationFieldType[size()];
56         for (int i = 0; i < result.length; i++) {
57             result[i] = getFieldType(i);
58         }
59         return result;
60     }
61
62     /**
63      * Gets an array of the value of each of the fields that this period supports.
64      * <p>
65      * The fields are returned largest to smallest, for example Hours, Minutes, Seconds.
66      * Each value corresponds to the same array index as <code>getFields()</code>
67      *
68      * @return the current values of each field in an array that may be altered, largest to smallest
69      */

70     public int[] getValues() {
71         int[] result = new int[size()];
72         for (int i = 0; i < result.length; i++) {
73             result[i] = getValue(i);
74         }
75         return result;
76     }
77
78     //-----------------------------------------------------------------------
79
/**
80      * Gets the value of one of the fields.
81      * <p>
82      * If the field type specified is not supported by the period then zero
83      * is returned.
84      *
85      * @param type the field type to query, null returns zero
86      * @return the value of that field, zero if field not supported
87      */

88     public int get(DurationFieldType type) {
89         int index = indexOf(type);
90         if (index == -1) {
91             return 0;
92         }
93         return getValue(index);
94     }
95
96     /**
97      * Checks whether the field specified is supported by this period.
98      *
99      * @param type the type to check, may be null which returns false
100      * @return true if the field is supported
101      */

102     public boolean isSupported(DurationFieldType type) {
103         return getPeriodType().isSupported(type);
104     }
105
106     /**
107      * Gets the index of the field in this period.
108      *
109      * @param type the type to check, may be null which returns -1
110      * @return the index of -1 if not supported
111      */

112     public int indexOf(DurationFieldType type) {
113         return getPeriodType().indexOf(type);
114     }
115
116     //-----------------------------------------------------------------------
117
/**
118      * Get this period as an immutable <code>Period</code> object.
119      *
120      * @return a Period using the same field set and values
121      */

122     public Period toPeriod() {
123         return new Period(this);
124     }
125
126     /**
127      * Get this object as a <code>MutablePeriod</code>.
128      * <p>
129      * This will always return a new <code>MutablePeriod</code> with the same fields.
130      *
131      * @return a MutablePeriod using the same field set and values
132      */

133     public MutablePeriod toMutablePeriod() {
134         return new MutablePeriod(this);
135     }
136
137     //-----------------------------------------------------------------------
138
/**
139      * Compares this object with the specified object for equality based
140      * on the value of each field. All ReadablePeriod instances are accepted.
141      * <p>
142      * Note that a period of 1 day is not equal to a period of 24 hours,
143      * nor is 1 hour equal to 60 minutes. Only periods with the same amount
144      * in each field are equal.
145      * <p>
146      * This is because periods represent an abstracted definition of a time
147      * period (eg. a day may not actually be 24 hours, it might be 23 or 25
148      * at daylight savings boundary).
149      * <p>
150      * To compare the actual duration of two periods, convert both to
151      * {@link org.joda.time.Duration Duration}s, an operation that emphasises
152      * that the result may differ according to the date you choose.
153      *
154      * @param period a readable period to check against
155      * @return true if all the field values are equal, false if
156      * not or the period is null or of an incorrect type
157      */

158     public boolean equals(Object JavaDoc period) {
159         if (this == period) {
160             return true;
161         }
162         if (period instanceof ReadablePeriod == false) {
163             return false;
164         }
165         ReadablePeriod other = (ReadablePeriod) period;
166         if (size() != other.size()) {
167             return false;
168         }
169         for (int i = 0, isize = size(); i < isize; i++) {
170             if (getValue(i) != other.getValue(i) || getFieldType(i) != other.getFieldType(i)) {
171                 return false;
172             }
173         }
174         return true;
175     }
176
177     /**
178      * Gets a hash code for the period as defined by ReadablePeriod.
179      *
180      * @return a hash code
181      */

182     public int hashCode() {
183         int total = 17;
184         for (int i = 0, isize = size(); i < isize; i++) {
185             total = 27 * total + getValue(i);
186             total = 27 * total + getFieldType(i).hashCode();
187         }
188         return total;
189     }
190
191     //-----------------------------------------------------------------------
192
/**
193      * Gets the value as a String in the ISO8601 duration format.
194      * <p>
195      * For example, "P6H3M7S" represents 6 hours, 3 minutes, 7 seconds.
196      * <p>
197      * For more control over the output, see
198      * {@link org.joda.time.format.PeriodFormatterBuilder PeriodFormatterBuilder}.
199      *
200      * @return the value as an ISO8601 string
201      */

202     public String JavaDoc toString() {
203         return ISOPeriodFormat.standard().print(this);
204     }
205
206 }
207
Popular Tags