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 * AbstractPieItemLabelGenerator.java
029 * ----------------------------------
030 * (C) Copyright 2004-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 09-Nov-2004 : Version 1, draws out code from StandardPieItemLabelGenerator
038 *               and StandardPieToolTipGenerator (DG);
039 * ------------- JFREECHART 1.0.x ---------------------------------------------
040 * 03-May-2006 : Fixed bug 1480978, a problem in the clone() method (DG);
041 * 23-Nov-2007 : Implemented hashCode() (DG);
042 * 03-Jul-2013 : Use ParamChecks (DG);
043 *
044 */
045
046package org.jfree.chart.labels;
047
048import java.io.Serializable;
049import java.text.MessageFormat;
050import java.text.NumberFormat;
051
052import org.jfree.chart.HashUtilities;
053import org.jfree.chart.util.ParamChecks;
054import org.jfree.data.general.DatasetUtilities;
055import org.jfree.data.general.PieDataset;
056
057/**
058 * A base class used for generating pie chart item labels.
059 */
060public class AbstractPieItemLabelGenerator implements Serializable {
061
062    /** For serialization. */
063    private static final long serialVersionUID = 7347703325267846275L;
064
065    /** The label format string. */
066    private String labelFormat;
067
068    /** A number formatter for the value. */
069    private NumberFormat numberFormat;
070
071    /** A number formatter for the percentage. */
072    private NumberFormat percentFormat;
073
074    /**
075     * Creates an item label generator using the specified number formatters.
076     *
077     * @param labelFormat  the label format string (<code>null</code> not
078     *                     permitted).
079     * @param numberFormat  the format object for the values (<code>null</code>
080     *                      not permitted).
081     * @param percentFormat  the format object for the percentages
082     *                       (<code>null</code> not permitted).
083     */
084    protected AbstractPieItemLabelGenerator(String labelFormat, 
085            NumberFormat numberFormat, NumberFormat percentFormat) {
086        ParamChecks.nullNotPermitted(labelFormat, "labelFormat");
087        ParamChecks.nullNotPermitted(numberFormat, "numberFormat");
088        ParamChecks.nullNotPermitted(percentFormat, "percentFormat");
089        this.labelFormat = labelFormat;
090        this.numberFormat = numberFormat;
091        this.percentFormat = percentFormat;
092    }
093
094    /**
095     * Returns the label format string.
096     *
097     * @return The label format string (never <code>null</code>).
098     */
099    public String getLabelFormat() {
100        return this.labelFormat;
101    }
102
103    /**
104     * Returns the number formatter.
105     *
106     * @return The formatter (never <code>null</code>).
107     */
108    public NumberFormat getNumberFormat() {
109        return this.numberFormat;
110    }
111
112    /**
113     * Returns the percent formatter.
114     *
115     * @return The formatter (never <code>null</code>).
116     */
117    public NumberFormat getPercentFormat() {
118        return this.percentFormat;
119    }
120
121    /**
122     * Creates the array of items that can be passed to the
123     * {@link MessageFormat} class for creating labels.  The returned array
124     * contains four values:
125     * <ul>
126     * <li>result[0] = the section key converted to a <code>String</code>;</li>
127     * <li>result[1] = the formatted data value;</li>
128     * <li>result[2] = the formatted percentage (of the total);</li>
129     * <li>result[3] = the formatted total value.</li>
130     * </ul>
131     *
132     * @param dataset  the dataset (<code>null</code> not permitted).
133     * @param key  the key (<code>null</code> not permitted).
134     *
135     * @return The items (never <code>null</code>).
136     */
137    protected Object[] createItemArray(PieDataset dataset, Comparable key) {
138        Object[] result = new Object[4];
139        double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
140        result[0] = key.toString();
141        Number value = dataset.getValue(key);
142        if (value != null) {
143            result[1] = this.numberFormat.format(value);
144        }
145        else {
146            result[1] = "null";
147        }
148        double percent = 0.0;
149        if (value != null) {
150            double v = value.doubleValue();
151            if (v > 0.0) {
152                percent = v / total;
153            }
154        }
155        result[2] = this.percentFormat.format(percent);
156        result[3] = this.numberFormat.format(total);
157        return result;
158    }
159
160    /**
161     * Generates a label for a pie section.
162     *
163     * @param dataset  the dataset (<code>null</code> not permitted).
164     * @param key  the section key (<code>null</code> not permitted).
165     *
166     * @return The label (possibly <code>null</code>).
167     */
168    protected String generateSectionLabel(PieDataset dataset, Comparable key) {
169        String result = null;
170        if (dataset != null) {
171            Object[] items = createItemArray(dataset, key);
172            result = MessageFormat.format(this.labelFormat, items);
173        }
174        return result;
175    }
176
177    /**
178     * Tests the generator for equality with an arbitrary object.
179     *
180     * @param obj  the object to test against (<code>null</code> permitted).
181     *
182     * @return A boolean.
183     */
184    @Override
185    public boolean equals(Object obj) {
186        if (obj == this) {
187            return true;
188        }
189        if (!(obj instanceof AbstractPieItemLabelGenerator)) {
190            return false;
191        }
192
193        AbstractPieItemLabelGenerator that
194                = (AbstractPieItemLabelGenerator) obj;
195        if (!this.labelFormat.equals(that.labelFormat)) {
196            return false;
197        }
198        if (!this.numberFormat.equals(that.numberFormat)) {
199            return false;
200        }
201        if (!this.percentFormat.equals(that.percentFormat)) {
202            return false;
203        }
204        return true;
205
206    }
207
208    /**
209     * Returns a hash code for this instance.
210     *
211     * @return A hash code.
212     */
213    @Override
214    public int hashCode() {
215        int result = 127;
216        result = HashUtilities.hashCode(result, this.labelFormat);
217        result = HashUtilities.hashCode(result, this.numberFormat);
218        result = HashUtilities.hashCode(result, this.percentFormat);
219        return result;
220    }
221
222    /**
223     * Returns an independent copy of the generator.
224     *
225     * @return A clone.
226     *
227     * @throws CloneNotSupportedException  should not happen.
228     */
229    @Override
230    public Object clone() throws CloneNotSupportedException {
231        AbstractPieItemLabelGenerator clone
232                = (AbstractPieItemLabelGenerator) super.clone();
233        if (this.numberFormat != null) {
234            clone.numberFormat = (NumberFormat) this.numberFormat.clone();
235        }
236        if (this.percentFormat != null) {
237            clone.percentFormat = (NumberFormat) this.percentFormat.clone();
238        }
239        return clone;
240    }
241
242}