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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ----------------------
  25. * RegularTimePeriod.java
  26. * ----------------------
  27. * (C) Copyright 2001-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: RegularTimePeriod.java,v 1.5 2005/01/14 17:29:50 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 11-Oct-2001 : Version 1 (DG);
  37. * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to evaluate with reference
  38. * to a particular time zone (DG);
  39. * 29-May-2002 : Implemented MonthConstants interface, so that these constants are conveniently
  40. * available (DG);
  41. * 10-Sep-2002 : Added getSerialIndex() method (DG);
  42. * 10-Jan-2003 : Renamed TimePeriod --> RegularTimePeriod (DG);
  43. * 13-Mar-2003 : Moved to com.jrefinery.data.time package (DG);
  44. * 29-Apr-2004 : Changed getMiddleMillisecond() methods to fix bug 943985 (DG);
  45. * 25-Nov-2004 : Added utility methods (DG);
  46. *
  47. */
  48. package org.jfree.data.time;
  49. import java.lang.reflect.Constructor;
  50. import java.util.Calendar;
  51. import java.util.Date;
  52. import java.util.TimeZone;
  53. import org.jfree.date.MonthConstants;
  54. /**
  55. * An abstract class representing a unit of time.
  56. * <p>
  57. * Convenient methods are provided for calculating the next and previous time periods.
  58. * <p>
  59. * Conversion methods are defined that return the first and last milliseconds of the time period.
  60. * The results from these methods are timezone dependent.
  61. * <P>
  62. * This class is immutable, and all subclasses should be immutable also.
  63. */
  64. public abstract class RegularTimePeriod implements TimePeriod, Comparable, MonthConstants {
  65. /**
  66. * Creates a time period that includes the specified millisecond, assuming the given time
  67. * zone.
  68. *
  69. * @param c the time period class.
  70. * @param millisecond the time.
  71. * @param zone the time zone.
  72. *
  73. * @return The time period.
  74. */
  75. public static RegularTimePeriod createInstance(Class c, Date millisecond, TimeZone zone) {
  76. RegularTimePeriod result = null;
  77. try {
  78. Constructor constructor = c.getDeclaredConstructor(
  79. new Class[] {Date.class, TimeZone.class}
  80. );
  81. result = (RegularTimePeriod) constructor.newInstance(new Object[] {millisecond, zone});
  82. }
  83. catch (Exception e) {
  84. // do nothing, so null is returned
  85. }
  86. return result;
  87. }
  88. /**
  89. * Returns a subclass of {@link RegularTimePeriod} that is smaller than
  90. * the specified class.
  91. *
  92. * @param c a subclass of {@link RegularTimePeriod}.
  93. *
  94. * @return A class.
  95. */
  96. public static Class downsize(Class c) {
  97. if (c.equals(Year.class)) {
  98. return Quarter.class;
  99. }
  100. else if (c.equals(Quarter.class)) {
  101. return Month.class;
  102. }
  103. else if (c.equals(Month.class)) {
  104. return Day.class;
  105. }
  106. else if (c.equals(Day.class)) {
  107. return Hour.class;
  108. }
  109. else if (c.equals(Hour.class)) {
  110. return Minute.class;
  111. }
  112. else if (c.equals(Minute.class)) {
  113. return Second.class;
  114. }
  115. else if (c.equals(Second.class)) {
  116. return Millisecond.class;
  117. }
  118. else {
  119. return Millisecond.class;
  120. }
  121. }
  122. /**
  123. * Returns the time period preceding this one, or <code>null</code> if some lower limit
  124. * has been reached.
  125. *
  126. * @return The previous time period (possibly <code>null</code>).
  127. */
  128. public abstract RegularTimePeriod previous();
  129. /**
  130. * Returns the time period following this one, or <code>null</code> if some limit has
  131. * been reached.
  132. *
  133. * @return The next time period (possibly <code>null</code>).
  134. */
  135. public abstract RegularTimePeriod next();
  136. /**
  137. * Returns a serial index number for the time unit.
  138. *
  139. * @return The serial index number.
  140. */
  141. public abstract long getSerialIndex();
  142. //////////////////////////////////////////////////////////////////////////
  143. /** The default time zone. */
  144. public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getDefault();
  145. /** A working calendar (recycle to avoid unnecessary object creation). */
  146. public static final Calendar WORKING_CALENDAR = Calendar.getInstance(DEFAULT_TIME_ZONE);
  147. /**
  148. * Returns the date/time that marks the start of the time period.
  149. *
  150. * @return The start date/time.
  151. */
  152. public Date getStart() {
  153. return new Date(getFirstMillisecond());
  154. }
  155. /**
  156. * Returns the date/time that marks the end of the time period.
  157. *
  158. * @return The end date/time.
  159. */
  160. public Date getEnd() {
  161. return new Date(getLastMillisecond());
  162. }
  163. /**
  164. * Returns the first millisecond of the time period, evaluated in the default time zone.
  165. *
  166. * @return The first millisecond of the time period.
  167. */
  168. public long getFirstMillisecond() {
  169. return getFirstMillisecond(DEFAULT_TIME_ZONE);
  170. }
  171. /**
  172. * Returns the first millisecond of the time period, evaluated within a specific time zone.
  173. *
  174. * @param zone the time zone.
  175. *
  176. * @return The first millisecond of the time period.
  177. */
  178. public long getFirstMillisecond(TimeZone zone) {
  179. WORKING_CALENDAR.setTimeZone(zone);
  180. return getFirstMillisecond(WORKING_CALENDAR);
  181. }
  182. /**
  183. * Returns the first millisecond of the time period, evaluated using the supplied calendar
  184. * (which incorporates a timezone).
  185. *
  186. * @param calendar the calendar.
  187. *
  188. * @return The first millisecond of the time period.
  189. */
  190. public abstract long getFirstMillisecond(Calendar calendar);
  191. /**
  192. * Returns the last millisecond of the time period, evaluated in the default time zone.
  193. *
  194. * @return The last millisecond of the time period.
  195. */
  196. public long getLastMillisecond() {
  197. return getLastMillisecond(DEFAULT_TIME_ZONE);
  198. }
  199. /**
  200. * Returns the last millisecond of the time period, evaluated within a specific time zone.
  201. *
  202. * @param zone the time zone.
  203. *
  204. * @return The last millisecond of the time period.
  205. */
  206. public long getLastMillisecond(TimeZone zone) {
  207. WORKING_CALENDAR.setTimeZone(zone);
  208. return getLastMillisecond(WORKING_CALENDAR);
  209. }
  210. /**
  211. * Returns the last millisecond of the time period, evaluated using the supplied calendar
  212. * (which incorporates a timezone).
  213. *
  214. * @param calendar the calendar.
  215. *
  216. * @return The last millisecond of the time period.
  217. */
  218. public abstract long getLastMillisecond(Calendar calendar);
  219. /**
  220. * Returns the millisecond closest to the middle of the time period,
  221. * evaluated in the default time zone.
  222. *
  223. * @return The middle millisecond.
  224. */
  225. public long getMiddleMillisecond() {
  226. long m1 = getFirstMillisecond();
  227. long m2 = getLastMillisecond();
  228. return m1 + (m2 - m1) / 2;
  229. }
  230. /**
  231. * Returns the millisecond closest to the middle of the time period,
  232. * evaluated within a specific time zone.
  233. *
  234. * @param zone the time zone.
  235. *
  236. * @return The middle millisecond.
  237. */
  238. public long getMiddleMillisecond(TimeZone zone) {
  239. long m1 = getFirstMillisecond(zone);
  240. long m2 = getLastMillisecond(zone);
  241. return m1 + (m2 - m1) / 2;
  242. }
  243. /**
  244. * Returns the millisecond closest to the middle of the time period,
  245. * evaluated using the supplied calendar (which incorporates a timezone).
  246. *
  247. * @param calendar the calendar.
  248. *
  249. * @return The middle millisecond.
  250. */
  251. public long getMiddleMillisecond(Calendar calendar) {
  252. long m1 = getFirstMillisecond(calendar);
  253. long m2 = getLastMillisecond(calendar);
  254. return m1 + (m2 - m1) / 2;
  255. }
  256. /**
  257. * Returns a string representation of the time period.
  258. *
  259. * @return The string.
  260. */
  261. public String toString() {
  262. return String.valueOf(getStart());
  263. }
  264. }