001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------------
028: * RegularTimePeriod.java
029: * ----------------------
030: * (C) Copyright 2001-2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: RegularTimePeriod.java,v 1.6.2.2 2006/10/06 14:00:15 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 11-Oct-2001 : Version 1 (DG);
040: * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to
041: * evaluate with reference to a particular time zone (DG);
042: * 29-May-2002 : Implemented MonthConstants interface, so that these constants
043: * are conveniently available (DG);
044: * 10-Sep-2002 : Added getSerialIndex() method (DG);
045: * 10-Jan-2003 : Renamed TimePeriod --> RegularTimePeriod (DG);
046: * 13-Mar-2003 : Moved to com.jrefinery.data.time package (DG);
047: * 29-Apr-2004 : Changed getMiddleMillisecond() methods to fix bug 943985 (DG);
048: * 25-Nov-2004 : Added utility methods (DG);
049: * ------------- JFREECHART 1.0.x ---------------------------------------------
050: * 06-Oct-2006 : Deprecated the WORKING_CALENDAR field and several methods,
051: * added new peg() method (DG);
052: *
053: */
054:
055: package org.jfree.data.time;
056:
057: import java.lang.reflect.Constructor;
058: import java.util.Calendar;
059: import java.util.Date;
060: import java.util.TimeZone;
061:
062: import org.jfree.date.MonthConstants;
063:
064: /**
065: * An abstract class representing a unit of time. Convenient methods are
066: * provided for calculating the next and previous time periods. Conversion
067: * methods are defined that return the first and last milliseconds of the time
068: * period. The results from these methods are timezone dependent.
069: * <P>
070: * This class is immutable, and all subclasses should be immutable also.
071: */
072: public abstract class RegularTimePeriod implements TimePeriod,
073: Comparable, MonthConstants {
074:
075: /**
076: * Creates a time period that includes the specified millisecond, assuming
077: * the given time zone.
078: *
079: * @param c the time period class.
080: * @param millisecond the time.
081: * @param zone the time zone.
082: *
083: * @return The time period.
084: */
085: public static RegularTimePeriod createInstance(Class c,
086: Date millisecond, TimeZone zone) {
087: RegularTimePeriod result = null;
088: try {
089: Constructor constructor = c
090: .getDeclaredConstructor(new Class[] { Date.class,
091: TimeZone.class });
092: result = (RegularTimePeriod) constructor
093: .newInstance(new Object[] { millisecond, zone });
094: } catch (Exception e) {
095: // do nothing, so null is returned
096: }
097: return result;
098: }
099:
100: /**
101: * Returns a subclass of {@link RegularTimePeriod} that is smaller than
102: * the specified class.
103: *
104: * @param c a subclass of {@link RegularTimePeriod}.
105: *
106: * @return A class.
107: */
108: public static Class downsize(Class c) {
109: if (c.equals(Year.class)) {
110: return Quarter.class;
111: } else if (c.equals(Quarter.class)) {
112: return Month.class;
113: } else if (c.equals(Month.class)) {
114: return Day.class;
115: } else if (c.equals(Day.class)) {
116: return Hour.class;
117: } else if (c.equals(Hour.class)) {
118: return Minute.class;
119: } else if (c.equals(Minute.class)) {
120: return Second.class;
121: } else if (c.equals(Second.class)) {
122: return Millisecond.class;
123: } else {
124: return Millisecond.class;
125: }
126: }
127:
128: /**
129: * Returns the time period preceding this one, or <code>null</code> if some
130: * lower limit has been reached.
131: *
132: * @return The previous time period (possibly <code>null</code>).
133: */
134: public abstract RegularTimePeriod previous();
135:
136: /**
137: * Returns the time period following this one, or <code>null</code> if some
138: * limit has been reached.
139: *
140: * @return The next time period (possibly <code>null</code>).
141: */
142: public abstract RegularTimePeriod next();
143:
144: /**
145: * Returns a serial index number for the time unit.
146: *
147: * @return The serial index number.
148: */
149: public abstract long getSerialIndex();
150:
151: //////////////////////////////////////////////////////////////////////////
152:
153: /**
154: * The default time zone.
155: */
156: public static final TimeZone DEFAULT_TIME_ZONE = TimeZone
157: .getDefault();
158:
159: /**
160: * A working calendar (recycle to avoid unnecessary object creation).
161: *
162: * @deprecated This was a bad idea, don't use it!
163: */
164: public static final Calendar WORKING_CALENDAR = Calendar
165: .getInstance(DEFAULT_TIME_ZONE);
166:
167: /**
168: * Recalculates the start date/time and end date/time for this time period
169: * relative to the supplied calendar (which incorporates a time zone).
170: *
171: * @param calendar the calendar (<code>null</code> not permitted).
172: *
173: * @since 1.0.3
174: */
175: public abstract void peg(Calendar calendar);
176:
177: /**
178: * Returns the date/time that marks the start of the time period. This
179: * method returns a new <code>Date</code> instance every time it is called.
180: *
181: * @return The start date/time.
182: *
183: * @see #getFirstMillisecond()
184: */
185: public Date getStart() {
186: return new Date(getFirstMillisecond());
187: }
188:
189: /**
190: * Returns the date/time that marks the end of the time period. This
191: * method returns a new <code>Date</code> instance every time it is called.
192: *
193: * @return The end date/time.
194: *
195: * @see #getLastMillisecond()
196: */
197: public Date getEnd() {
198: return new Date(getLastMillisecond());
199: }
200:
201: /**
202: * Returns the first millisecond of the time period. This will be
203: * determined relative to the time zone specified in the constructor, or
204: * in the calendar instance passed in the most recent call to the
205: * {@link #peg(Calendar)} method.
206: *
207: * @return The first millisecond of the time period.
208: *
209: * @see #getLastMillisecond()
210: */
211: public abstract long getFirstMillisecond();
212:
213: /**
214: * Returns the first millisecond of the time period, evaluated within a
215: * specific time zone.
216: *
217: * @param zone the time zone (<code>null</code> not permitted).
218: *
219: * @return The first millisecond of the time period.
220: *
221: * @deprecated As of 1.0.3, you should avoid using this method (it creates
222: * a new Calendar instance every time it is called). You are advised
223: * to call {@link #getFirstMillisecond(Calendar)} instead.
224: *
225: * @see #getLastMillisecond(TimeZone)
226: */
227: public long getFirstMillisecond(TimeZone zone) {
228: Calendar calendar = Calendar.getInstance(zone);
229: return getFirstMillisecond(calendar);
230: }
231:
232: /**
233: * Returns the first millisecond of the time period, evaluated using the
234: * supplied calendar (which incorporates a timezone).
235: *
236: * @param calendar the calendar (<code>null</code> not permitted).
237: *
238: * @return The first millisecond of the time period.
239: *
240: * @throws NullPointerException if <code>calendar,/code> is
241: * </code>null</code>.
242: *
243: * @see #getLastMillisecond(Calendar)
244: */
245: public abstract long getFirstMillisecond(Calendar calendar);
246:
247: /**
248: * Returns the last millisecond of the time period. This will be
249: * determined relative to the time zone specified in the constructor, or
250: * in the calendar instance passed in the most recent call to the
251: * {@link #peg(Calendar)} method.
252: *
253: * @return The last millisecond of the time period.
254: *
255: * @see #getFirstMillisecond()
256: */
257: public abstract long getLastMillisecond();
258:
259: /**
260: * Returns the last millisecond of the time period, evaluated within a
261: * specific time zone.
262: *
263: * @param zone the time zone (<code>null</code> not permitted).
264: *
265: * @return The last millisecond of the time period.
266: *
267: * @deprecated As of 1.0.3, you should avoid using this method (it creates
268: * a new Calendar instance every time it is called). You are advised
269: * to call {@link #getLastMillisecond(Calendar)} instead.
270: *
271: * @see #getFirstMillisecond(TimeZone)
272: */
273: public long getLastMillisecond(TimeZone zone) {
274: Calendar calendar = Calendar.getInstance(zone);
275: return getLastMillisecond(calendar);
276: }
277:
278: /**
279: * Returns the last millisecond of the time period, evaluated using the
280: * supplied calendar (which incorporates a timezone).
281: *
282: * @param calendar the calendar (<code>null</code> not permitted).
283: *
284: * @return The last millisecond of the time period.
285: *
286: * @see #getFirstMillisecond(Calendar)
287: */
288: public abstract long getLastMillisecond(Calendar calendar);
289:
290: /**
291: * Returns the millisecond closest to the middle of the time period.
292: *
293: * @return The middle millisecond.
294: */
295: public long getMiddleMillisecond() {
296: long m1 = getFirstMillisecond();
297: long m2 = getLastMillisecond();
298: return m1 + (m2 - m1) / 2;
299: }
300:
301: /**
302: * Returns the millisecond closest to the middle of the time period,
303: * evaluated within a specific time zone.
304: *
305: * @param zone the time zone (<code>null</code> not permitted).
306: *
307: * @return The middle millisecond.
308: *
309: * @deprecated As of 1.0.3, you should avoid using this method (it creates
310: * a new Calendar instance every time it is called). You are advised
311: * to call {@link #getMiddleMillisecond(Calendar)} instead.
312: */
313: public long getMiddleMillisecond(TimeZone zone) {
314: Calendar calendar = Calendar.getInstance(zone);
315: long m1 = getFirstMillisecond(calendar);
316: long m2 = getLastMillisecond(calendar);
317: return m1 + (m2 - m1) / 2;
318: }
319:
320: /**
321: * Returns the millisecond closest to the middle of the time period,
322: * evaluated using the supplied calendar (which incorporates a timezone).
323: *
324: * @param calendar the calendar.
325: *
326: * @return The middle millisecond.
327: */
328: public long getMiddleMillisecond(Calendar calendar) {
329: long m1 = getFirstMillisecond(calendar);
330: long m2 = getLastMillisecond(calendar);
331: return m1 + (m2 - m1) / 2;
332: }
333:
334: /**
335: * Returns a string representation of the time period.
336: *
337: * @return The string.
338: */
339: public String toString() {
340: return String.valueOf(getStart());
341: }
342:
343: }
|