KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > category > DefaultCategoryDataset


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------------------
28  * DefaultCategoryDataset.java
29  * ---------------------------
30  * (C) Copyright 2002-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DefaultCategoryDataset.java,v 1.5.2.2 2005/10/25 21:29:58 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 21-Jan-2003 : Added standard header, and renamed DefaultCategoryDataset (DG);
40  * 13-Mar-2003 : Inserted DefaultKeyedValues2DDataset into class hierarchy (DG);
41  * 06-Oct-2003 : Added incrementValue() method (DG);
42  * 05-Apr-2004 : Added clear() method (DG);
43  * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.category (DG);
44  *
45  */

46
47 package org.jfree.data.category;
48
49 import java.io.Serializable JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.jfree.data.DefaultKeyedValues2D;
53 import org.jfree.data.UnknownKeyException;
54 import org.jfree.data.general.AbstractDataset;
55 import org.jfree.data.general.DatasetChangeEvent;
56
57 /**
58  * A default implementation of the {@link CategoryDataset} interface.
59  */

60 public class DefaultCategoryDataset extends AbstractDataset
61                                     implements CategoryDataset, Serializable JavaDoc {
62
63     /** For serialization. */
64     private static final long serialVersionUID = -8168173757291644622L;
65     
66     /** A storage structure for the data. */
67     private DefaultKeyedValues2D data;
68
69     /**
70      * Creates a new (empty) dataset.
71      */

72     public DefaultCategoryDataset() {
73         this.data = new DefaultKeyedValues2D();
74     }
75
76     /**
77      * Returns the number of rows in the table.
78      *
79      * @return The row count.
80      */

81     public int getRowCount() {
82         return this.data.getRowCount();
83     }
84
85     /**
86      * Returns the number of columns in the table.
87      *
88      * @return The column count.
89      */

90     public int getColumnCount() {
91         return this.data.getColumnCount();
92     }
93
94     /**
95      * Returns a value from the table.
96      *
97      * @param row the row index (zero-based).
98      * @param column the column index (zero-based).
99      *
100      * @return The value (possibly <code>null</code>).
101      */

102     public Number JavaDoc getValue(int row, int column) {
103         return this.data.getValue(row, column);
104     }
105
106     /**
107      * Returns a row key.
108      *
109      * @param row the row index (zero-based).
110      *
111      * @return The row key.
112      */

113     public Comparable JavaDoc getRowKey(int row) {
114         return this.data.getRowKey(row);
115     }
116
117     /**
118      * Returns the row index for a given key.
119      *
120      * @param key the row key.
121      *
122      * @return The row index.
123      */

124     public int getRowIndex(Comparable JavaDoc key) {
125         return this.data.getRowIndex(key);
126     }
127
128     /**
129      * Returns the row keys.
130      *
131      * @return The keys.
132      */

133     public List JavaDoc getRowKeys() {
134         return this.data.getRowKeys();
135     }
136
137     /**
138      * Returns a column key.
139      *
140      * @param column the column index (zero-based).
141      *
142      * @return The column key.
143      */

144     public Comparable JavaDoc getColumnKey(int column) {
145         return this.data.getColumnKey(column);
146     }
147
148     /**
149      * Returns the column index for a given key.
150      *
151      * @param key the column key.
152      *
153      * @return The column index.
154      */

155     public int getColumnIndex(Comparable JavaDoc key) {
156         return this.data.getColumnIndex(key);
157     }
158
159     /**
160      * Returns the column keys.
161      *
162      * @return The keys.
163      */

164     public List JavaDoc getColumnKeys() {
165         return this.data.getColumnKeys();
166     }
167
168     /**
169      * Returns the value for a pair of keys.
170      *
171      * @param rowKey the row key (<code>null</code> not permitted).
172      * @param columnKey the column key (<code>null</code> not permitted).
173      *
174      * @return The value (possibly <code>null</code>).
175      *
176      * @throws UnknownKeyException if either key is not defined in the dataset.
177      */

178     public Number JavaDoc getValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
179         return this.data.getValue(rowKey, columnKey);
180     }
181
182     /**
183      * Adds a value to the table. Performs the same function as setValue().
184      *
185      * @param value the value.
186      * @param rowKey the row key.
187      * @param columnKey the column key.
188      */

189     public void addValue(Number JavaDoc value, Comparable JavaDoc rowKey,
190                          Comparable JavaDoc columnKey) {
191         this.data.addValue(value, rowKey, columnKey);
192         fireDatasetChanged();
193     }
194
195     /**
196      * Adds a value to the table.
197      *
198      * @param value the value.
199      * @param rowKey the row key.
200      * @param columnKey the column key.
201      */

202     public void addValue(double value, Comparable JavaDoc rowKey,
203                          Comparable JavaDoc columnKey) {
204         addValue(new Double JavaDoc(value), rowKey, columnKey);
205     }
206
207     /**
208      * Adds or updates a value in the table and sends a
209      * {@link DatasetChangeEvent} to all registered listeners.
210      *
211      * @param value the value (<code>null</code> permitted).
212      * @param rowKey the row key (<code>null</code> not permitted).
213      * @param columnKey the column key (<code>null</code> not permitted).
214      */

215     public void setValue(Number JavaDoc value, Comparable JavaDoc rowKey,
216                          Comparable JavaDoc columnKey) {
217         this.data.setValue(value, rowKey, columnKey);
218         fireDatasetChanged();
219     }
220
221     /**
222      * Adds or updates a value in the table and sends a
223      * {@link DatasetChangeEvent} to all registered listeners.
224      *
225      * @param value the value.
226      * @param rowKey the row key (<code>null</code> not permitted).
227      * @param columnKey the column key (<code>null</code> not permitted).
228      */

229     public void setValue(double value, Comparable JavaDoc rowKey,
230                          Comparable JavaDoc columnKey) {
231         setValue(new Double JavaDoc(value), rowKey, columnKey);
232     }
233
234     /**
235      * Adds the specified value to an existing value in the dataset (if the
236      * existing value is <code>null</code>, it is treated as if it were 0.0).
237      *
238      * @param value the value.
239      * @param rowKey the row key (<code>null</code> not permitted).
240      * @param columnKey the column key (<code>null</code> not permitted).
241      *
242      * @throws UnknownKeyException if either key is not defined in the dataset.
243      */

244     public void incrementValue(double value,
245                                Comparable JavaDoc rowKey,
246                                Comparable JavaDoc columnKey) {
247         double existing = 0.0;
248         Number JavaDoc n = getValue(rowKey, columnKey);
249         if (n != null) {
250             existing = n.doubleValue();
251         }
252         setValue(existing + value, rowKey, columnKey);
253     }
254     
255     /**
256      * Removes a value from the dataset.
257      *
258      * @param rowKey the row key.
259      * @param columnKey the column key.
260      */

261     public void removeValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
262         this.data.removeValue(rowKey, columnKey);
263         fireDatasetChanged();
264     }
265
266     /**
267      * Removes a row from the dataset.
268      *
269      * @param rowIndex the row index.
270      */

271     public void removeRow(int rowIndex) {
272         this.data.removeRow(rowIndex);
273         fireDatasetChanged();
274     }
275
276     /**
277      * Removes a row from the dataset.
278      *
279      * @param rowKey the row key.
280      */

281     public void removeRow(Comparable JavaDoc rowKey) {
282         this.data.removeRow(rowKey);
283         fireDatasetChanged();
284     }
285
286     /**
287      * Removes a column from the dataset.
288      *
289      * @param columnIndex the column index.
290      */

291     public void removeColumn(int columnIndex) {
292         this.data.removeColumn(columnIndex);
293         fireDatasetChanged();
294     }
295
296     /**
297      * Removes a column from the dataset.
298      *
299      * @param columnKey the column key.
300      */

301     public void removeColumn(Comparable JavaDoc columnKey) {
302         this.data.removeColumn(columnKey);
303         fireDatasetChanged();
304     }
305
306     /**
307      * Clears all data from the dataset and sends a {@link DatasetChangeEvent}
308      * to all registered listeners.
309      */

310     public void clear() {
311         this.data.clear();
312         fireDatasetChanged();
313     }
314     
315     /**
316      * Tests if this object is equal to another.
317      *
318      * @param obj the other object.
319      *
320      * @return A boolean.
321      */

322     public boolean equals(Object JavaDoc obj) {
323
324         if (obj == this) {
325             return true;
326         }
327
328         if (!(obj instanceof CategoryDataset)) {
329             return false;
330         }
331
332         CategoryDataset that = (CategoryDataset) obj;
333         if (!getRowKeys().equals(that.getRowKeys())) {
334             return false;
335         }
336
337         if (!getColumnKeys().equals(that.getColumnKeys())) {
338             return false;
339         }
340
341         int rowCount = getRowCount();
342         int colCount = getColumnCount();
343         for (int r = 0; r < rowCount; r++) {
344             for (int c = 0; c < colCount; c++) {
345                 Number JavaDoc v1 = getValue(r, c);
346                 Number JavaDoc v2 = that.getValue(r, c);
347                 if (v1 == null) {
348                     if (v2 != null) {
349                         return false;
350                     }
351                 }
352                 else if (!v1.equals(v2)) {
353                     return false;
354                 }
355             }
356         }
357         return true;
358     }
359
360     /**
361      * Returns a hash code for the dataset.
362      *
363      * @return A hash code.
364      */

365     public int hashCode() {
366         return this.data.hashCode();
367     }
368     
369 }
370
Popular Tags