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. * Year.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: Year.java,v 1.4 2005/01/14 17:29:49 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 11-Oct-2001 : Version 1 (DG);
  37. * 14-Nov-2001 : Override for toString() method (DG);
  38. * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG);
  39. * 29-Jan-2002 : Worked on parseYear(...) method (DG);
  40. * 14-Feb-2002 : Fixed bug in Year(Date) constructor (DG);
  41. * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to evaluate with reference
  42. * to a particular time zone (DG);
  43. * 19-Mar-2002 : Changed API for TimePeriod classes (DG);
  44. * 10-Sep-2002 : Added getSerialIndex() method (DG);
  45. * 04-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  46. * 10-Jan-2003 : Changed base class and method names (DG);
  47. * 05-Mar-2003 : Fixed bug in getFirstMillisecond(...) picked up in JUnit tests (DG);
  48. * 13-Mar-2003 : Moved to com.jrefinery.data.time package, and implemented Serializable (DG);
  49. * 21-Oct-2003 : Added hashCode() method (DG);
  50. *
  51. */
  52. package org.jfree.data.time;
  53. import java.io.Serializable;
  54. import java.util.Calendar;
  55. import java.util.Date;
  56. import java.util.TimeZone;
  57. import org.jfree.date.MonthConstants;
  58. import org.jfree.date.SerialDate;
  59. /**
  60. * Represents a year in the range 1900 to 9999.
  61. * <P>
  62. * This class is immutable, which is a requirement for all {@link RegularTimePeriod} subclasses.
  63. */
  64. public class Year extends RegularTimePeriod implements Serializable {
  65. /** The year. */
  66. private int year;
  67. /**
  68. * Creates a new <code>Year</code>, based on the current system date/time.
  69. */
  70. public Year() {
  71. this(new Date());
  72. }
  73. /**
  74. * Creates a time period representing a single year.
  75. *
  76. * @param year the year.
  77. */
  78. public Year(int year) {
  79. // check arguments...
  80. if ((year < SerialDate.MINIMUM_YEAR_SUPPORTED)
  81. || (year > SerialDate.MAXIMUM_YEAR_SUPPORTED)) {
  82. throw new IllegalArgumentException(
  83. "Year constructor: year (" + year + ") outside valid range.");
  84. }
  85. // initialise...
  86. this.year = year;
  87. }
  88. /**
  89. * Creates a new <code>Year</code>, based on a particular instant in time, using the
  90. * default time zone.
  91. *
  92. * @param time the time.
  93. */
  94. public Year(Date time) {
  95. this(time, RegularTimePeriod.DEFAULT_TIME_ZONE);
  96. }
  97. /**
  98. * Constructs a year, based on a particular instant in time and a time zone.
  99. *
  100. * @param time the time.
  101. * @param zone the time zone.
  102. */
  103. public Year(Date time, TimeZone zone) {
  104. Calendar calendar = Calendar.getInstance(zone);
  105. calendar.setTime(time);
  106. this.year = calendar.get(Calendar.YEAR);
  107. }
  108. /**
  109. * Returns the year.
  110. *
  111. * @return The year.
  112. */
  113. public int getYear() {
  114. return this.year;
  115. }
  116. /**
  117. * Returns the year preceding this one.
  118. *
  119. * @return The year preceding this one (or null if the current year is 1900).
  120. */
  121. public RegularTimePeriod previous() {
  122. if (this.year > SerialDate.MINIMUM_YEAR_SUPPORTED) {
  123. return new Year(this.year - 1);
  124. }
  125. else {
  126. return null;
  127. }
  128. }
  129. /**
  130. * Returns the year following this one.
  131. *
  132. * @return The year following this one (or <code>null</code> if the current year is 9999).
  133. */
  134. public RegularTimePeriod next() {
  135. if (this.year < SerialDate.MAXIMUM_YEAR_SUPPORTED) {
  136. return new Year(this.year + 1);
  137. }
  138. else {
  139. return null;
  140. }
  141. }
  142. /**
  143. * Returns a serial index number for the year.
  144. * <P>
  145. * The implementation simply returns the year number (e.g. 2002).
  146. *
  147. * @return The serial index number.
  148. */
  149. public long getSerialIndex() {
  150. return this.year;
  151. }
  152. /**
  153. * Returns the first millisecond of the year, evaluated using the supplied
  154. * calendar (which determines the time zone).
  155. *
  156. * @param calendar the calendar.
  157. *
  158. * @return The first millisecond of the year.
  159. */
  160. public long getFirstMillisecond(Calendar calendar) {
  161. Day jan1 = new Day(1, MonthConstants.JANUARY, this.year);
  162. return jan1.getFirstMillisecond(calendar);
  163. }
  164. /**
  165. * Returns the last millisecond of the year, evaluated using the supplied
  166. * calendar (which determines the time zone).
  167. *
  168. * @param calendar the calendar.
  169. *
  170. * @return the last millisecond of the year.
  171. */
  172. public long getLastMillisecond(Calendar calendar) {
  173. Day dec31 = new Day(31, MonthConstants.DECEMBER, this.year);
  174. return dec31.getLastMillisecond(calendar);
  175. }
  176. /**
  177. * Tests the equality of this <code>Year</code> object to an arbitrary object. Returns
  178. * <code>true</code> if the target is a <code>Year</code> instance representing the same
  179. * year as this object. In all other cases, returns <code>false</code>.
  180. *
  181. * @param object the object.
  182. *
  183. * @return <code>true</code> if the year of this and the object are the same.
  184. */
  185. public boolean equals(Object object) {
  186. if (object != null) {
  187. if (object instanceof Year) {
  188. Year target = (Year) object;
  189. return (this.year == target.getYear());
  190. }
  191. else {
  192. return false;
  193. }
  194. }
  195. else {
  196. return false;
  197. }
  198. }
  199. /**
  200. * Returns a hash code for this object instance.
  201. * <p>
  202. * The approach described by Joshua Bloch in "Effective Java" has been used here:
  203. * <p>
  204. * <code>http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf</code>
  205. *
  206. * @return A hash code.
  207. */
  208. public int hashCode() {
  209. int result = 17;
  210. int c = this.year ^ (this.year >>> 32);
  211. result = 37 * result + c;
  212. return result;
  213. }
  214. /**
  215. * Returns an integer indicating the order of this <code>Year</code> object relative to
  216. * the specified object:
  217. *
  218. * negative == before, zero == same, positive == after.
  219. *
  220. * @param o1 the object to compare.
  221. *
  222. * @return negative == before, zero == same, positive == after.
  223. */
  224. public int compareTo(Object o1) {
  225. int result;
  226. // CASE 1 : Comparing to another Year object
  227. // -----------------------------------------
  228. if (o1 instanceof Year) {
  229. Year y = (Year) o1;
  230. result = this.year - y.getYear();
  231. }
  232. // CASE 2 : Comparing to another TimePeriod object
  233. // -----------------------------------------------
  234. else if (o1 instanceof RegularTimePeriod) {
  235. // more difficult case - evaluate later...
  236. result = 0;
  237. }
  238. // CASE 3 : Comparing to a non-TimePeriod object
  239. // ---------------------------------------------
  240. else {
  241. // consider time periods to be ordered after general objects
  242. result = 1;
  243. }
  244. return result;
  245. }
  246. /**
  247. * Returns a string representing the year..
  248. *
  249. * @return A string representing the year.
  250. */
  251. public String toString() {
  252. return Integer.toString(this.year);
  253. }
  254. /**
  255. * Parses the string argument as a year.
  256. * <P>
  257. * The string format is YYYY.
  258. *
  259. * @param s a string representing the year.
  260. *
  261. * @return <code>null</code> if the string is not parseable, the year otherwise.
  262. */
  263. public static Year parseYear(String s) {
  264. // parse the string...
  265. int y;
  266. try {
  267. y = Integer.parseInt(s.trim());
  268. }
  269. catch (NumberFormatException e) {
  270. throw new TimePeriodFormatException("Year.parseYear(string): cannot parse string.");
  271. }
  272. // create the year...
  273. try {
  274. return new Year(y);
  275. }
  276. catch (IllegalArgumentException e) {
  277. throw new TimePeriodFormatException("Year outside valid range.");
  278. }
  279. }
  280. }