001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * StandardCategorySeriesLabelGenerator.java
029 * -----------------------------------------
030 * (C) Copyright 2005-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 20-Apr-2005 : Version 1 (DG);
038 * ------------- JFREECHART 1.0.x ---------------------------------------------
039 * 03-May-2006 : Fixed equals() method (bug 1481102) (DG);
040 * 31-Mar-2008 : Added hashCode() method to appease FindBugs (DG);
041 * 03-Jul-2013 : Use ParamChecks (DG);
042 *
043 */
044
045package org.jfree.chart.labels;
046
047import java.io.Serializable;
048import java.text.MessageFormat;
049
050import org.jfree.chart.HashUtilities;
051import org.jfree.chart.util.ParamChecks;
052import org.jfree.data.category.CategoryDataset;
053import org.jfree.util.PublicCloneable;
054
055/**
056 * A standard series label generator for plots that use data from
057 * a {@link org.jfree.data.category.CategoryDataset}.
058 */
059public class StandardCategorySeriesLabelGenerator implements
060    CategorySeriesLabelGenerator, Cloneable, PublicCloneable, Serializable {
061
062    /** For serialization. */
063    private static final long serialVersionUID = 4630760091523940820L;
064
065    /** The default item label format. */
066    public static final String DEFAULT_LABEL_FORMAT = "{0}";
067
068    /** The format pattern. */
069    private String formatPattern;
070
071    /**
072     * Creates a default series label generator (uses
073     * {@link #DEFAULT_LABEL_FORMAT}).
074     */
075    public StandardCategorySeriesLabelGenerator() {
076        this(DEFAULT_LABEL_FORMAT);
077    }
078
079    /**
080     * Creates a new series label generator.
081     *
082     * @param format  the format pattern (<code>null</code> not permitted).
083     */
084    public StandardCategorySeriesLabelGenerator(String format) {
085        ParamChecks.nullNotPermitted(format, "format");
086        this.formatPattern = format;
087    }
088
089    /**
090     * Generates a label for the specified series.
091     *
092     * @param dataset  the dataset (<code>null</code> not permitted).
093     * @param series  the series.
094     *
095     * @return A series label.
096     */
097    @Override
098    public String generateLabel(CategoryDataset dataset, int series) {
099        ParamChecks.nullNotPermitted(dataset, "dataset");
100        String label = MessageFormat.format(this.formatPattern,
101                createItemArray(dataset, series));
102        return label;
103    }
104
105    /**
106     * Creates the array of items that can be passed to the
107     * {@link MessageFormat} class for creating labels.
108     *
109     * @param dataset  the dataset (<code>null</code> not permitted).
110     * @param series  the series (zero-based index).
111     *
112     * @return The items (never <code>null</code>).
113     */
114    protected Object[] createItemArray(CategoryDataset dataset, int series) {
115        Object[] result = new Object[1];
116        result[0] = dataset.getRowKey(series).toString();
117        return result;
118    }
119
120    /**
121     * Returns an independent copy of the generator.
122     *
123     * @return A clone.
124     *
125     * @throws CloneNotSupportedException if cloning is not supported.
126     */
127    @Override
128    public Object clone() throws CloneNotSupportedException {
129        return super.clone();
130    }
131
132    /**
133     * Tests this object for equality with an arbitrary object.
134     *
135     * @param obj  the other object (<code>null</code> permitted).
136     *
137     * @return A boolean.
138     */
139    @Override
140    public boolean equals(Object obj) {
141        if (obj == this) {
142            return true;
143        }
144        if (!(obj instanceof StandardCategorySeriesLabelGenerator)) {
145            return false;
146        }
147        StandardCategorySeriesLabelGenerator that
148                = (StandardCategorySeriesLabelGenerator) obj;
149        if (!this.formatPattern.equals(that.formatPattern)) {
150            return false;
151        }
152        return true;
153    }
154
155    /**
156     * Returns a hash code for this instance.
157     *
158     * @return A hash code.
159     */
160    @Override
161    public int hashCode() {
162        int result = 127;
163        result = HashUtilities.hashCode(result, this.formatPattern);
164        return result;
165    }
166
167}