001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * -----------------------------------------
028 * StandardCategorySeriesLabelGenerator.java
029 * -----------------------------------------
030 * (C) Copyright 2005-2008, 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 *
042 */
043
044package org.jfree.chart.labels;
045
046import java.io.Serializable;
047import java.text.MessageFormat;
048
049import org.jfree.chart.HashUtilities;
050import org.jfree.data.category.CategoryDataset;
051import org.jfree.util.PublicCloneable;
052
053/**
054 * A standard series label generator for plots that use data from
055 * a {@link org.jfree.data.category.CategoryDataset}.
056 */
057public class StandardCategorySeriesLabelGenerator implements
058    CategorySeriesLabelGenerator, Cloneable, PublicCloneable, Serializable {
059
060    /** For serialization. */
061    private static final long serialVersionUID = 4630760091523940820L;
062
063    /** The default item label format. */
064    public static final String DEFAULT_LABEL_FORMAT = "{0}";
065
066    /** The format pattern. */
067    private String formatPattern;
068
069    /**
070     * Creates a default series label generator (uses
071     * {@link #DEFAULT_LABEL_FORMAT}).
072     */
073    public StandardCategorySeriesLabelGenerator() {
074        this(DEFAULT_LABEL_FORMAT);
075    }
076
077    /**
078     * Creates a new series label generator.
079     *
080     * @param format  the format pattern (<code>null</code> not permitted).
081     */
082    public StandardCategorySeriesLabelGenerator(String format) {
083        if (format == null) {
084            throw new IllegalArgumentException("Null 'format' argument.");
085        }
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    public String generateLabel(CategoryDataset dataset, int series) {
098        if (dataset == null) {
099            throw new IllegalArgumentException("Null 'dataset' argument.");
100        }
101        String label = MessageFormat.format(this.formatPattern,
102                createItemArray(dataset, series));
103        return label;
104    }
105
106    /**
107     * Creates the array of items that can be passed to the
108     * {@link MessageFormat} class for creating labels.
109     *
110     * @param dataset  the dataset (<code>null</code> not permitted).
111     * @param series  the series (zero-based index).
112     *
113     * @return The items (never <code>null</code>).
114     */
115    protected Object[] createItemArray(CategoryDataset dataset, int series) {
116        Object[] result = new Object[1];
117        result[0] = dataset.getRowKey(series).toString();
118        return result;
119    }
120
121    /**
122     * Returns an independent copy of the generator.
123     *
124     * @return A clone.
125     *
126     * @throws CloneNotSupportedException if cloning is not supported.
127     */
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    public boolean equals(Object obj) {
140        if (obj == this) {
141            return true;
142        }
143        if (!(obj instanceof StandardCategorySeriesLabelGenerator)) {
144            return false;
145        }
146        StandardCategorySeriesLabelGenerator that
147                = (StandardCategorySeriesLabelGenerator) 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    public int hashCode() {
160        int result = 127;
161        result = HashUtilities.hashCode(result, this.formatPattern);
162        return result;
163    }
164
165}