KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------------
27  * KeyedObject2D.java
28  * ------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: KeyedObjects2D.java,v 1.6 2005/05/19 10:34:07 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 05-Feb-2003 : Version 1 (DG);
39  * 01-Mar-2004 : Added equals() and clone() methods and implemented
40  * Serializable (DG);
41  *
42  */

43
44 package org.jfree.data;
45
46 import java.io.Serializable JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50
51
52 /**
53  * A data structure that stores zero, one or many objects, where each object is
54  * associated with two keys (a 'row' key and a 'column' key).
55  */

56 public class KeyedObjects2D implements Cloneable JavaDoc, Serializable JavaDoc {
57
58     /** For serialization. */
59     private static final long serialVersionUID = -1015873563138522374L;
60     
61     /** The row keys. */
62     private List JavaDoc rowKeys;
63
64     /** The column keys. */
65     private List JavaDoc columnKeys;
66
67     /** The row data. */
68     private List JavaDoc rows;
69
70     /**
71      * Creates a new instance (initially empty).
72      */

73     public KeyedObjects2D() {
74         this.rowKeys = new java.util.ArrayList JavaDoc();
75         this.columnKeys = new java.util.ArrayList JavaDoc();
76         this.rows = new java.util.ArrayList JavaDoc();
77     }
78
79     /**
80      * Returns the row count.
81      *
82      * @return The row count.
83      */

84     public int getRowCount() {
85         return this.rowKeys.size();
86     }
87
88     /**
89      * Returns the column count.
90      *
91      * @return The column count.
92      */

93     public int getColumnCount() {
94         return this.columnKeys.size();
95     }
96
97     /**
98      * Returns the object for a given row and column.
99      *
100      * @param row the row index.
101      * @param column the column index.
102      *
103      * @return The object.
104      */

105     public Object JavaDoc getObject(int row, int column) {
106
107         Object JavaDoc result = null;
108         KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
109         if (rowData != null) {
110             Comparable JavaDoc columnKey = (Comparable JavaDoc) this.columnKeys.get(column);
111             if (columnKey != null) {
112                 result = rowData.getObject(columnKey);
113             }
114         }
115         return result;
116
117     }
118
119     /**
120      * Returns the key for a given row.
121      *
122      * @param row the row index (zero based).
123      *
124      * @return The row index.
125      */

126     public Comparable JavaDoc getRowKey(int row) {
127         return (Comparable JavaDoc) this.rowKeys.get(row);
128     }
129
130     /**
131      * Returns the row index for a given key.
132      *
133      * @param key the key.
134      *
135      * @return The row index.
136      */

137     public int getRowIndex(Comparable JavaDoc key) {
138         return this.rowKeys.indexOf(key);
139     }
140
141     /**
142      * Returns the row keys.
143      *
144      * @return The row keys (never <code>null</code>).
145      */

146     public List JavaDoc getRowKeys() {
147         return Collections.unmodifiableList(this.rowKeys);
148     }
149
150     /**
151      * Returns the key for a given column.
152      *
153      * @param column the column.
154      *
155      * @return The key.
156      */

157     public Comparable JavaDoc getColumnKey(int column) {
158         return (Comparable JavaDoc) this.columnKeys.get(column);
159     }
160
161     /**
162      * Returns the column index for a given key.
163      *
164      * @param key the key.
165      *
166      * @return The column index.
167      */

168     public int getColumnIndex(Comparable JavaDoc key) {
169         return this.columnKeys.indexOf(key);
170     }
171
172     /**
173      * Returns the column keys.
174      *
175      * @return The column keys (never <code>null</code>).
176      */

177     public List JavaDoc getColumnKeys() {
178         return Collections.unmodifiableList(this.columnKeys);
179     }
180
181     /**
182      * Returns the object for the given row and column keys.
183      *
184      * @param rowKey the row key.
185      * @param columnKey the column key.
186      *
187      * @return The object.
188      */

189     public Object JavaDoc getObject(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
190
191         Object JavaDoc result = null;
192         int row = this.rowKeys.indexOf(rowKey);
193         if (row >= 0) {
194             KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
195             result = rowData.getObject(columnKey);
196         }
197         return result;
198
199     }
200
201     /**
202      * Adds an object to the table. Performs the same function as setObject().
203      *
204      * @param object the object.
205      * @param rowKey the row key.
206      * @param columnKey the column key.
207      */

208     public void addObject(Object JavaDoc object,
209                           Comparable JavaDoc rowKey,
210                           Comparable JavaDoc columnKey) {
211         setObject(object, rowKey, columnKey);
212     }
213
214     /**
215      * Adds or updates an object.
216      *
217      * @param object the object.
218      * @param rowKey the row key.
219      * @param columnKey the column key.
220      */

221     public void setObject(Object JavaDoc object,
222                           Comparable JavaDoc rowKey,
223                           Comparable JavaDoc columnKey) {
224
225         KeyedObjects row;
226         int rowIndex = this.rowKeys.indexOf(rowKey);
227         if (rowIndex >= 0) {
228             row = (KeyedObjects) this.rows.get(rowIndex);
229         }
230         else {
231             this.rowKeys.add(rowKey);
232             row = new KeyedObjects();
233             this.rows.add(row);
234         }
235         row.setObject(columnKey, object);
236         int columnIndex = this.columnKeys.indexOf(columnKey);
237         if (columnIndex < 0) {
238             this.columnKeys.add(columnKey);
239         }
240
241     }
242
243     /**
244      * Removes an object.
245      *
246      * @param rowKey the row key.
247      * @param columnKey the column key.
248      */

249     public void removeObject(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
250         setObject(null, rowKey, columnKey);
251         // actually, a null value is different to a value that doesn't exist
252
// at all, need to fix this code.
253
}
254
255     /**
256      * Removes a row.
257      *
258      * @param rowIndex the row index.
259      */

260     public void removeRow(int rowIndex) {
261         this.rowKeys.remove(rowIndex);
262         this.rows.remove(rowIndex);
263     }
264
265     /**
266      * Removes a row.
267      *
268      * @param rowKey the row key.
269      */

270     public void removeRow(Comparable JavaDoc rowKey) {
271         removeRow(getRowIndex(rowKey));
272     }
273
274     /**
275      * Removes a column.
276      *
277      * @param columnIndex the column index.
278      */

279     public void removeColumn(int columnIndex) {
280         Comparable JavaDoc columnKey = getColumnKey(columnIndex);
281         removeColumn(columnKey);
282     }
283
284     /**
285      * Removes a column.
286      *
287      * @param columnKey the column key.
288      */

289     public void removeColumn(Comparable JavaDoc columnKey) {
290         Iterator JavaDoc iterator = this.rows.iterator();
291         while (iterator.hasNext()) {
292             KeyedObjects rowData = (KeyedObjects) iterator.next();
293             rowData.removeValue(columnKey);
294         }
295         this.columnKeys.remove(columnKey);
296     }
297
298     /**
299      * Tests this object for equality with an arbitrary object.
300      *
301      * @param obj the object to test (<code>null</code> permitted).
302      *
303      * @return A boolean.
304      */

305     public boolean equals(Object JavaDoc obj) {
306
307         if (obj == null) {
308             return false;
309         }
310         
311         if (obj == this) {
312             return true;
313         }
314
315         if (!(obj instanceof KeyedObjects2D)) {
316             return false;
317         }
318         
319         KeyedObjects2D ko2D = (KeyedObjects2D) obj;
320         if (!getRowKeys().equals(ko2D.getRowKeys())) {
321             return false;
322         }
323         if (!getColumnKeys().equals(ko2D.getColumnKeys())) {
324             return false;
325         }
326         int rowCount = getRowCount();
327         if (rowCount != ko2D.getRowCount()) {
328             return false;
329         }
330
331         int colCount = getColumnCount();
332         if (colCount != ko2D.getColumnCount()) {
333             return false;
334         }
335
336         for (int r = 0; r < rowCount; r++) {
337             for (int c = 0; c < colCount; c++) {
338                 Object JavaDoc v1 = getObject(r, c);
339                 Object JavaDoc v2 = ko2D.getObject(r, c);
340                 if (v1 == null) {
341                     if (v2 != null) {
342                         return false;
343                     }
344                 }
345                 else {
346                     if (!v1.equals(v2)) {
347                         return false;
348                     }
349                 }
350             }
351         }
352         return true;
353     }
354
355     /**
356      * Returns a hashcode for this object.
357      *
358      * @return A hashcode.
359      */

360     public int hashCode() {
361         int result;
362         result = this.rowKeys.hashCode();
363         result = 29 * result + this.columnKeys.hashCode();
364         result = 29 * result + this.rows.hashCode();
365         return result;
366     }
367
368     /**
369      * Returns a clone.
370      *
371      * @return A clone.
372      *
373      * @throws CloneNotSupportedException this class will not throw this
374      * exception, but subclasses (if any) might.
375      */

376     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
377         return super.clone();
378     }
379
380 }
381
Popular Tags