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 * DefaultPieDataset.java
029 * ----------------------
030 * (C) Copyright 2001-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Sam (oldman);
034 *                   Tracy Hiltbrand (generics for bug fix to PiePlot);
035 */
036
037package org.jfree.data.general;
038
039import java.io.Serializable;
040import java.util.Collections;
041import java.util.List;
042import org.jfree.chart.util.Args;
043import org.jfree.chart.util.PublicCloneable;
044import org.jfree.chart.util.SortOrder;
045
046import org.jfree.data.DefaultKeyedValues;
047import org.jfree.data.KeyedValues;
048import org.jfree.data.UnknownKeyException;
049
050/**
051 * A default implementation of the {@link PieDataset} interface.
052 * 
053 * @param <K> Key type for PieDataset
054 */
055public class DefaultPieDataset<K extends Comparable<K>> extends AbstractDataset
056        implements PieDataset<K>, Cloneable, PublicCloneable, Serializable {
057
058    /** For serialization. */
059    private static final long serialVersionUID = 2904745139106540618L;
060
061    /** Storage for the data. */
062    private DefaultKeyedValues<K> data;
063
064    /**
065     * Constructs a new dataset, initially empty.
066     */
067    public DefaultPieDataset() {
068        this.data = new DefaultKeyedValues<>();
069    }
070
071    /**
072     * Creates a new dataset by copying data from a {@link KeyedValues}
073     * instance.
074     *
075     * @param source  the data ({@code null} not permitted).
076     */
077    public DefaultPieDataset(KeyedValues<K> source) {
078        Args.nullNotPermitted(source, "source");
079        this.data = new DefaultKeyedValues<>();
080        for (int i = 0; i < source.getItemCount(); i++) {
081            this.data.addValue(source.getKey(i), source.getValue(i));
082        }
083    }
084
085    /**
086     * Returns the number of items in the dataset.
087     *
088     * @return The item count.
089     */
090    @Override
091    public int getItemCount() {
092        return this.data.getItemCount();
093    }
094
095    /**
096     * Returns the categories in the dataset.  The returned list is
097     * unmodifiable.
098     *
099     * @return The categories in the dataset.
100     */
101    @Override
102    public List<K> getKeys() {
103        return Collections.unmodifiableList(this.data.getKeys());
104    }
105
106    /**
107     * Returns the key for the specified item, or {@code null}.
108     *
109     * @param item  the item index (in the range {@code 0} to
110     *     {@code getItemCount() - 1}).
111     *
112     * @return The key, or {@code null}.
113     *
114     * @throws IndexOutOfBoundsException if {@code item} is not in the
115     *     specified range.
116     */
117    @Override
118    public K getKey(int item) {
119        return this.data.getKey(item);
120    }
121
122    /**
123     * Returns the index for a key, or -1 if the key is not recognised.
124     *
125     * @param key  the key ({@code null} not permitted).
126     *
127     * @return The index, or {@code -1} if the key is unrecognised.
128     *
129     * @throws IllegalArgumentException if {@code key} is
130     *     {@code null}.
131     */
132    @Override
133    public int getIndex(K key) {
134        return this.data.getIndex(key);
135    }
136
137    /**
138     * Returns a value.
139     *
140     * @param item  the value index.
141     *
142     * @return The value (possibly {@code null}).
143     */
144    @Override
145    public Number getValue(int item) {
146        return this.data.getValue(item);
147    }
148
149    /**
150     * Returns the data value associated with a key.
151     *
152     * @param key  the key ({@code null} not permitted).
153     *
154     * @return The value (possibly {@code null}).
155     *
156     * @throws UnknownKeyException if the key is not recognised.
157     */
158    @Override
159    public Number getValue(K key) {
160        Args.nullNotPermitted(key, "key");
161        return this.data.getValue(key);
162    }
163
164    /**
165     * Sets the data value for a key and sends a {@link DatasetChangeEvent} to
166     * all registered listeners.
167     *
168     * @param key  the key ({@code null} not permitted).
169     * @param value  the value.
170     *
171     * @throws IllegalArgumentException if {@code key} is
172     *     {@code null}.
173     */
174    public void setValue(K key, Number value) {
175        this.data.setValue(key, value);
176        fireDatasetChanged();
177    }
178
179    /**
180     * Sets the data value for a key and sends a {@link DatasetChangeEvent} to
181     * all registered listeners.
182     *
183     * @param key  the key ({@code null} not permitted).
184     * @param value  the value.
185     *
186     * @throws IllegalArgumentException if {@code key} is
187     *     {@code null}.
188     */
189    public void setValue(K key, double value) {
190        setValue(key, Double.valueOf(value));
191    }
192
193    /**
194     * Inserts a new value at the specified position in the dataset or, if
195     * there is an existing item with the specified key, updates the value
196     * for that item and moves it to the specified position.  After the change
197     * is made, this methods sends a {@link DatasetChangeEvent} to all
198     * registered listeners.
199     *
200     * @param position  the position (in the range 0 to getItemCount()).
201     * @param key  the key ({@code null} not permitted).
202     * @param value  the value ({@code null} permitted).
203     */
204    public void insertValue(int position, K key, double value) {
205        insertValue(position, key, Double.valueOf(value));
206    }
207
208    /**
209     * Inserts a new value at the specified position in the dataset or, if
210     * there is an existing item with the specified key, updates the value
211     * for that item and moves it to the specified position.  After the change
212     * is made, this methods sends a {@link DatasetChangeEvent} to all
213     * registered listeners.
214     *
215     * @param position  the position (in the range 0 to getItemCount()).
216     * @param key  the key ({@code null} not permitted).
217     * @param value  the value ({@code null} permitted).
218     */
219    public void insertValue(int position, K key, Number value) {
220        this.data.insertValue(position, key, value);
221        fireDatasetChanged();
222    }
223
224    /**
225     * Removes an item from the dataset and sends a {@link DatasetChangeEvent}
226     * to all registered listeners.
227     *
228     * @param key  the key ({@code null} not permitted).
229     *
230     * @throws IllegalArgumentException if {@code key} is
231     *     {@code null}.
232     */
233    public void remove(K key) {
234        this.data.removeValue(key);
235        fireDatasetChanged();
236    }
237
238    /**
239     * Clears all data from this dataset and sends a {@link DatasetChangeEvent}
240     * to all registered listeners (unless the dataset was already empty).
241     */
242    public void clear() {
243        if (getItemCount() > 0) {
244            this.data.clear();
245            fireDatasetChanged();
246        }
247    }
248
249    /**
250     * Sorts the dataset's items by key and sends a {@link DatasetChangeEvent}
251     * to all registered listeners.
252     *
253     * @param order  the sort order ({@code null} not permitted).
254     */
255    public void sortByKeys(SortOrder order) {
256        this.data.sortByKeys(order);
257        fireDatasetChanged();
258    }
259
260    /**
261     * Sorts the dataset's items by value and sends a {@link DatasetChangeEvent}
262     * to all registered listeners.
263     *
264     * @param order  the sort order ({@code null} not permitted).
265     */
266    public void sortByValues(SortOrder order) {
267        this.data.sortByValues(order);
268        fireDatasetChanged();
269    }
270
271    /**
272     * Tests if this object is equal to another.
273     *
274     * @param obj  the other object.
275     *
276     * @return A boolean.
277     */
278    @Override
279    public boolean equals(Object obj) {
280        if (obj == this) {
281            return true;
282        }
283
284        if (!(obj instanceof PieDataset)) {
285            return false;
286        }
287        @SuppressWarnings("unchecked")
288        PieDataset<K> that = (PieDataset<K>) obj;
289        int count = getItemCount();
290        if (that.getItemCount() != count) {
291            return false;
292        }
293
294        for (int i = 0; i < count; i++) {
295            K k1 = getKey(i);
296            K k2 = that.getKey(i);
297            if (!k1.equals(k2)) {
298                return false;
299            }
300
301            Number v1 = getValue(i);
302            Number v2 = that.getValue(i);
303            if (v1 == null) {
304                if (v2 != null) {
305                    return false;
306                }
307            }
308            else {
309                if (!v1.equals(v2)) {
310                    return false;
311                }
312            }
313        }
314        return true;
315
316    }
317
318    /**
319     * Returns a hash code.
320     *
321     * @return A hash code.
322     */
323    @Override
324    public int hashCode() {
325        return this.data.hashCode();
326    }
327
328    /**
329     * Returns a clone of the dataset.
330     *
331     * @return A clone.
332     *
333     * @throws CloneNotSupportedException This class will not throw this
334     *         exception, but subclasses (if any) might.
335     */
336    @Override
337    @SuppressWarnings("unchecked")
338    public Object clone() throws CloneNotSupportedException {
339        DefaultPieDataset<K> clone = (DefaultPieDataset<K>) super.clone();
340        clone.data = (DefaultKeyedValues) this.data.clone();
341        return clone;
342    }
343
344}