001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------- 028 * Year.java 029 * --------- 030 * (C) Copyright 2001-2012, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 11-Oct-2001 : Version 1 (DG); 038 * 14-Nov-2001 : Override for toString() method (DG); 039 * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG); 040 * 29-Jan-2002 : Worked on parseYear() method (DG); 041 * 14-Feb-2002 : Fixed bug in Year(Date) constructor (DG); 042 * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to 043 * evaluate with reference to a particular time zone (DG); 044 * 19-Mar-2002 : Changed API for TimePeriod classes (DG); 045 * 10-Sep-2002 : Added getSerialIndex() method (DG); 046 * 04-Oct-2002 : Fixed errors reported by Checkstyle (DG); 047 * 10-Jan-2003 : Changed base class and method names (DG); 048 * 05-Mar-2003 : Fixed bug in getFirstMillisecond() picked up in JUnit 049 * tests (DG); 050 * 13-Mar-2003 : Moved to com.jrefinery.data.time package, and implemented 051 * Serializable (DG); 052 * 21-Oct-2003 : Added hashCode() method (DG); 053 * ------------- JFREECHART 1.0.x --------------------------------------------- 054 * 05-Oct-2006 : Updated API docs (DG); 055 * 06-Oct-2006 : Refactored to cache first and last millisecond values (DG); 056 * 16-Sep-2008 : Extended range of valid years, and deprecated 057 * DEFAULT_TIME_ZONE (DG); 058 * 25-Nov-2008 : Added new constructor with Locale (DG); 059 * 05-Jul-2012 : Removed JRE 1.3.1 code (DG); 060 * 061 */ 062 063package org.jfree.data.time; 064 065import java.io.Serializable; 066import java.util.Calendar; 067import java.util.Date; 068import java.util.Locale; 069import java.util.TimeZone; 070 071/** 072 * Represents a year in the range -9999 to 9999. This class is immutable, 073 * which is a requirement for all {@link RegularTimePeriod} subclasses. 074 */ 075public class Year extends RegularTimePeriod implements Serializable { 076 077 /** 078 * The minimum year value. 079 * 080 * @since 1.0.11 081 */ 082 public static final int MINIMUM_YEAR = -9999; 083 084 /** 085 * The maximum year value. 086 * 087 * @since 1.0.11 088 */ 089 public static final int MAXIMUM_YEAR = 9999; 090 091 /** For serialization. */ 092 private static final long serialVersionUID = -7659990929736074836L; 093 094 /** The year. */ 095 private short year; 096 097 /** The first millisecond. */ 098 private long firstMillisecond; 099 100 /** The last millisecond. */ 101 private long lastMillisecond; 102 103 /** 104 * Creates a new <code>Year</code>, based on the current system date/time. 105 */ 106 public Year() { 107 this(new Date()); 108 } 109 110 /** 111 * Creates a time period representing a single year. 112 * 113 * @param year the year. 114 */ 115 public Year(int year) { 116 if ((year < Year.MINIMUM_YEAR) || (year > Year.MAXIMUM_YEAR)) { 117 throw new IllegalArgumentException( 118 "Year constructor: year (" + year + ") outside valid range."); 119 } 120 this.year = (short) year; 121 peg(Calendar.getInstance()); 122 } 123 124 /** 125 * Creates a new <code>Year</code>, based on a particular instant in time, 126 * using the default time zone. 127 * 128 * @param time the time (<code>null</code> not permitted). 129 * 130 * @see #Year(Date, TimeZone) 131 */ 132 public Year(Date time) { 133 this(time, TimeZone.getDefault()); 134 } 135 136 /** 137 * Constructs a year, based on a particular instant in time and a time zone. 138 * 139 * @param time the time (<code>null</code> not permitted). 140 * @param zone the time zone. 141 * 142 * @deprecated Since 1.0.12, use {@link #Year(Date, TimeZone, Locale)} 143 * instead. 144 */ 145 public Year(Date time, TimeZone zone) { 146 this(time, zone, Locale.getDefault()); 147 } 148 149 /** 150 * Creates a new <code>Year</code> instance, for the specified time zone 151 * and locale. 152 * 153 * @param time the current time (<code>null</code> not permitted). 154 * @param zone the time zone. 155 * @param locale the locale. 156 * 157 * @since 1.0.12 158 */ 159 public Year(Date time, TimeZone zone, Locale locale) { 160 Calendar calendar = Calendar.getInstance(zone, locale); 161 calendar.setTime(time); 162 this.year = (short) calendar.get(Calendar.YEAR); 163 peg(calendar); 164 } 165 166 /** 167 * Returns the year. 168 * 169 * @return The year. 170 */ 171 public int getYear() { 172 return this.year; 173 } 174 175 /** 176 * Returns the first millisecond of the year. This will be determined 177 * relative to the time zone specified in the constructor, or in the 178 * calendar instance passed in the most recent call to the 179 * {@link #peg(Calendar)} method. 180 * 181 * @return The first millisecond of the year. 182 * 183 * @see #getLastMillisecond() 184 */ 185 @Override 186 public long getFirstMillisecond() { 187 return this.firstMillisecond; 188 } 189 190 /** 191 * Returns the last millisecond of the year. This will be 192 * determined relative to the time zone specified in the constructor, or 193 * in the calendar instance passed in the most recent call to the 194 * {@link #peg(Calendar)} method. 195 * 196 * @return The last millisecond of the year. 197 * 198 * @see #getFirstMillisecond() 199 */ 200 @Override 201 public long getLastMillisecond() { 202 return this.lastMillisecond; 203 } 204 205 /** 206 * Recalculates the start date/time and end date/time for this time period 207 * relative to the supplied calendar (which incorporates a time zone). 208 * 209 * @param calendar the calendar (<code>null</code> not permitted). 210 * 211 * @since 1.0.3 212 */ 213 @Override 214 public void peg(Calendar calendar) { 215 this.firstMillisecond = getFirstMillisecond(calendar); 216 this.lastMillisecond = getLastMillisecond(calendar); 217 } 218 219 /** 220 * Returns the year preceding this one. 221 * 222 * @return The year preceding this one (or <code>null</code> if the 223 * current year is -9999). 224 */ 225 @Override 226 public RegularTimePeriod previous() { 227 if (this.year > Year.MINIMUM_YEAR) { 228 return new Year(this.year - 1); 229 } 230 else { 231 return null; 232 } 233 } 234 235 /** 236 * Returns the year following this one. 237 * 238 * @return The year following this one (or <code>null</code> if the current 239 * year is 9999). 240 */ 241 @Override 242 public RegularTimePeriod next() { 243 if (this.year < Year.MAXIMUM_YEAR) { 244 return new Year(this.year + 1); 245 } 246 else { 247 return null; 248 } 249 } 250 251 /** 252 * Returns a serial index number for the year. 253 * <P> 254 * The implementation simply returns the year number (e.g. 2002). 255 * 256 * @return The serial index number. 257 */ 258 @Override 259 public long getSerialIndex() { 260 return this.year; 261 } 262 263 /** 264 * Returns the first millisecond of the year, evaluated using the supplied 265 * calendar (which determines the time zone). 266 * 267 * @param calendar the calendar (<code>null</code> not permitted). 268 * 269 * @return The first millisecond of the year. 270 * 271 * @throws NullPointerException if <code>calendar</code> is 272 * <code>null</code>. 273 */ 274 @Override 275 public long getFirstMillisecond(Calendar calendar) { 276 calendar.set(this.year, Calendar.JANUARY, 1, 0, 0, 0); 277 calendar.set(Calendar.MILLISECOND, 0); 278 return calendar.getTimeInMillis(); 279 } 280 281 /** 282 * Returns the last millisecond of the year, evaluated using the supplied 283 * calendar (which determines the time zone). 284 * 285 * @param calendar the calendar (<code>null</code> not permitted). 286 * 287 * @return The last millisecond of the year. 288 * 289 * @throws NullPointerException if <code>calendar</code> is 290 * <code>null</code>. 291 */ 292 @Override 293 public long getLastMillisecond(Calendar calendar) { 294 calendar.set(this.year, Calendar.DECEMBER, 31, 23, 59, 59); 295 calendar.set(Calendar.MILLISECOND, 999); 296 return calendar.getTimeInMillis(); 297 } 298 299 /** 300 * Tests the equality of this <code>Year</code> object to an arbitrary 301 * object. Returns <code>true</code> if the target is a <code>Year</code> 302 * instance representing the same year as this object. In all other cases, 303 * returns <code>false</code>. 304 * 305 * @param obj the object (<code>null</code> permitted). 306 * 307 * @return <code>true</code> if the year of this and the object are the 308 * same. 309 */ 310 @Override 311 public boolean equals(Object obj) { 312 if (obj == this) { 313 return true; 314 } 315 if (!(obj instanceof Year)) { 316 return false; 317 } 318 Year that = (Year) obj; 319 return (this.year == that.year); 320 } 321 322 /** 323 * Returns a hash code for this object instance. The approach described by 324 * Joshua Bloch in "Effective Java" has been used here: 325 * <p> 326 * <code>http://developer.java.sun.com/developer/Books/effectivejava 327 * /Chapter3.pdf</code> 328 * 329 * @return A hash code. 330 */ 331 @Override 332 public int hashCode() { 333 int result = 17; 334 int c = this.year; 335 result = 37 * result + c; 336 return result; 337 } 338 339 /** 340 * Returns an integer indicating the order of this <code>Year</code> object 341 * relative to the specified object: 342 * 343 * negative == before, zero == same, positive == after. 344 * 345 * @param o1 the object to compare. 346 * 347 * @return negative == before, zero == same, positive == after. 348 */ 349 @Override 350 public int compareTo(Object o1) { 351 352 int result; 353 354 // CASE 1 : Comparing to another Year object 355 // ----------------------------------------- 356 if (o1 instanceof Year) { 357 Year y = (Year) o1; 358 result = this.year - y.getYear(); 359 } 360 361 // CASE 2 : Comparing to another TimePeriod object 362 // ----------------------------------------------- 363 else if (o1 instanceof RegularTimePeriod) { 364 // more difficult case - evaluate later... 365 result = 0; 366 } 367 368 // CASE 3 : Comparing to a non-TimePeriod object 369 // --------------------------------------------- 370 else { 371 // consider time periods to be ordered after general objects 372 result = 1; 373 } 374 375 return result; 376 377 } 378 379 /** 380 * Returns a string representing the year.. 381 * 382 * @return A string representing the year. 383 */ 384 @Override 385 public String toString() { 386 return Integer.toString(this.year); 387 } 388 389 /** 390 * Parses the string argument as a year. 391 * <P> 392 * The string format is YYYY. 393 * 394 * @param s a string representing the year. 395 * 396 * @return <code>null</code> if the string is not parseable, the year 397 * otherwise. 398 */ 399 public static Year parseYear(String s) { 400 401 // parse the string... 402 int y; 403 try { 404 y = Integer.parseInt(s.trim()); 405 } 406 catch (NumberFormatException e) { 407 throw new TimePeriodFormatException("Cannot parse string."); 408 } 409 410 // create the year... 411 try { 412 return new Year(y); 413 } 414 catch (IllegalArgumentException e) { 415 throw new TimePeriodFormatException("Year outside valid range."); 416 } 417 } 418 419}