Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2006, 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 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * --------- 28: * Week.java 29: * --------- 30: * (C) Copyright 2001-2004, by Object Refinery Limited and Contributors. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): Aimin Han; 34: * 35: * $Id: Week.java,v 1.7.2.2 2006/04/06 13:23:57 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 11-Oct-2001 : Version 1 (DG); 40: * 18-Dec-2001 : Changed order of parameters in constructor (DG); 41: * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG); 42: * 29-Jan-2002 : Worked on the parseWeek() method (DG); 43: * 13-Feb-2002 : Fixed bug in Week(Date) constructor (DG); 44: * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to 45: * evaluate with reference to a particular time zone (DG); 46: * 05-Apr-2002 : Reinstated this class to the JCommon library (DG); 47: * 24-Jun-2002 : Removed unnecessary main method (DG); 48: * 10-Sep-2002 : Added getSerialIndex() method (DG); 49: * 06-Oct-2002 : Fixed errors reported by Checkstyle (DG); 50: * 18-Oct-2002 : Changed to observe 52 or 53 weeks per year, consistent with 51: * GregorianCalendar. Thanks to Aimin Han for the code (DG); 52: * 02-Jan-2003 : Removed debug code (DG); 53: * 13-Mar-2003 : Moved to com.jrefinery.data.time package, and implemented 54: * Serializable (DG); 55: * 21-Oct-2003 : Added hashCode() method (DG); 56: * 24-May-2004 : Modified getFirstMillisecond() and getLastMillisecond() to 57: * take account of firstDayOfWeek setting in Java's Calendar 58: * class (DG); 59: * 30-Sep-2004 : Replaced getTime().getTime() with getTimeInMillis() (DG); 60: * 04-Nov-2004 : Reverted change of 30-Sep-2004, because it won't work for 61: * JDK 1.3 (DG); 62: * ------------- JFREECHART 1.0.0 --------------------------------------------- 63: * 06-Mar-2006 : Fix for bug 1448828, incorrect calculation of week and year 64: * for the first few days of some years (DG); 65: * 66: */ 67: 68: package org.jfree.data.time; 69: 70: import java.io.Serializable; 71: import java.util.Calendar; 72: import java.util.Date; 73: import java.util.TimeZone; 74: 75: /** 76: * A calendar week. All years are considered to have 53 weeks, numbered from 1 77: * to 53, although in many cases the 53rd week is empty. Most of the time, the 78: * 1st week of the year *begins* in the previous calendar year, but it always 79: * finishes in the current year (this behaviour matches the workings of the 80: * <code>GregorianCalendar</code> class). 81: * <P> 82: * This class is immutable, which is a requirement for all 83: * {@link RegularTimePeriod} subclasses. 84: */ 85: public class Week extends RegularTimePeriod implements Serializable { 86: 87: /** For serialization. */ 88: private static final long serialVersionUID = 1856387786939865061L; 89: 90: /** Constant for the first week in the year. */ 91: public static final int FIRST_WEEK_IN_YEAR = 1; 92: 93: /** Constant for the last week in the year. */ 94: public static final int LAST_WEEK_IN_YEAR = 53; 95: 96: /** The year in which the week falls. */ 97: private Year year; 98: 99: /** The week (1-53). */ 100: private int week; 101: 102: /** 103: * Creates a new time period for the week in which the current system 104: * date/time falls. 105: */ 106: public Week() { 107: this(new Date()); 108: } 109: 110: /** 111: * Creates a time period representing the week in the specified year. 112: * 113: * @param week the week (1 to 53). 114: * @param year the year (1900 to 9999). 115: */ 116: public Week(int week, int year) { 117: this(week, new Year(year)); 118: } 119: 120: /** 121: * Creates a time period representing the week in the specified year. 122: * 123: * @param week the week (1 to 53). 124: * @param year the year (1900 to 9999). 125: */ 126: public Week(int week, Year year) { 127: if ((week < FIRST_WEEK_IN_YEAR) && (week > LAST_WEEK_IN_YEAR)) { 128: throw new IllegalArgumentException( 129: "The 'week' argument must be in the range 1 - 53." 130: ); 131: } 132: this.week = week; 133: this.year = year; 134: } 135: 136: /** 137: * Creates a time period for the week in which the specified date/time 138: * falls. 139: * 140: * @param time the time (<code>null</code> not permitted). 141: */ 142: public Week(Date time) { 143: // defer argument checking... 144: this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 145: } 146: 147: /** 148: * Creates a time period for the week in which the specified date/time 149: * falls, calculated relative to the specified time zone. 150: * 151: * @param time the date/time (<code>null</code> not permitted). 152: * @param zone the time zone (<code>null</code> not permitted). 153: */ 154: public Week(Date time, TimeZone zone) { 155: if (time == null) { 156: throw new IllegalArgumentException("Null 'time' argument."); 157: } 158: if (zone == null) { 159: throw new IllegalArgumentException("Null 'zone' argument."); 160: } 161: Calendar calendar = Calendar.getInstance(zone); 162: calendar.setTime(time); 163: 164: // sometimes the last few days of the year are considered to fall in 165: // the *first* week of the following year. Refer to the Javadocs for 166: // GregorianCalendar. 167: int tempWeek = calendar.get(Calendar.WEEK_OF_YEAR); 168: if (tempWeek == 1 169: && calendar.get(Calendar.MONTH) == Calendar.DECEMBER) { 170: this.week = 1; 171: this.year = new Year(calendar.get(Calendar.YEAR) + 1); 172: } 173: else { 174: this.week = Math.min(tempWeek, LAST_WEEK_IN_YEAR); 175: int yyyy = calendar.get(Calendar.YEAR); 176: // alternatively, sometimes the first few days of the year are 177: // considered to fall in the *last* week of the previous year... 178: if (calendar.get(Calendar.MONTH) == Calendar.JANUARY 179: && this.week >= 52) { 180: yyyy--; 181: } 182: this.year = new Year(yyyy); 183: } 184: 185: } 186: 187: /** 188: * Returns the year in which the week falls. 189: * 190: * @return The year (never <code>null</code>). 191: */ 192: public Year getYear() { 193: return this.year; 194: } 195: 196: /** 197: * Returns the year in which the week falls, as an integer value. 198: * 199: * @return The year. 200: */ 201: public int getYearValue() { 202: return this.year.getYear(); 203: } 204: 205: /** 206: * Returns the week. 207: * 208: * @return The week. 209: */ 210: public int getWeek() { 211: return this.week; 212: } 213: 214: /** 215: * Returns the week preceding this one. This method will return 216: * <code>null</code> for some lower limit on the range of weeks (currently 217: * week 1, 1900). For week 1 of any year, the previous week is always week 218: * 53, but week 53 may not contain any days (you should check for this). 219: * 220: * @return The preceding week (possibly <code>null</code>). 221: */ 222: public RegularTimePeriod previous() { 223: 224: Week result; 225: if (this.week != FIRST_WEEK_IN_YEAR) { 226: result = new Week(this.week - 1, this.year); 227: } 228: else { 229: // we need to work out if the previous year has 52 or 53 weeks... 230: Year prevYear = (Year) this.year.previous(); 231: if (prevYear != null) { 232: int yy = prevYear.getYear(); 233: Calendar prevYearCalendar = Calendar.getInstance(); 234: prevYearCalendar.set(yy, Calendar.DECEMBER, 31); 235: result = new Week( 236: prevYearCalendar.getActualMaximum(Calendar.WEEK_OF_YEAR), 237: prevYear 238: ); 239: } 240: else { 241: result = null; 242: } 243: } 244: return result; 245: 246: } 247: 248: /** 249: * Returns the week following this one. This method will return 250: * <code>null</code> for some upper limit on the range of weeks (currently 251: * week 53, 9999). For week 52 of any year, the following week is always 252: * week 53, but week 53 may not contain any days (you should check for 253: * this). 254: * 255: * @return The following week (possibly <code>null</code>). 256: */ 257: public RegularTimePeriod next() { 258: 259: Week result; 260: if (this.week < 52) { 261: result = new Week(this.week + 1, this.year); 262: } 263: else { 264: Calendar calendar = Calendar.getInstance(); 265: calendar.set(this.year.getYear(), Calendar.DECEMBER, 31); 266: int actualMaxWeek 267: = calendar.getActualMaximum(Calendar.WEEK_OF_YEAR); 268: if (this.week != actualMaxWeek) { 269: result = new Week(this.week + 1, this.year); 270: } 271: else { 272: Year nextYear = (Year) this.year.next(); 273: if (nextYear != null) { 274: result = new Week(FIRST_WEEK_IN_YEAR, nextYear); 275: } 276: else { 277: result = null; 278: } 279: } 280: } 281: return result; 282: 283: } 284: 285: /** 286: * Returns a serial index number for the week. 287: * 288: * @return The serial index number. 289: */ 290: public long getSerialIndex() { 291: return this.year.getYear() * 53L + this.week; 292: } 293: 294: /** 295: * Returns the first millisecond of the week, evaluated using the supplied 296: * calendar (which determines the time zone). 297: * 298: * @param calendar the calendar. 299: * 300: * @return The first millisecond of the week. 301: */ 302: public long getFirstMillisecond(Calendar calendar) { 303: Calendar c = (Calendar) calendar.clone(); 304: c.clear(); 305: c.set(Calendar.YEAR, this.year.getYear()); 306: c.set(Calendar.WEEK_OF_YEAR, this.week); 307: c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); 308: c.set(Calendar.HOUR, 0); 309: c.set(Calendar.MINUTE, 0); 310: c.set(Calendar.SECOND, 0); 311: c.set(Calendar.MILLISECOND, 0); 312: //return c.getTimeInMillis(); // this won't work for JDK 1.3 313: return c.getTime().getTime(); 314: } 315: 316: /** 317: * Returns the last millisecond of the week, evaluated using the supplied 318: * calendar (which determines the time zone). 319: * 320: * @param calendar the calendar. 321: * 322: * @return The last millisecond of the week. 323: */ 324: public long getLastMillisecond(Calendar calendar) { 325: RegularTimePeriod next = next(); 326: return next.getFirstMillisecond(calendar) - 1; 327: } 328: 329: /** 330: * Returns a string representing the week (e.g. "Week 9, 2002"). 331: * 332: * TODO: look at internationalisation. 333: * 334: * @return A string representing the week. 335: */ 336: public String toString() { 337: return "Week " + this.week + ", " + this.year; 338: } 339: 340: /** 341: * Tests the equality of this Week object to an arbitrary object. Returns 342: * true if the target is a Week instance representing the same week as this 343: * object. In all other cases, returns false. 344: * @param obj The object. 345: * 346: * @return <code>true</code> if week and year of this and object are the 347: * same. 348: */ 349: public boolean equals(Object obj) { 350: 351: if (obj == this) { 352: return true; 353: } 354: if (!(obj instanceof Week)) { 355: return false; 356: } 357: Week that = (Week) obj; 358: if (this.week != that.week) { 359: return false; 360: } 361: if (!this.year.equals(that.year)) { 362: return false; 363: } 364: return true; 365: 366: } 367: 368: /** 369: * Returns a hash code for this object instance. The approach described by 370: * Joshua Bloch in "Effective Java" has been used here: 371: * <p> 372: * <code>http://developer.java.sun.com/developer/Books/effectivejava 373: * /Chapter3.pdf</code> 374: * 375: * @return A hash code. 376: */ 377: public int hashCode() { 378: int result = 17; 379: result = 37 * result + this.week; 380: result = 37 * result + this.year.hashCode(); 381: return result; 382: } 383: 384: /** 385: * Returns an integer indicating the order of this Week object relative to 386: * the specified object: 387: * 388: * negative == before, zero == same, positive == after. 389: * 390: * @param o1 the object to compare. 391: * 392: * @return negative == before, zero == same, positive == after. 393: */ 394: public int compareTo(Object o1) { 395: 396: int result; 397: 398: // CASE 1 : Comparing to another Week object 399: // -------------------------------------------- 400: if (o1 instanceof Week) { 401: Week w = (Week) o1; 402: result = this.year.getYear() - w.getYear().getYear(); 403: if (result == 0) { 404: result = this.week - w.getWeek(); 405: } 406: } 407: 408: // CASE 2 : Comparing to another TimePeriod object 409: // ----------------------------------------------- 410: else if (o1 instanceof RegularTimePeriod) { 411: // more difficult case - evaluate later... 412: result = 0; 413: } 414: 415: // CASE 3 : Comparing to a non-TimePeriod object 416: // --------------------------------------------- 417: else { 418: // consider time periods to be ordered after general objects 419: result = 1; 420: } 421: 422: return result; 423: 424: } 425: 426: /** 427: * Parses the string argument as a week. 428: * <P> 429: * This method is required to accept the format "YYYY-Wnn". It will also 430: * accept "Wnn-YYYY". Anything else, at the moment, is a bonus. 431: * 432: * @param s string to parse. 433: * 434: * @return <code>null</code> if the string is not parseable, the week 435: * otherwise. 436: */ 437: public static Week parseWeek(String s) { 438: 439: Week result = null; 440: if (s != null) { 441: 442: // trim whitespace from either end of the string 443: s = s.trim(); 444: 445: int i = Week.findSeparator(s); 446: if (i != -1) { 447: String s1 = s.substring(0, i).trim(); 448: String s2 = s.substring(i + 1, s.length()).trim(); 449: 450: Year y = Week.evaluateAsYear(s1); 451: int w; 452: if (y != null) { 453: w = Week.stringToWeek(s2); 454: if (w == -1) { 455: throw new TimePeriodFormatException( 456: "Can't evaluate the week." 457: ); 458: } 459: result = new Week(w, y); 460: } 461: else { 462: y = Week.evaluateAsYear(s2); 463: if (y != null) { 464: w = Week.stringToWeek(s1); 465: if (w == -1) { 466: throw new TimePeriodFormatException( 467: "Can't evaluate the week." 468: ); 469: } 470: result = new Week(w, y); 471: } 472: else { 473: throw new TimePeriodFormatException( 474: "Can't evaluate the year." 475: ); 476: } 477: } 478: 479: } 480: else { 481: throw new TimePeriodFormatException( 482: "Could not find separator." 483: ); 484: } 485: 486: } 487: return result; 488: 489: } 490: 491: /** 492: * Finds the first occurrence of ' ', '-', ',' or '.' 493: * 494: * @param s the string to parse. 495: * 496: * @return <code>-1</code> if none of the characters was found, the 497: * index of the first occurrence otherwise. 498: */ 499: private static int findSeparator(String s) { 500: 501: int result = s.indexOf('-'); 502: if (result == -1) { 503: result = s.indexOf(','); 504: } 505: if (result == -1) { 506: result = s.indexOf(' '); 507: } 508: if (result == -1) { 509: result = s.indexOf('.'); 510: } 511: return result; 512: } 513: 514: /** 515: * Creates a year from a string, or returns null (format exceptions 516: * suppressed). 517: * 518: * @param s string to parse. 519: * 520: * @return <code>null</code> if the string is not parseable, the year 521: * otherwise. 522: */ 523: private static Year evaluateAsYear(String s) { 524: 525: Year result = null; 526: try { 527: result = Year.parseYear(s); 528: } 529: catch (TimePeriodFormatException e) { 530: // suppress 531: } 532: return result; 533: 534: } 535: 536: /** 537: * Converts a string to a week. 538: * 539: * @param s the string to parse. 540: * @return <code>-1</code> if the string does not contain a week number, 541: * the number of the week otherwise. 542: */ 543: private static int stringToWeek(String s) { 544: 545: int result = -1; 546: s = s.replace('W', ' '); 547: s = s.trim(); 548: try { 549: result = Integer.parseInt(s); 550: if ((result < 1) || (result > LAST_WEEK_IN_YEAR)) { 551: result = -1; 552: } 553: } 554: catch (NumberFormatException e) { 555: // suppress 556: } 557: return result; 558: 559: } 560: 561: }