001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert 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-2022, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
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.internal.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    public static final String[] GREEK_QUARTERS = new String[] {"\u0391",
073            "\u0392", "\u0393", "\u0394"};
074
075    /** The strings. */
076    private String[] quarters = REGULAR_QUARTERS;
077
078    /** A flag that controls whether the quarter or the year goes first. */
079    private boolean quarterFirst;
080
081    /**
082     * Creates a new instance for the default time zone.
083     */
084    public QuarterDateFormat() {
085        this(TimeZone.getDefault());
086    }
087
088    /**
089     * Creates a new instance for the specified time zone.
090     *
091     * @param zone  the time zone ({@code null} not permitted).
092     */
093    public QuarterDateFormat(TimeZone zone) {
094        this(zone, REGULAR_QUARTERS);
095    }
096
097    /**
098     * Creates a new instance for the specified time zone.
099     *
100     * @param zone  the time zone ({@code null} not permitted).
101     * @param quarterSymbols  the quarter symbols.
102     */
103    public QuarterDateFormat(TimeZone zone, String[] quarterSymbols) {
104        this(zone, quarterSymbols, false);
105    }
106
107    /**
108     * Creates a new instance for the specified time zone.
109     *
110     * @param zone  the time zone ({@code null} not permitted).
111     * @param quarterSymbols  the quarter symbols.
112     * @param quarterFirst  a flag that controls whether the quarter or the
113     *         year is displayed first.
114     */
115    public QuarterDateFormat(TimeZone zone, String[] quarterSymbols,
116            boolean quarterFirst) {
117        Args.nullNotPermitted(zone, "zone");
118        this.calendar = new GregorianCalendar(zone);
119        this.quarters = quarterSymbols;
120        this.quarterFirst = quarterFirst;
121
122        // the following is never used, but it seems that DateFormat requires
123        // it to be non-null.  It isn't well covered in the spec, refer to
124        // bug parade 5061189 for more info.
125        this.numberFormat = NumberFormat.getNumberInstance();
126
127    }
128
129    /**
130     * Formats the given date.
131     *
132     * @param date  the date.
133     * @param toAppendTo  the string buffer.
134     * @param fieldPosition  the field position.
135     *
136     * @return The formatted date.
137     */
138    @Override
139    public StringBuffer format(Date date, StringBuffer toAppendTo,
140                               FieldPosition fieldPosition) {
141        this.calendar.setTime(date);
142        int year = this.calendar.get(Calendar.YEAR);
143        int month = this.calendar.get(Calendar.MONTH);
144        int quarter = month / 3;
145        if (this.quarterFirst) {
146            toAppendTo.append(this.quarters[quarter]);
147            toAppendTo.append(" ");
148            toAppendTo.append(year);
149        }
150        else {
151            toAppendTo.append(year);
152            toAppendTo.append(" ");
153            toAppendTo.append(this.quarters[quarter]);
154        }
155        return toAppendTo;
156    }
157
158    /**
159     * Parses the given string (not implemented).
160     *
161     * @param source  the date string.
162     * @param pos  the parse position.
163     *
164     * @return {@code null}, as this method has not been implemented.
165     */
166    @Override
167    public Date parse(String source, ParsePosition pos) {
168        return null;
169    }
170
171    /**
172     * Tests this formatter for equality with an arbitrary object.
173     *
174     * @param obj  the object ({@code null} permitted).
175     *
176     * @return A boolean.
177     */
178    @Override
179    public boolean equals(Object obj) {
180        if (obj == this) {
181            return true;
182        }
183        if (!(obj instanceof QuarterDateFormat)) {
184            return false;
185        }
186        QuarterDateFormat that = (QuarterDateFormat) obj;
187        if (!Arrays.equals(this.quarters, that.quarters)) {
188            return false;
189        }
190        if (this.quarterFirst != that.quarterFirst) {
191            return false;
192        }
193        return super.equals(obj);
194    }
195
196}