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 * AbstractCategoryItemLabelGenerator.java
029 * ---------------------------------------
030 * (C) Copyright 2005-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
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.Objects;
044
045import org.jfree.chart.HashUtils;
046import org.jfree.chart.util.Args;
047import org.jfree.chart.util.PublicCloneable;
048import org.jfree.data.DataUtils;
049import org.jfree.data.category.CategoryDataset;
050
051/**
052 * A base class that can be used to create a label or tooltip generator that
053 * can be assigned to a
054 * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
055 */
056public abstract class AbstractCategoryItemLabelGenerator
057        implements PublicCloneable, Cloneable, Serializable {
058
059    /** For serialization. */
060    private static final long serialVersionUID = -7108591260223293197L;
061
062    /**
063     * The label format string used by a {@code MessageFormat} object to
064     * combine the standard items:  {0} = series name, {1} = category,
065     * {2} = value, {3} = value as a percentage of the column total.
066     */
067    private final String labelFormat;
068
069    /** The string used to represent a null value. */
070    private final String nullValueString;
071
072    /**
073     * A number formatter used to preformat the value before it is passed to
074     * the MessageFormat object.
075     */
076    private NumberFormat numberFormat;
077
078    /**
079     * A date formatter used to preformat the value before it is passed to the
080     * MessageFormat object.
081     */
082    private DateFormat dateFormat;
083
084    /**
085     * A number formatter used to preformat the percentage value before it is
086     * passed to the MessageFormat object.
087     */
088    private final NumberFormat percentFormat;
089
090    /**
091     * Creates a label generator with the specified number formatter.
092     *
093     * @param labelFormat  the label format string ({@code null} not
094     *                     permitted).
095     * @param formatter  the number formatter ({@code null} not permitted).
096     */
097    protected AbstractCategoryItemLabelGenerator(String labelFormat,
098                                                 NumberFormat formatter) {
099        this(labelFormat, formatter, NumberFormat.getPercentInstance());
100    }
101
102    /**
103     * Creates a label generator with the specified number formatter.
104     *
105     * @param labelFormat  the label format string ({@code null} not
106     *                     permitted).
107     * @param formatter  the number formatter ({@code null} not permitted).
108     * @param percentFormatter  the percent formatter ({@code null} not
109     *     permitted).
110     */
111    protected AbstractCategoryItemLabelGenerator(String labelFormat,
112            NumberFormat formatter, NumberFormat percentFormatter) {
113        Args.nullNotPermitted(labelFormat, "labelFormat");
114        Args.nullNotPermitted(formatter, "formatter");
115        Args.nullNotPermitted(percentFormatter, "percentFormatter");
116        this.labelFormat = labelFormat;
117        this.numberFormat = formatter;
118        this.percentFormat = percentFormatter;
119        this.dateFormat = null;
120        this.nullValueString = "-";
121    }
122
123    /**
124     * Creates a label generator with the specified date formatter.
125     *
126     * @param labelFormat  the label format string ({@code null} not
127     *                     permitted).
128     * @param formatter  the date formatter ({@code null} not permitted).
129     */
130    protected AbstractCategoryItemLabelGenerator(String labelFormat,
131            DateFormat formatter) {
132        Args.nullNotPermitted(labelFormat, "labelFormat");
133        Args.nullNotPermitted(formatter, "formatter");
134        this.labelFormat = labelFormat;
135        this.numberFormat = null;
136        this.percentFormat = NumberFormat.getPercentInstance();
137        this.dateFormat = formatter;
138        this.nullValueString = "-";
139    }
140
141    /**
142     * Generates a label for the specified row.
143     *
144     * @param dataset  the dataset ({@code null} not permitted).
145     * @param row  the row index (zero-based).
146     *
147     * @return The label.
148     */
149    public String generateRowLabel(CategoryDataset dataset, int row) {
150        return dataset.getRowKey(row).toString();
151    }
152
153    /**
154     * Generates a label for the specified row.
155     *
156     * @param dataset  the dataset ({@code null} not permitted).
157     * @param column  the column index (zero-based).
158     *
159     * @return The label.
160     */
161    public String generateColumnLabel(CategoryDataset dataset, int column) {
162        return dataset.getColumnKey(column).toString();
163    }
164
165    /**
166     * Returns the label format string.
167     *
168     * @return The label format string (never {@code null}).
169     */
170    public String getLabelFormat() {
171        return this.labelFormat;
172    }
173
174    /**
175     * Returns the number formatter.
176     *
177     * @return The number formatter (possibly {@code null}).
178     */
179    public NumberFormat getNumberFormat() {
180        return this.numberFormat;
181    }
182
183    /**
184     * Returns the date formatter.
185     *
186     * @return The date formatter (possibly {@code null}).
187     */
188    public DateFormat getDateFormat() {
189        return this.dateFormat;
190    }
191
192    /**
193     * Generates a for the specified item.
194     *
195     * @param dataset  the dataset ({@code null} not permitted).
196     * @param row  the row index (zero-based).
197     * @param column  the column index (zero-based).
198     *
199     * @return The label (possibly {@code null}).
200     */
201    protected String generateLabelString(CategoryDataset dataset,
202                                         int row, int column) {
203        Args.nullNotPermitted(dataset, "dataset");
204        String result;
205        Object[] items = createItemArray(dataset, row, column);
206        result = MessageFormat.format(this.labelFormat, items);
207        return result;
208
209    }
210
211    /**
212     * Creates the array of items that can be passed to the
213     * {@link MessageFormat} class for creating labels.
214     *
215     * @param dataset  the dataset ({@code null} not permitted).
216     * @param row  the row index (zero-based).
217     * @param column  the column index (zero-based).
218     *
219     * @return The items (never {@code null}).
220     */
221    protected Object[] createItemArray(CategoryDataset dataset,
222                                       int row, int column) {
223        Object[] result = new Object[4];
224        result[0] = dataset.getRowKey(row).toString();
225        result[1] = dataset.getColumnKey(column).toString();
226        Number value = dataset.getValue(row, column);
227        if (value != null) {
228            if (this.numberFormat != null) {
229                result[2] = this.numberFormat.format(value);
230            }
231            else if (this.dateFormat != null) {
232                result[2] = this.dateFormat.format(value);
233            }
234        }
235        else {
236            result[2] = this.nullValueString;
237        }
238        if (value != null) {
239            double total = DataUtils.calculateColumnTotal(dataset, column);
240            double percent = value.doubleValue() / total;
241            result[3] = this.percentFormat.format(percent);
242        }
243
244        return result;
245    }
246
247    /**
248     * Tests this object for equality with an arbitrary object.
249     *
250     * @param obj  the other object ({@code null} permitted).
251     *
252     * @return A boolean.
253     */
254    @Override
255    public boolean equals(Object obj) {
256        if (obj == this) {
257            return true;
258        }
259        if (!(obj instanceof AbstractCategoryItemLabelGenerator)) {
260            return false;
261        }
262
263        AbstractCategoryItemLabelGenerator that
264            = (AbstractCategoryItemLabelGenerator) obj;
265        if (!Objects.equals(this.labelFormat, that.labelFormat)) {
266            return false;
267        }
268        if (!Objects.equals(this.dateFormat, that.dateFormat)) {
269            return false;
270        }
271        if (!Objects.equals(this.nullValueString, that.nullValueString)) {
272            return false;
273        }
274        if (!Objects.equals(this.numberFormat, that.numberFormat)) {
275            return false;
276        }
277        if (!Objects.equals(this.percentFormat, that.percentFormat)) {
278            return false;
279        }
280        if (!that.canEqual(this)) {
281            return false;
282        }
283        return true;
284    }
285
286    public boolean canEqual(Object other) {
287        // fix the "equals not symmetric" problem
288        return (other instanceof AbstractCategoryItemLabelGenerator);
289    }
290
291    /**
292     * Returns a hash code for this instance.
293     *
294     * @return A hash code.
295     */
296    @Override
297    public int hashCode() {
298        int result = 127;
299        result = HashUtils.hashCode(result, this.labelFormat);
300        result = HashUtils.hashCode(result, this.nullValueString);
301        result = HashUtils.hashCode(result, this.dateFormat);
302        result = HashUtils.hashCode(result, this.numberFormat);
303        result = HashUtils.hashCode(result, this.percentFormat);
304        return result;
305    }
306
307    /**
308     * Returns an independent copy of the generator.
309     *
310     * @return A clone.
311     *
312     * @throws CloneNotSupportedException  should not happen.
313     */
314    @Override
315    public Object clone() throws CloneNotSupportedException {
316        AbstractCategoryItemLabelGenerator clone
317            = (AbstractCategoryItemLabelGenerator) super.clone();
318        if (this.numberFormat != null) {
319            clone.numberFormat = (NumberFormat) this.numberFormat.clone();
320        }
321        if (this.dateFormat != null) {
322            clone.dateFormat = (DateFormat) this.dateFormat.clone();
323        }
324        return clone;
325    }
326
327}