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