001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * QuarterDateFormat.java
029 * ----------------------
030 * (C) Copyright 2005-2020, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.Serializable;
040import java.text.DateFormat;
041import java.text.FieldPosition;
042import java.text.NumberFormat;
043import java.text.ParsePosition;
044import java.util.Arrays;
045import java.util.Calendar;
046import java.util.Date;
047import java.util.GregorianCalendar;
048import java.util.TimeZone;
049import org.jfree.chart.util.Args;
050
051/**
052 * A formatter that formats dates to show the year and quarter (for example,
053 * '2004 IV' for the last quarter of 2004.
054 */
055public class QuarterDateFormat extends DateFormat
056        implements Cloneable, Serializable {
057
058    /** For serialization. */
059    private static final long serialVersionUID = -6738465248529797176L;
060
061    /** Symbols for regular quarters. */
062    public static final String[] REGULAR_QUARTERS = new String[] {"1", "2",
063            "3", "4"};
064
065    /** Symbols for roman numbered quarters. */
066    public static final String[] ROMAN_QUARTERS  = new String[] {"I", "II",
067            "III", "IV"};
068
069    /**
070     * Symbols for greek numbered quarters.
071     *
072     * @since 1.0.6
073     */
074    public static final String[] GREEK_QUARTERS = new String[] {"\u0391",
075            "\u0392", "\u0393", "\u0394"};
076
077    /** The strings. */
078    private String[] quarters = REGULAR_QUARTERS;
079
080    /** A flag that controls whether the quarter or the year goes first. */
081    private boolean quarterFirst;
082
083    /**
084     * Creates a new instance for the default time zone.
085     */
086    public QuarterDateFormat() {
087        this(TimeZone.getDefault());
088    }
089
090    /**
091     * Creates a new instance for the specified time zone.
092     *
093     * @param zone  the time zone ({@code null} not permitted).
094     */
095    public QuarterDateFormat(TimeZone zone) {
096        this(zone, REGULAR_QUARTERS);
097    }
098
099    /**
100     * Creates a new instance for the specified time zone.
101     *
102     * @param zone  the time zone ({@code null} not permitted).
103     * @param quarterSymbols  the quarter symbols.
104     */
105    public QuarterDateFormat(TimeZone zone, String[] quarterSymbols) {
106        this(zone, quarterSymbols, false);
107    }
108
109    /**
110     * Creates a new instance for the specified time zone.
111     *
112     * @param zone  the time zone ({@code null} not permitted).
113     * @param quarterSymbols  the quarter symbols.
114     * @param quarterFirst  a flag that controls whether the quarter or the
115     *         year is displayed first.
116     *
117     * @since 1.0.6
118     */
119    public QuarterDateFormat(TimeZone zone, String[] quarterSymbols,
120            boolean quarterFirst) {
121        Args.nullNotPermitted(zone, "zone");
122        this.calendar = new GregorianCalendar(zone);
123        this.quarters = quarterSymbols;
124        this.quarterFirst = quarterFirst;
125
126        // the following is never used, but it seems that DateFormat requires
127        // it to be non-null.  It isn't well covered in the spec, refer to
128        // bug parade 5061189 for more info.
129        this.numberFormat = NumberFormat.getNumberInstance();
130
131    }
132
133    /**
134     * Formats the given date.
135     *
136     * @param date  the date.
137     * @param toAppendTo  the string buffer.
138     * @param fieldPosition  the field position.
139     *
140     * @return The formatted date.
141     */
142    @Override
143    public StringBuffer format(Date date, StringBuffer toAppendTo,
144                               FieldPosition fieldPosition) {
145        this.calendar.setTime(date);
146        int year = this.calendar.get(Calendar.YEAR);
147        int month = this.calendar.get(Calendar.MONTH);
148        int quarter = month / 3;
149        if (this.quarterFirst) {
150            toAppendTo.append(this.quarters[quarter]);
151            toAppendTo.append(" ");
152            toAppendTo.append(year);
153        }
154        else {
155            toAppendTo.append(year);
156            toAppendTo.append(" ");
157            toAppendTo.append(this.quarters[quarter]);
158        }
159        return toAppendTo;
160    }
161
162    /**
163     * Parses the given string (not implemented).
164     *
165     * @param source  the date string.
166     * @param pos  the parse position.
167     *
168     * @return {@code null}, as this method has not been implemented.
169     */
170    @Override
171    public Date parse(String source, ParsePosition pos) {
172        return null;
173    }
174
175    /**
176     * Tests this formatter for equality with an arbitrary object.
177     *
178     * @param obj  the object ({@code null} permitted).
179     *
180     * @return A boolean.
181     */
182    @Override
183    public boolean equals(Object obj) {
184        if (obj == this) {
185            return true;
186        }
187        if (!(obj instanceof QuarterDateFormat)) {
188            return false;
189        }
190        QuarterDateFormat that = (QuarterDateFormat) obj;
191        if (!Arrays.equals(this.quarters, that.quarters)) {
192            return false;
193        }
194        if (this.quarterFirst != that.quarterFirst) {
195            return false;
196        }
197        return super.equals(obj);
198    }
199
200}