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 * StandardXYSeriesLabelGenerator.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.MessageFormat;
041
042import org.jfree.chart.HashUtils;
043import org.jfree.chart.util.Args;
044import org.jfree.chart.util.PublicCloneable;
045import org.jfree.data.xy.XYDataset;
046
047/**
048 * A standard series label generator for plots that use data from
049 * an {@link org.jfree.data.xy.XYDataset}.
050 * <br><br>
051 * This class implements {@code PublicCloneable} by mistake but we retain
052 * this for the sake of backward compatibility.
053 */
054public class StandardXYSeriesLabelGenerator implements XYSeriesLabelGenerator,
055        Cloneable, PublicCloneable, Serializable {
056
057    /** For serialization. */
058    private static final long serialVersionUID = 1916017081848400024L;
059
060    /** The default item label format. */
061    public static final String DEFAULT_LABEL_FORMAT = "{0}";
062
063    /** The format pattern. */
064    private String formatPattern;
065
066    /**
067     * Creates a default series label generator (uses
068     * {@link #DEFAULT_LABEL_FORMAT}).
069     */
070    public StandardXYSeriesLabelGenerator() {
071        this(DEFAULT_LABEL_FORMAT);
072    }
073
074    /**
075     * Creates a new series label generator.
076     *
077     * @param format  the format pattern ({@code null} not permitted).
078     */
079    public StandardXYSeriesLabelGenerator(String format) {
080        Args.nullNotPermitted(format, "format");
081        this.formatPattern = format;
082    }
083
084    /**
085     * Generates a label for the specified series.  This label will be
086     * used for the chart legend.
087     *
088     * @param dataset  the dataset ({@code null} not permitted).
089     * @param series  the series.
090     *
091     * @return A series label.
092     */
093    @Override
094    public String generateLabel(XYDataset dataset, int series) {
095        Args.nullNotPermitted(dataset, "dataset");
096        String label = MessageFormat.format(
097            this.formatPattern, createItemArray(dataset, series)
098        );
099        return label;
100    }
101
102    /**
103     * Creates the array of items that can be passed to the
104     * {@link MessageFormat} class for creating labels.
105     *
106     * @param dataset  the dataset ({@code null} not permitted).
107     * @param series  the series (zero-based index).
108     *
109     * @return The items (never {@code null}).
110     */
111    protected Object[] createItemArray(XYDataset dataset, int series) {
112        Object[] result = new Object[1];
113        result[0] = dataset.getSeriesKey(series).toString();
114        return result;
115    }
116
117    /**
118     * Returns an independent copy of the generator.  This is unnecessary,
119     * because instances are immutable anyway, but we retain this
120     * behaviour for backwards compatibility.
121     *
122     * @return A clone.
123     *
124     * @throws CloneNotSupportedException if cloning is not supported.
125     */
126    @Override
127    public Object clone() throws CloneNotSupportedException {
128        return super.clone();
129    }
130
131    /**
132     * Tests this object for equality with an arbitrary object.
133     *
134     * @param obj  the other object ({@code null} permitted).
135     *
136     * @return A boolean.
137     */
138    @Override
139    public boolean equals(Object obj) {
140        if (obj == this) {
141            return true;
142        }
143        if (!(obj instanceof StandardXYSeriesLabelGenerator)) {
144            return false;
145        }
146        StandardXYSeriesLabelGenerator that
147                = (StandardXYSeriesLabelGenerator) obj;
148        if (!this.formatPattern.equals(that.formatPattern)) {
149            return false;
150        }
151        return true;
152    }
153
154    /**
155     * Returns a hash code for this instance.
156     *
157     * @return A hash code.
158     */
159    @Override
160    public int hashCode() {
161        int result = 127;
162        result = HashUtils.hashCode(result, this.formatPattern);
163        return result;
164    }
165
166}