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