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