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