001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, 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 * AbstractXYItemLabelGenerator.java
029 * ---------------------------------
030 * (C) Copyright 2004-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.io.Serializable;
040import java.text.DateFormat;
041import java.text.MessageFormat;
042import java.text.NumberFormat;
043import java.util.Date;
044import java.util.Objects;
045
046import org.jfree.chart.HashUtils;
047import org.jfree.chart.util.Args;
048import org.jfree.data.xy.XYDataset;
049
050/**
051 * A base class for creating item label generators.
052 */
053public class AbstractXYItemLabelGenerator implements Cloneable, Serializable {
054
055    /** For serialization. */
056    private static final long serialVersionUID = 5869744396278660636L;
057
058    /** The item label format string. */
059    private final String formatString;
060
061    /** A number formatter for the x value. */
062    private NumberFormat xFormat;
063
064    /** A date formatter for the x value. */
065    private DateFormat xDateFormat;
066
067    /** A formatter for the y value. */
068    private NumberFormat yFormat;
069
070    /** A date formatter for the y value. */
071    private DateFormat yDateFormat;
072
073    /** The string used to represent 'null' for the y-value. */
074    private final String nullYString = "null";
075
076    /**
077     * Creates an item label generator using default number formatters.
078     */
079    protected AbstractXYItemLabelGenerator() {
080        this("{2}", NumberFormat.getNumberInstance(),
081                NumberFormat.getNumberInstance());
082    }
083
084    /**
085     * Creates an item label generator using the specified number formatters.
086     *
087     * @param formatString  the item label format string ({@code null}
088     *                      not permitted).
089     * @param xFormat  the format object for the x values ({@code null}
090     *                 not permitted).
091     * @param yFormat  the format object for the y values ({@code null}
092     *                 not permitted).
093     */
094    protected AbstractXYItemLabelGenerator(String formatString, 
095            NumberFormat xFormat, NumberFormat yFormat) {
096
097        Args.nullNotPermitted(formatString, "formatString");
098        Args.nullNotPermitted(xFormat, "xFormat");
099        Args.nullNotPermitted(yFormat, "yFormat");
100        this.formatString = formatString;
101        this.xFormat = xFormat;
102        this.yFormat = yFormat;
103    }
104
105    /**
106     * Creates an item label generator using the specified number formatters.
107     *
108     * @param formatString  the item label format string ({@code null}
109     *                      not permitted).
110     * @param xFormat  the format object for the x values ({@code null}
111     *                 permitted).
112     * @param yFormat  the format object for the y values ({@code null}
113     *                 not permitted).
114     */
115    protected AbstractXYItemLabelGenerator(String formatString, 
116            DateFormat xFormat, NumberFormat yFormat) {
117
118        this(formatString, NumberFormat.getInstance(), yFormat);
119        this.xDateFormat = xFormat;
120    }
121
122    /**
123     * Creates an item label generator using the specified formatters (a
124     * number formatter for the x-values and a date formatter for the
125     * y-values).
126     *
127     * @param formatString  the item label format string ({@code null}
128     *                      not permitted).
129     * @param xFormat  the format object for the x values ({@code null}
130     *                 permitted).
131     * @param yFormat  the format object for the y values ({@code null}
132     *                 not permitted).
133     */
134    protected AbstractXYItemLabelGenerator(String formatString,
135            NumberFormat xFormat, DateFormat yFormat) {
136
137        this(formatString, xFormat, NumberFormat.getInstance());
138        this.yDateFormat = yFormat;
139    }
140
141    /**
142     * Creates an item label generator using the specified number formatters.
143     *
144     * @param formatString  the item label format string ({@code null}
145     *                      not permitted).
146     * @param xFormat  the format object for the x values ({@code null}
147     *                 permitted).
148     * @param yFormat  the format object for the y values ({@code null}
149     *                 not permitted).
150     */
151    protected AbstractXYItemLabelGenerator(String formatString, 
152            DateFormat xFormat, DateFormat yFormat) {
153
154        this(formatString, NumberFormat.getInstance(),
155                NumberFormat.getInstance());
156        this.xDateFormat = xFormat;
157        this.yDateFormat = yFormat;
158    }
159
160    /**
161     * Returns the format string (this controls the overall structure of the
162     * label).
163     *
164     * @return The format string (never {@code null}).
165     */
166    public String getFormatString() {
167        return this.formatString;
168    }
169
170    /**
171     * Returns the number formatter for the x-values.
172     *
173     * @return The number formatter (possibly {@code null}).
174     */
175    public NumberFormat getXFormat() {
176        return this.xFormat;
177    }
178
179    /**
180     * Returns the date formatter for the x-values.
181     *
182     * @return The date formatter (possibly {@code null}).
183     */
184    public DateFormat getXDateFormat() {
185        return this.xDateFormat;
186    }
187
188    /**
189     * Returns the number formatter for the y-values.
190     *
191     * @return The number formatter (possibly {@code null}).
192     */
193    public NumberFormat getYFormat() {
194        return this.yFormat;
195    }
196
197    /**
198     * Returns the date formatter for the y-values.
199     *
200     * @return The date formatter (possibly {@code null}).
201     */
202    public DateFormat getYDateFormat() {
203        return this.yDateFormat;
204    }
205
206    /**
207     * Generates a label string for an item in the dataset.
208     *
209     * @param dataset  the dataset ({@code null} not permitted).
210     * @param series  the series (zero-based index).
211     * @param item  the item (zero-based index).
212     *
213     * @return The label (possibly {@code null}).
214     */
215    public String generateLabelString(XYDataset dataset, int series, int item) {
216        String result;
217        Object[] items = createItemArray(dataset, series, item);
218        result = MessageFormat.format(this.formatString, items);
219        return result;
220    }
221
222    /**
223     * Returns the string representing a null value.
224     *
225     * @return The string representing a null value.
226     */
227    public String getNullYString() {
228        return this.nullYString;
229    }
230
231    /**
232     * Creates the array of items that can be passed to the
233     * {@link MessageFormat} class for creating labels.
234     *
235     * @param dataset  the dataset ({@code null} not permitted).
236     * @param series  the series (zero-based index).
237     * @param item  the item (zero-based index).
238     *
239     * @return An array of three items from the dataset formatted as
240     *         {@code String} objects (never {@code null}).
241     */
242    protected Object[] createItemArray(XYDataset dataset, int series,
243                                       int item) {
244        Object[] result = new Object[3];
245        result[0] = dataset.getSeriesKey(series).toString();
246
247        double x = dataset.getXValue(series, item);
248        if (this.xDateFormat != null) {
249            result[1] = this.xDateFormat.format(new Date((long) x));
250        }
251        else {
252            result[1] = this.xFormat.format(x);
253        }
254
255        double y = dataset.getYValue(series, item);
256        if (Double.isNaN(y) && dataset.getY(series, item) == null) {
257            result[2] = this.nullYString;
258        }
259        else {
260            if (this.yDateFormat != null) {
261                result[2] = this.yDateFormat.format(new Date((long) y));
262            }
263            else {
264                result[2] = this.yFormat.format(y);
265            }
266        }
267        return result;
268    }
269
270    /**
271     * Tests this object for equality with an arbitrary object.
272     *
273     * @param obj  the other object ({@code null} permitted).
274     *
275     * @return A boolean.
276     */
277    @Override
278    public boolean equals(Object obj) {
279        if (obj == this) {
280            return true;
281        }
282        if (!(obj instanceof AbstractXYItemLabelGenerator)) {
283            return false;
284        }
285        AbstractXYItemLabelGenerator that = (AbstractXYItemLabelGenerator) obj;
286        if (!this.formatString.equals(that.formatString)) {
287            return false;
288        }
289        if (!Objects.equals(this.xFormat, that.xFormat)) {
290            return false;
291        }
292        if (!Objects.equals(this.xDateFormat, that.xDateFormat)) {
293            return false;
294        }
295        if (!Objects.equals(this.yFormat, that.yFormat)) {
296            return false;
297        }
298        if (!Objects.equals(this.yDateFormat, that.yDateFormat)) {
299            return false;
300        }
301        if (!this.nullYString.equals(that.nullYString)) {
302            return false;
303        }
304        return true;
305    }
306
307    /**
308     * Returns a hash code for this instance.
309     *
310     * @return A hash code.
311     */
312    @Override
313    public int hashCode() {
314        int result = 127;
315        result = HashUtils.hashCode(result, this.formatString);
316        result = HashUtils.hashCode(result, this.xFormat);
317        result = HashUtils.hashCode(result, this.xDateFormat);
318        result = HashUtils.hashCode(result, this.yFormat);
319        result = HashUtils.hashCode(result, this.yDateFormat);
320        return result;
321    }
322
323    /**
324     * Returns an independent copy of the generator.
325     *
326     * @return A clone.
327     *
328     * @throws CloneNotSupportedException if cloning is not supported.
329     */
330    @Override
331    public Object clone() throws CloneNotSupportedException {
332        AbstractXYItemLabelGenerator clone
333                = (AbstractXYItemLabelGenerator) super.clone();
334        if (this.xFormat != null) {
335            clone.xFormat = (NumberFormat) this.xFormat.clone();
336        }
337        if (this.yFormat != null) {
338            clone.yFormat = (NumberFormat) this.yFormat.clone();
339        }
340        if (this.xDateFormat != null) {
341            clone.xDateFormat = (DateFormat) this.xDateFormat.clone();
342        }
343        if (this.yDateFormat != null) {
344            clone.yDateFormat = (DateFormat) this.yDateFormat.clone();
345        }
346        return clone;
347    }
348
349}