KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > DefaultPieDataset


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ----------------------
23  * DefaultPieDataset.java
24  * ----------------------
25  * (C) Copyright 2001-2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Sam (oldman);
29  *
30  * $Id: DefaultPieDataset.java,v 1.6 2003/10/30 09:53:06 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 17-Nov-2001 : Version 1 (DG);
35  * 22-Jan-2002 : Removed legend methods from dataset implementations (DG);
36  * 07-Apr-2002 : Modified implementation to guarantee data sequence to remain in the order
37  * categories are added (oldman);
38  * 23-Oct-2002 : Added getCategory(int) method and getItemCount() method, in line with changes
39  * to the PieDataset interface (DG);
40  * 04-Feb-2003 : Changed underlying data storage to DefaultKeyedValues (DG);
41  * 04-Mar-2003 : Inserted DefaultKeyedValuesDataset class into hierarchy (DG);
42  * 24-Apr-2003 : Switched places with DefaultKeyedValuesDataset (DG);
43  * 18-Aug-2003 : Implemented Cloneable (DG);
44  *
45  */

46
47 package org.jfree.data;
48
49 import java.io.Serializable JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.List JavaDoc;
52
53 /**
54  * A default implementation of the {@link PieDataset} interface.
55  *
56  * @author David Gilbert
57  */

58 public class DefaultPieDataset extends AbstractDataset
59                                implements PieDataset, Cloneable JavaDoc, Serializable JavaDoc {
60
61     /** Storage for the data. */
62     private DefaultKeyedValues data;
63
64     /**
65      * Constructs a new dataset, initially empty.
66      */

67     public DefaultPieDataset() {
68
69         this.data = new DefaultKeyedValues();
70
71     }
72
73     /**
74      * Creates a new dataset that uses the data from a {@link KeyedValues} instance.
75      *
76      * @param data the data.
77      */

78     public DefaultPieDataset(KeyedValues data) {
79
80         this.data = new DefaultKeyedValues();
81         for (int i = 0; i < data.getItemCount(); i++) {
82             this.data.addValue(data.getKey(i), data.getValue(i));
83         }
84     }
85
86     /**
87      * Returns the number of items in the dataset.
88      *
89      * @return the item count.
90      */

91     public int getItemCount() {
92         return this.data.getItemCount();
93     }
94
95     /**
96      * Returns the categories in the dataset.
97      *
98      * @return the categories in the dataset.
99      */

100     public List JavaDoc getKeys() {
101         return Collections.unmodifiableList(this.data.getKeys());
102     }
103
104     /**
105      * Returns the key for an item.
106      *
107      * @param item the item index (zero-based).
108      *
109      * @return the category.
110      */

111     public Comparable JavaDoc getKey(int item) {
112
113         Comparable JavaDoc result = null;
114         if (getItemCount() > item) {
115             result = this.data.getKey(item);
116         }
117         return result;
118
119     }
120
121     /**
122      * Returns the index for a key.
123      *
124      * @param key the key.
125      *
126      * @return the key index.
127      */

128     public int getIndex(Comparable JavaDoc key) {
129
130         return this.data.getIndex(key);
131
132     }
133
134     /**
135      * Returns a value.
136      *
137      * @param item the value index.
138      *
139      * @return the value (possibly <code>null</code>).
140      */

141     public Number JavaDoc getValue(int item) {
142
143         Number JavaDoc result = null;
144         if (getItemCount() > item) {
145             result = this.data.getValue(item);
146         }
147         return result;
148
149     }
150
151     /**
152      * Returns the data value associated with a key.
153      *
154      * @param key the key.
155      *
156      * @return The value (possibly <code>null</code>).
157      */

158     public Number JavaDoc getValue(Comparable JavaDoc key) {
159
160         // check arguments...
161
if (key == null) {
162             throw new IllegalArgumentException JavaDoc("PieDataset: null key not allowed.");
163         }
164
165         // fetch the value...
166
return this.data.getValue(key);
167
168     }
169
170     /**
171      * Sets the data value for a key.
172      *
173      * @param key the key.
174      * @param value the value.
175      */

176     public void setValue(Comparable JavaDoc key, Number JavaDoc value) {
177
178         this.data.setValue(key, value);
179         fireDatasetChanged();
180
181     }
182
183     /**
184      * Sets the data value for a key.
185      *
186      * @param key the key.
187      * @param value the value.
188      */

189     public void setValue(Comparable JavaDoc key, double value) {
190
191         setValue(key, new Double JavaDoc(value));
192
193     }
194
195     /**
196      * Tests if this object is equal to another.
197      *
198      * @param o the other object.
199      *
200      * @return A boolean.
201      */

202     public boolean equals(Object JavaDoc o) {
203
204         if (o == null) {
205             return false;
206         }
207         if (o == this) {
208             return true;
209         }
210
211         if (o instanceof PieDataset) {
212             PieDataset pd = (PieDataset) o;
213             int count = getItemCount();
214             for (int i = 0; i < count; i++) {
215                 Comparable JavaDoc k1 = getKey(i);
216                 Comparable JavaDoc k2 = pd.getKey(i);
217                 if (k1.equals(k2)) {
218                     Number JavaDoc v1 = getValue(i);
219                     Number JavaDoc v2 = pd.getValue(i);
220                     if (v1 == null) {
221                         if (v2 != null) {
222                             return false;
223                         }
224                     }
225                     else {
226                         if (!v1.equals(v2)) {
227                             return false;
228                         }
229                     }
230                 }
231                 else {
232                     return false;
233                 }
234             }
235             return true;
236         }
237
238         return false;
239
240     }
241     
242     /**
243      * Returns a clone.
244      *
245      * @return A clone.
246      *
247      * @throws CloneNotSupportedException This class will not throw this exception, but subclasses
248      * (if any) might.
249      */

250     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
251         DefaultPieDataset clone = (DefaultPieDataset) super.clone();
252         clone.data = (DefaultKeyedValues) this.data.clone();
253         return clone;
254     }
255     
256 }
257
Popular Tags