KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultCategoryDataset.java
24  * ---------------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: DefaultCategoryDataset.java,v 1.5 2003/10/06 10:50:03 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 21-Jan-2003 : Added standard header, and renamed DefaultCategoryDataset (DG);
35  * 13-Mar-2003 : Inserted DefaultKeyedValues2DDataset into class hierarchy (DG);
36  * 06-Oct-2003 : Added incrementValue(...) method (DG);
37  *
38  */

39 package org.jfree.data;
40
41 import java.io.Serializable JavaDoc;
42 import java.util.List JavaDoc;
43
44 /**
45  * A default implementation of the {@link CategoryDataset} interface.
46  *
47  * @author David Gilbert
48  */

49 public class DefaultCategoryDataset extends AbstractDataset
50                                     implements CategoryDataset, Serializable JavaDoc {
51
52     /** A storage structure for the data. */
53     private DefaultKeyedValues2D data;
54
55     /**
56      * Creates a new (empty) dataset.
57      */

58     public DefaultCategoryDataset() {
59         this.data = new DefaultKeyedValues2D();
60     }
61
62     /**
63      * Returns the number of rows in the table.
64      *
65      * @return the row count.
66      */

67     public int getRowCount() {
68         return this.data.getRowCount();
69     }
70
71     /**
72      * Returns the number of columns in the table.
73      *
74      * @return the column count.
75      */

76     public int getColumnCount() {
77         return this.data.getColumnCount();
78     }
79
80     /**
81      * Returns a value from the table.
82      *
83      * @param row the row index (zero-based).
84      * @param column the column index (zero-based).
85      *
86      * @return the value (possibly null).
87      */

88     public Number JavaDoc getValue(int row, int column) {
89         return this.data.getValue(row, column);
90     }
91
92     /**
93      * Returns a row key.
94      *
95      * @param row the row index (zero-based).
96      *
97      * @return the row key.
98      */

99     public Comparable JavaDoc getRowKey(int row) {
100         return this.data.getRowKey(row);
101     }
102
103     /**
104      * Returns the row index for a given key.
105      *
106      * @param key the row key.
107      *
108      * @return the row index.
109      */

110     public int getRowIndex(Comparable JavaDoc key) {
111         return this.data.getRowIndex(key);
112     }
113
114     /**
115      * Returns the row keys.
116      *
117      * @return the keys.
118      */

119     public List JavaDoc getRowKeys() {
120         return this.data.getRowKeys();
121     }
122
123     /**
124      * Returns a column key.
125      *
126      * @param column the column index (zero-based).
127      *
128      * @return the column key.
129      */

130     public Comparable JavaDoc getColumnKey(int column) {
131         return this.data.getColumnKey(column);
132     }
133
134     /**
135      * Returns the column index for a given key.
136      *
137      * @param key the column key.
138      *
139      * @return the column index.
140      */

141     public int getColumnIndex(Comparable JavaDoc key) {
142         return this.data.getColumnIndex(key);
143     }
144
145     /**
146      * Returns the column keys.
147      *
148      * @return the keys.
149      */

150     public List JavaDoc getColumnKeys() {
151         return this.data.getColumnKeys();
152     }
153
154     /**
155      * Returns the value for a pair of keys.
156      * <P>
157      * This method should return <code>null</code> if either of the keys is not found.
158      *
159      * @param rowKey the row key.
160      * @param columnKey the column key.
161      *
162      * @return the value.
163      */

164     public Number JavaDoc getValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
165         return this.data.getValue(rowKey, columnKey);
166     }
167
168     /**
169      * Adds a value to the table. Performs the same function as setValue(...).
170      *
171      * @param value the value.
172      * @param rowKey the row key.
173      * @param columnKey the column key.
174      */

175     public void addValue(Number JavaDoc value, Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
176         this.data.addValue(value, rowKey, columnKey);
177         fireDatasetChanged();
178     }
179
180     /**
181      * Adds a value to the table.
182      *
183      * @param value the value.
184      * @param rowKey the row key.
185      * @param columnKey the column key.
186      */

187     public void addValue(double value, Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
188         this.addValue(new Double JavaDoc(value), rowKey, columnKey);
189     }
190
191     /**
192      * Adds or updates a value in the table.
193      *
194      * @param value the value.
195      * @param rowKey the row key.
196      * @param columnKey the column key.
197      */

198     public void setValue(Number JavaDoc value, Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
199         this.data.setValue(value, rowKey, columnKey);
200         fireDatasetChanged();
201     }
202
203     /**
204      * Adds or updates a value in the table.
205      *
206      * @param value the value.
207      * @param rowKey the row key.
208      * @param columnKey the column key.
209      */

210     public void setValue(double value, Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
211         this.setValue(new Double JavaDoc(value), rowKey, columnKey);
212     }
213
214     /**
215      * Adds the specified value to an existing value in the dataset (if the existing value is
216      * <code>null</code>, it is treated as if it were 0.0).
217      *
218      * @param value the value.
219      * @param rowKey the row key.
220      * @param columnKey the column key.
221      */

222     public void incrementValue(double value, Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
223         double existing = 0.0;
224         Number JavaDoc n = getValue(rowKey, columnKey);
225         if (n != null) {
226             existing = n.doubleValue();
227         }
228         setValue(existing + value, rowKey, columnKey);
229     }
230     
231     /**
232      * Removes a value from the dataset.
233      *
234      * @param rowKey the row key.
235      * @param columnKey the column key.
236      */

237     public void removeValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
238         this.data.removeValue(rowKey, columnKey);
239         fireDatasetChanged();
240     }
241
242     /**
243      * Removes a row from the dataset.
244      *
245      * @param rowIndex the row index.
246      */

247     public void removeRow(int rowIndex) {
248         this.data.removeRow(rowIndex);
249         fireDatasetChanged();
250     }
251
252     /**
253      * Removes a row from the dataset.
254      *
255      * @param rowKey the row key.
256      */

257     public void removeRow(Comparable JavaDoc rowKey) {
258         this.data.removeRow(rowKey);
259         fireDatasetChanged();
260     }
261
262     /**
263      * Removes a column from the dataset.
264      *
265      * @param columnIndex the column index.
266      */

267     public void removeColumn(int columnIndex) {
268         this.data.removeColumn(columnIndex);
269         fireDatasetChanged();
270     }
271
272     /**
273      * Removes a column from the dataset.
274      *
275      * @param columnKey the column key.
276      */

277     public void removeColumn(Comparable JavaDoc columnKey) {
278         this.data.removeColumn(columnKey);
279         fireDatasetChanged();
280     }
281
282     /**
283      * Tests if this object is equal to another.
284      *
285      * @param o the other object.
286      *
287      * @return A boolean.
288      */

289     public boolean equals(Object JavaDoc o) {
290
291         if (o == null) {
292             return false;
293         }
294         if (o == this) {
295             return true;
296         }
297
298         if (o instanceof CategoryDataset) {
299             CategoryDataset cd = (CategoryDataset) o;
300             boolean b1 = getRowKeys().equals(cd.getRowKeys());
301             boolean b2 = getColumnKeys().equals(cd.getColumnKeys());
302             if (b1 && b2) {
303                 for (int r = 0; r < getRowCount(); r++) {
304                     for (int c = 0; c < getColumnCount(); c++) {
305                         Number JavaDoc v1 = getValue(r, c);
306                         Number JavaDoc v2 = cd.getValue(r, c);
307                         if (v1 == null) {
308                             if (v2 != null) {
309                                 return false;
310                             }
311                         }
312                         else {
313                             if (!v1.equals(v2)) {
314                                 return false;
315                             }
316                         }
317
318                     }
319                 }
320                 return true;
321             }
322             else {
323                 return false;
324             }
325         }
326
327         return false;
328
329     }
330
331
332 }
333
Popular Tags