KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > DateTickUnit


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  * DateTickUnit.java
28  * -----------------
29  * (C) Copyright 2000-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: DateTickUnit.java,v 1.7 2005/05/19 13:58:11 mungady Exp $
35  *
36  * Changes (from 8-Nov-2002)
37  * --------------------------
38  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
39  * 27-Nov-2002 : Added IllegalArgumentException to getMillisecondCount()
40  * method (DG);
41  * 26-Mar-2003 : Implemented Serializable (DG);
42  * 12-Nov-2003 : Added roll fields that can improve the labelling on segmented
43  * date axes (DG);
44  * 03-Dec-2003 : DateFormat constructor argument is now filled with an default
45  * if null (TM);
46  * 07-Dec-2003 : Fixed bug (null pointer exception) in constructor (DG);
47  *
48  */

49
50 package org.jfree.chart.axis;
51
52 import java.io.Serializable JavaDoc;
53 import java.text.DateFormat JavaDoc;
54 import java.util.Calendar JavaDoc;
55 import java.util.Date JavaDoc;
56
57 import org.jfree.util.ObjectUtilities;
58
59 /**
60  * A tick unit for use by subclasses of {@link DateAxis}.
61  * <p>
62  * Instances of this class are immutable.
63  */

64 public class DateTickUnit extends TickUnit implements Serializable JavaDoc {
65
66     /** For serialization. */
67     private static final long serialVersionUID = -7289292157229621901L;
68     
69     /** A constant for years. */
70     public static final int YEAR = 0;
71
72     /** A constant for months. */
73     public static final int MONTH = 1;
74
75     /** A constant for days. */
76     public static final int DAY = 2;
77
78     /** A constant for hours. */
79     public static final int HOUR = 3;
80
81     /** A constant for minutes. */
82     public static final int MINUTE = 4;
83
84     /** A constant for seconds. */
85     public static final int SECOND = 5;
86
87     /** A constant for milliseconds. */
88     public static final int MILLISECOND = 6;
89
90     /** The unit. */
91     private int unit;
92
93     /** The unit count. */
94     private int count;
95
96     /** The roll unit. */
97     private int rollUnit;
98
99     /** The roll count. */
100     private int rollCount;
101
102     /** The date formatter. */
103     private DateFormat JavaDoc formatter;
104
105     /**
106      * Creates a new date tick unit. The dates will be formatted using a
107      * SHORT format for the default locale.
108      *
109      * @param unit the unit.
110      * @param count the unit count.
111      */

112     public DateTickUnit(int unit, int count) {
113         this(unit, count, null);
114     }
115
116     /**
117      * Creates a new date tick unit. You can specify the units using one of
118      * the constants YEAR, MONTH, DAY, HOUR, MINUTE, SECOND or MILLISECOND.
119      * In addition, you can specify a unit count, and a date format.
120      *
121      * @param unit the unit.
122      * @param count the unit count.
123      * @param formatter the date formatter (defaults to DateFormat.SHORT).
124      */

125     public DateTickUnit(int unit, int count, DateFormat JavaDoc formatter) {
126
127         this(unit, count, unit, count, formatter);
128
129     }
130
131     /**
132      * Creates a new unit.
133      *
134      * @param unit the unit.
135      * @param count the count.
136      * @param rollUnit the roll unit.
137      * @param rollCount the roll count.
138      * @param formatter the date formatter (defaults to DateFormat.SHORT).
139      */

140     public DateTickUnit(int unit, int count, int rollUnit, int rollCount,
141                         DateFormat JavaDoc formatter) {
142         super(DateTickUnit.getMillisecondCount(unit, count));
143         this.unit = unit;
144         this.count = count;
145         this.rollUnit = rollUnit;
146         this.rollCount = rollCount;
147         this.formatter = formatter;
148         if (formatter == null) {
149             this.formatter = DateFormat.getDateInstance(DateFormat.SHORT);
150         }
151     }
152
153     /**
154      * Returns the date unit. This will be one of the constants
155      * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>,
156      * <code>HOUR</code>, <code>MINUTE</code>, <code>SECOND</code> or
157      * <code>MILLISECOND</code>, defined by this class. Note that these
158      * constants do NOT correspond to those defined in Java's
159      * <code>Calendar</code> class.
160      *
161      * @return The date unit.
162      */

163     public int getUnit() {
164         return this.unit;
165     }
166
167     /**
168      * Returns the unit count.
169      *
170      * @return The unit count.
171      */

172     public int getCount() {
173         return this.count;
174     }
175
176     /**
177      * Returns the roll unit. This is the amount by which the tick advances if
178      * it is "hidden" when displayed on a segmented date axis. Typically the
179      * roll will be smaller than the regular tick unit (for example, a 7 day
180      * tick unit might use a 1 day roll).
181      *
182      * @return The roll unit.
183      */

184     public int getRollUnit() {
185         return this.rollUnit;
186     }
187
188     /**
189      * Returns the roll count.
190      *
191      * @return The roll count.
192      */

193     public int getRollCount() {
194         return this.rollCount;
195     }
196
197     /**
198      * Formats a value.
199      *
200      * @param milliseconds date in milliseconds since 01-01-1970.
201      *
202      * @return The formatted date.
203      */

204     public String JavaDoc valueToString(double milliseconds) {
205         return this.formatter.format(new Date JavaDoc((long) milliseconds));
206     }
207
208     /**
209      * Formats a date using the tick unit's formatter.
210      *
211      * @param date the date.
212      *
213      * @return The formatted date.
214      */

215     public String JavaDoc dateToString(Date JavaDoc date) {
216         return this.formatter.format(date);
217     }
218
219     /**
220      * Calculates a new date by adding this unit to the base date.
221      *
222      * @param base the base date.
223      *
224      * @return A new date one unit after the base date.
225      */

226     public Date JavaDoc addToDate(Date JavaDoc base) {
227
228         Calendar JavaDoc calendar = Calendar.getInstance();
229         calendar.setTime(base);
230         calendar.add(getCalendarField(this.unit), this.count);
231         return calendar.getTime();
232
233     }
234
235     /**
236      * Rolls the date forward by the amount specified by the roll unit and
237      * count.
238      *
239      * @param base the base date.
240
241      * @return The rolled date.
242      */

243     public Date JavaDoc rollDate(Date JavaDoc base) {
244         Calendar JavaDoc calendar = Calendar.getInstance();
245         calendar.setTime(base);
246         calendar.add(getCalendarField(this.rollUnit), this.rollCount);
247         return calendar.getTime();
248     }
249
250     /**
251      * Returns a field code that can be used with the <code>Calendar</code>
252      * class.
253      *
254      * @return The field code.
255      */

256     public int getCalendarField() {
257         return getCalendarField(this.unit);
258     }
259
260     /**
261      * Returns a field code (that can be used with the Calendar class) for a
262      * given 'unit' code. The 'unit' is one of: {@link #YEAR}, {@link #MONTH},
263      * {@link #DAY}, {@link #HOUR}, {@link #MINUTE}, {@link #SECOND} and
264      * {@link #MILLISECOND}.
265      *
266      * @param tickUnit the unit.
267      *
268      * @return The field code.
269      */

270     private int getCalendarField(int tickUnit) {
271
272         switch (tickUnit) {
273             case (YEAR):
274                 return Calendar.YEAR;
275             case (MONTH):
276                 return Calendar.MONTH;
277             case (DAY):
278                 return Calendar.DATE;
279             case (HOUR):
280                 return Calendar.HOUR_OF_DAY;
281             case (MINUTE):
282                 return Calendar.MINUTE;
283             case (SECOND):
284                 return Calendar.SECOND;
285             case (MILLISECOND):
286                 return Calendar.MILLISECOND;
287             default:
288                 return Calendar.MILLISECOND;
289         }
290
291     }
292
293     /**
294      * Returns the (approximate) number of milliseconds for the given unit and
295      * unit count.
296      * <P>
297      * This value is an approximation some of the time (e.g. months are
298      * assumed to have 31 days) but this shouldn't matter.
299      *
300      * @param unit the unit.
301      * @param count the unit count.
302      *
303      * @return The number of milliseconds.
304      */

305     private static long getMillisecondCount(int unit, int count) {
306
307         switch (unit) {
308             case (YEAR):
309                 return (365L * 24L * 60L * 60L * 1000L) * count;
310             case (MONTH):
311                 return (31L * 24L * 60L * 60L * 1000L) * count;
312             case (DAY):
313                 return (24L * 60L * 60L * 1000L) * count;
314             case (HOUR):
315                 return (60L * 60L * 1000L) * count;
316             case (MINUTE):
317                 return (60L * 1000L) * count;
318             case (SECOND):
319                 return 1000L * count;
320             case (MILLISECOND):
321                 return count;
322             default:
323                 throw new IllegalArgumentException JavaDoc(
324                     "DateTickUnit.getMillisecondCount() : unit must "
325                     + "be one of the constants YEAR, MONTH, DAY, HOUR, MINUTE, "
326                     + "SECOND or MILLISECOND defined in the DateTickUnit "
327                     + "class. Do *not* use the constants defined in "
328                     + "java.util.Calendar."
329                 );
330         }
331
332     }
333
334     /**
335      * Tests this unit for equality with another object.
336      *
337      * @param obj the object (<code>null</code> permitted).
338      *
339      * @return <code>true</code> or <code>false</code>.
340      */

341     public boolean equals(Object JavaDoc obj) {
342         if (obj == this) {
343             return true;
344         }
345         if (!(obj instanceof DateTickUnit)) {
346             return false;
347         }
348         if (!super.equals(obj)) {
349             return false;
350         }
351         DateTickUnit that = (DateTickUnit) obj;
352         if (this.unit != that.unit) {
353             return false;
354         }
355         if (this.count != that.count) {
356             return false;
357         }
358         if (!ObjectUtilities.equal(this.formatter, that.formatter)) {
359             return false;
360         }
361         return true;
362     }
363     
364     /**
365      * Returns a hash code for this object.
366      *
367      * @return A hash code.
368      */

369     public int hashCode() {
370         int result = 19;
371         result = 37 * result + this.unit;
372         result = 37 * result + this.count;
373         result = 37 * result + this.formatter.hashCode();
374         return result;
375     }
376
377 }
378
Popular Tags