KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > Quarter


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------
27  * Quarter.java
28  * ------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: Quarter.java,v 1.6 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-Oct-2001 : Version 1 (DG);
39  * 18-Dec-2001 : Changed order of parameters in constructor (DG);
40  * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG);
41  * 29-Jan-2002 : Added a new method parseQuarter(String) (DG);
42  * 14-Feb-2002 : Fixed bug in Quarter(Date) constructor (DG);
43  * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to
44  * evaluate with reference to a particular time zone (DG);
45  * 19-Mar-2002 : Changed API for TimePeriod classes (DG);
46  * 24-Jun-2002 : Removed main method (just test code) (DG);
47  * 10-Sep-2002 : Added getSerialIndex() method (DG);
48  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
49  * 10-Jan-2003 : Changed base class and method names (DG);
50  * 13-Mar-2003 : Moved to com.jrefinery.data.time package, and implemented
51  * Serializable (DG);
52  * 21-Oct-2003 : Added hashCode() method (DG);
53  *
54  */

55
56 package org.jfree.data.time;
57
58 import java.io.Serializable JavaDoc;
59 import java.util.Calendar JavaDoc;
60 import java.util.Date JavaDoc;
61 import java.util.TimeZone JavaDoc;
62
63 import org.jfree.date.MonthConstants;
64 import org.jfree.date.SerialDate;
65
66 /**
67  * Defines a quarter (in a given year). The range supported is Q1 1900 to
68  * Q4 9999. This class is immutable, which is a requirement for all
69  * {@link RegularTimePeriod} subclasses.
70  */

71 public class Quarter extends RegularTimePeriod implements Serializable JavaDoc {
72
73     /** For serialization. */
74     private static final long serialVersionUID = 3810061714380888671L;
75     
76     /** Constant for quarter 1. */
77     public static final int FIRST_QUARTER = 1;
78
79     /** Constant for quarter 4. */
80     public static final int LAST_QUARTER = 4;
81
82     /** The first month in each quarter. */
83     public static final int[] FIRST_MONTH_IN_QUARTER = {
84         0, MonthConstants.JANUARY, MonthConstants.APRIL, MonthConstants.JULY,
85         MonthConstants.OCTOBER
86     };
87
88     /** The last month in each quarter. */
89     public static final int[] LAST_MONTH_IN_QUARTER = {
90         0, MonthConstants.MARCH, MonthConstants.JUNE, MonthConstants.SEPTEMBER,
91         MonthConstants.DECEMBER
92     };
93
94     /** The year in which the quarter falls. */
95     private Year year;
96
97     /** The quarter (1-4). */
98     private int quarter;
99
100     /**
101      * Constructs a new Quarter, based on the current system date/time.
102      */

103     public Quarter() {
104         this(new Date JavaDoc());
105     }
106
107     /**
108      * Constructs a new quarter.
109      *
110      * @param year the year (1900 to 9999).
111      * @param quarter the quarter (1 to 4).
112      */

113     public Quarter(int quarter, int year) {
114         this(quarter, new Year(year));
115     }
116
117     /**
118      * Constructs a new quarter.
119      *
120      * @param quarter the quarter (1 to 4).
121      * @param year the year (1900 to 9999).
122      */

123     public Quarter(int quarter, Year year) {
124         if ((quarter < FIRST_QUARTER) && (quarter > LAST_QUARTER)) {
125             throw new IllegalArgumentException JavaDoc("Quarter outside valid range.");
126         }
127         this.year = year;
128         this.quarter = quarter;
129     }
130
131     /**
132      * Constructs a new Quarter, based on a date/time and the default time zone.
133      *
134      * @param time the date/time.
135      */

136     public Quarter(Date JavaDoc time) {
137         this(time, RegularTimePeriod.DEFAULT_TIME_ZONE);
138     }
139
140     /**
141      * Constructs a Quarter, based on a date/time and time zone.
142      *
143      * @param time the date/time.
144      * @param zone the zone.
145      */

146     public Quarter(Date JavaDoc time, TimeZone JavaDoc zone) {
147
148         Calendar JavaDoc calendar = Calendar.getInstance(zone);
149         calendar.setTime(time);
150         int month = calendar.get(Calendar.MONTH) + 1;
151         this.quarter = SerialDate.monthCodeToQuarter(month);
152         this.year = new Year(calendar.get(Calendar.YEAR));
153
154     }
155
156     /**
157      * Returns the quarter.
158      *
159      * @return The quarter.
160      */

161     public int getQuarter() {
162         return this.quarter;
163     }
164
165     /**
166      * Returns the year.
167      *
168      * @return The year.
169      */

170     public Year getYear() {
171         return this.year;
172     }
173
174     /**
175      * Returns the quarter preceding this one.
176      *
177      * @return The quarter preceding this one (or null if this is Q1 1900).
178      */

179     public RegularTimePeriod previous() {
180
181         Quarter result;
182         if (this.quarter > FIRST_QUARTER) {
183             result = new Quarter(this.quarter - 1, this.year);
184         }
185         else {
186             Year prevYear = (Year) this.year.previous();
187             if (prevYear != null) {
188                 result = new Quarter(LAST_QUARTER, prevYear);
189             }
190             else {
191                 result = null;
192             }
193         }
194         return result;
195
196     }
197
198     /**
199      * Returns the quarter following this one.
200      *
201      * @return The quarter following this one (or null if this is Q4 9999).
202      */

203     public RegularTimePeriod next() {
204
205         Quarter result;
206         if (this.quarter < LAST_QUARTER) {
207             result = new Quarter(this.quarter + 1, this.year);
208         }
209         else {
210             Year nextYear = (Year) this.year.next();
211             if (nextYear != null) {
212                 result = new Quarter(FIRST_QUARTER, nextYear);
213             }
214             else {
215                 result = null;
216             }
217         }
218         return result;
219
220     }
221
222     /**
223      * Returns a serial index number for the quarter.
224      *
225      * @return The serial index number.
226      */

227     public long getSerialIndex() {
228         return this.year.getYear() * 4L + this.quarter;
229     }
230
231     /**
232      * Tests the equality of this Quarter object to an arbitrary object.
233      * Returns true if the target is a Quarter instance representing the same
234      * quarter as this object. In all other cases, returns false.
235      *
236      * @param obj the object.
237      *
238      * @return <code>true</code> if quarter and year of this and the object are
239      * the same.
240      */

241     public boolean equals(Object JavaDoc obj) {
242
243         if (obj != null) {
244             if (obj instanceof Quarter) {
245                 Quarter target = (Quarter) obj;
246                 return (
247                     (this.quarter == target.getQuarter())
248                     && (this.year.equals(target.getYear()))
249                 );
250             }
251             else {
252                 return false;
253             }
254         }
255         else {
256             return false;
257         }
258
259     }
260
261     /**
262      * Returns a hash code for this object instance. The approach described by
263      * Joshua Bloch in "Effective Java" has been used here:
264      * <p>
265      * <code>http://developer.java.sun.com/developer/Books/effectivejava
266      * /Chapter3.pdf</code>
267      *
268      * @return A hash code.
269      */

270     public int hashCode() {
271         int result = 17;
272         result = 37 * result + this.quarter;
273         result = 37 * result + this.year.hashCode();
274         return result;
275     }
276
277     /**
278      * Returns an integer indicating the order of this Quarter object relative
279      * to the specified object:
280      *
281      * negative == before, zero == same, positive == after.
282      *
283      * @param o1 the object to compare
284      *
285      * @return negative == before, zero == same, positive == after.
286      */

287     public int compareTo(Object JavaDoc o1) {
288
289         int result;
290
291         // CASE 1 : Comparing to another Quarter object
292
// --------------------------------------------
293
if (o1 instanceof Quarter) {
294             Quarter q = (Quarter) o1;
295             result = this.year.getYear() - q.getYear().getYear();
296             if (result == 0) {
297                 result = this.quarter - q.getQuarter();
298             }
299         }
300
301         // CASE 2 : Comparing to another TimePeriod object
302
// -----------------------------------------------
303
else if (o1 instanceof RegularTimePeriod) {
304             // more difficult case - evaluate later...
305
result = 0;
306         }
307
308         // CASE 3 : Comparing to a non-TimePeriod object
309
// ---------------------------------------------
310
else {
311             // consider time periods to be ordered after general objects
312
result = 1;
313         }
314
315         return result;
316
317     }
318
319     /**
320      * Returns a string representing the quarter (e.g. "Q1/2002").
321      *
322      * @return A string representing the quarter.
323      */

324     public String JavaDoc toString() {
325         return "Q" + this.quarter + "/" + this.year;
326     }
327
328     /**
329      * Returns the first millisecond in the Quarter, evaluated using the
330      * supplied calendar (which determines the time zone).
331      *
332      * @param calendar the calendar.
333      *
334      * @return The first millisecond in the Quarter.
335      */

336     public long getFirstMillisecond(Calendar JavaDoc calendar) {
337
338         int month = Quarter.FIRST_MONTH_IN_QUARTER[this.quarter];
339         Day first = new Day(1, month, this.year.getYear());
340         return first.getFirstMillisecond(calendar);
341
342     }
343
344     /**
345      * Returns the last millisecond of the Quarter, evaluated using the
346      * supplied calendar (which determines the time zone).
347      *
348      * @param calendar the calendar.
349      *
350      * @return The last millisecond of the Quarter.
351      */

352     public long getLastMillisecond(Calendar JavaDoc calendar) {
353
354         int month = Quarter.LAST_MONTH_IN_QUARTER[this.quarter];
355         int eom = SerialDate.lastDayOfMonth(month, this.year.getYear());
356         Day last = new Day(eom, month, this.year.getYear());
357         return last.getLastMillisecond(calendar);
358
359     }
360
361     /**
362      * Parses the string argument as a quarter.
363      * <P>
364      * This method should accept the following formats: "YYYY-QN" and "QN-YYYY",
365      * where the "-" can be a space, a forward-slash (/), comma or a dash (-).
366      * @param s A string representing the quarter.
367      *
368      * @return The quarter.
369      */

370     public static Quarter parseQuarter(String JavaDoc s) {
371
372         // find the Q and the integer following it (remove both from the
373
// string)...
374
int i = s.indexOf("Q");
375         if (i == -1) {
376             throw new TimePeriodFormatException("Missing Q.");
377         }
378
379         if (i == s.length() - 1) {
380             throw new TimePeriodFormatException("Q found at end of string.");
381         }
382
383         String JavaDoc qstr = s.substring(i + 1, i + 2);
384         int quarter = Integer.parseInt(qstr);
385         String JavaDoc remaining = s.substring(0, i) + s.substring(i + 2, s.length());
386
387         // replace any / , or - with a space
388
remaining = remaining.replace('/', ' ');
389         remaining = remaining.replace(',', ' ');
390         remaining = remaining.replace('-', ' ');
391
392         // parse the string...
393
Year year = Year.parseYear(remaining.trim());
394         Quarter result = new Quarter(quarter, year);
395         return result;
396
397     }
398
399 }
400
Popular Tags