Source for org.jfree.chart.axis.QuarterDateFormat

   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 
  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:  * QuarterDateFormat.java
  29:  * ----------------------
  30:  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: QuarterDateFormat.java,v 1.3.2.2 2005/10/25 20:37:34 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 01-Mar-2005 : Version 1 (DG);
  40:  * 10-May-2005 : Added equals() method, and implemented Cloneable and 
  41:  *               Serializable (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.chart.axis;
  46: 
  47: import java.io.Serializable;
  48: import java.text.DateFormat;
  49: import java.text.FieldPosition;
  50: import java.text.NumberFormat;
  51: import java.text.ParsePosition;
  52: import java.util.Arrays;
  53: import java.util.Calendar;
  54: import java.util.Date;
  55: import java.util.GregorianCalendar;
  56: import java.util.TimeZone;
  57: 
  58: /**
  59:  * A formatter that formats dates to show the year and quarter (for example,
  60:  * '2004 IV' for the last quarter of 2004.
  61:  */
  62: public class QuarterDateFormat extends DateFormat 
  63:                                implements Cloneable, Serializable {
  64:     
  65:     /** For serialization. */
  66:     private static final long serialVersionUID = -6738465248529797176L;
  67:     
  68:     /** Symbols for regular quarters. */
  69:     public static final String[] REGULAR_QUARTERS 
  70:         = new String[] {"1", "2", "3", "4"};
  71:     
  72:     /** Symbols for roman numbered quarters. */
  73:     public static final String[] ROMAN_QUARTERS 
  74:         = new String[] {"I", "II", "III", "IV"};
  75:     
  76:     /** The strings. */
  77:     private String[] quarters = REGULAR_QUARTERS;
  78:     
  79:     /**
  80:      * Creates a new instance for the default time zone.
  81:      */
  82:     public QuarterDateFormat() {
  83:         this(TimeZone.getDefault());  
  84:     }
  85:     
  86:     /**
  87:      * Creates a new instance for the specified time zone.
  88:      * 
  89:      * @param zone  the time zone (<code>null</code> not permitted).
  90:      */
  91:     public QuarterDateFormat(TimeZone zone) {
  92:         this(zone, REGULAR_QUARTERS);
  93:     }
  94:     
  95:     /**
  96:      * Creates a new instance for the specified time zone.
  97:      * 
  98:      * @param zone  the time zone (<code>null</code> not permitted).
  99:      * @param quarterSymbols  the quarter symbols.
 100:      */
 101:     public QuarterDateFormat(TimeZone zone, String[] quarterSymbols) {
 102:         if (zone == null) {
 103:             throw new IllegalArgumentException("Null 'zone' argument.");   
 104:         }
 105:         this.calendar = new GregorianCalendar(zone);
 106:         this.quarters = quarterSymbols;
 107:         
 108:         // the following is never used, but it seems that DateFormat requires
 109:         // it to be non-null.  It isn't well covered in the spec, refer to 
 110:         // bug parade 5061189 for more info.
 111:         this.numberFormat = NumberFormat.getNumberInstance();
 112:     }
 113:     
 114:     /**
 115:      * Formats the given date.
 116:      * 
 117:      * @param date  the date.
 118:      * @param toAppendTo  the string buffer.
 119:      * @param fieldPosition  the field position.
 120:      * 
 121:      * @return The formatted date.
 122:      */
 123:     public StringBuffer format(Date date, StringBuffer toAppendTo,
 124:                                FieldPosition fieldPosition) {
 125:         this.calendar.setTime(date);
 126:         int year = this.calendar.get(Calendar.YEAR);
 127:         int month = this.calendar.get(Calendar.MONTH);
 128:         toAppendTo.append(year);
 129:         toAppendTo.append(" ");
 130:         int quarter = month / 3;
 131:         toAppendTo.append(this.quarters[quarter]);
 132:         return toAppendTo;   
 133:     }
 134: 
 135:     /**
 136:      * Parses the given string (not implemented).
 137:      * 
 138:      * @param source  the date string.
 139:      * @param pos  the parse position.
 140:      * 
 141:      * @return <code>null</code>, as this method has not been implemented.
 142:      */
 143:     public Date parse(String source, ParsePosition pos) {
 144:         return null;   
 145:     }
 146: 
 147:     /**
 148:      * Tests this formatter for equality with an arbitrary object.
 149:      * 
 150:      * @param obj  the object.
 151:      * 
 152:      * @return A boolean.
 153:      */
 154:     public boolean equals(Object obj) {
 155:         if (obj == this) {
 156:             return true;
 157:         }
 158:         if (!(obj instanceof QuarterDateFormat)) {
 159:             return false;
 160:         }
 161:         if (!super.equals(obj)) {
 162:             return false;
 163:         }
 164:         QuarterDateFormat that = (QuarterDateFormat) obj;
 165:         if (!Arrays.equals(this.quarters, that.quarters)) {
 166:             return false;
 167:         }
 168:         return true;
 169:     }
 170:     
 171: }