KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeyedObjects.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: KeyedObjects.java,v 1.5 2005/05/19 10:34:07 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 31-Oct-2002 : Version 1 (DG);
39  * 11-Jan-2005 : Minor tidy up (DG);
40  *
41  */

42
43 package org.jfree.data;
44
45 import java.io.Serializable JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import org.jfree.util.PublicCloneable;
50
51 /**
52  * A collection of (key, object) pairs.
53  */

54 public class KeyedObjects implements Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
55
56     /** For serialization. */
57     private static final long serialVersionUID = 1321582394193530984L;
58     
59     /** Storage for the data. */
60     private List JavaDoc data;
61
62     /**
63      * Creates a new collection (initially empty).
64      */

65     public KeyedObjects() {
66         this.data = new java.util.ArrayList JavaDoc();
67     }
68
69     /**
70      * Returns the number of items (values) in the collection.
71      *
72      * @return The item count.
73      */

74     public int getItemCount() {
75         return this.data.size();
76     }
77
78     /**
79      * Returns an object.
80      *
81      * @param item the item index (zero-based).
82      *
83      * @return The object (<code>null</code> if the index is out of range).
84      */

85     public Object JavaDoc getObject(int item) {
86         Object JavaDoc result = null;
87         if (item >= 0 && item < this.data.size()) {
88             KeyedObject kobj = (KeyedObject) this.data.get(item);
89             if (kobj != null) {
90                 result = kobj.getObject();
91             }
92         }
93         return result;
94     }
95
96     /**
97      * Returns a key.
98      *
99      * @param index the item index (zero-based).
100      *
101      * @return The row key.
102      *
103      * @throws IndexOutOfBoundsException if <code>index</code> is out of bounds.
104      */

105     public Comparable JavaDoc getKey(int index) {
106         Comparable JavaDoc result = null;
107         if (index >= 0 && index < this.data.size()) {
108             KeyedObject item = (KeyedObject) this.data.get(index);
109             if (item != null) {
110                 result = item.getKey();
111             }
112         }
113         return result;
114     }
115
116     /**
117      * Returns the index for a given key.
118      *
119      * @param key the key.
120      *
121      * @return The index, or <code>-1</code> if the key is unrecognised.
122      */

123     public int getIndex(Comparable JavaDoc key) {
124         int result = -1;
125         int i = 0;
126         Iterator JavaDoc iterator = this.data.iterator();
127         while (iterator.hasNext()) {
128             KeyedObject ko = (KeyedObject) iterator.next();
129             if (ko.getKey().equals(key)) {
130                 result = i;
131             }
132             i++;
133         }
134         return result;
135     }
136
137     /**
138      * Returns the keys.
139      *
140      * @return The keys (never <code>null</code>).
141      */

142     public List JavaDoc getKeys() {
143         List JavaDoc result = new java.util.ArrayList JavaDoc();
144         Iterator JavaDoc iterator = this.data.iterator();
145         while (iterator.hasNext()) {
146             KeyedObject ko = (KeyedObject) iterator.next();
147             result.add(ko.getKey());
148         }
149         return result;
150     }
151
152     /**
153      * Returns the object for a given key. If the key is not recognised, the
154      * method should return <code>null</code>.
155      *
156      * @param key the key.
157      *
158      * @return The object (possibly <code>null</code>).
159      */

160     public Object JavaDoc getObject(Comparable JavaDoc key) {
161         return getObject(getIndex(key));
162     }
163
164     /**
165      * Adds a new object to the collection, or overwrites an existing object.
166      * This is the same as the {@link #setObject(Comparable, Object)} method.
167      *
168      * @param key the key.
169      * @param object the object.
170      */

171     public void addObject(Comparable JavaDoc key, Object JavaDoc object) {
172         setObject(key, object);
173     }
174
175     /**
176      * Replaces an existing object, or adds a new object to the collection.
177      * This is the same as the {@link #addObject(Comparable, Object)}
178      * method.
179      *
180      * @param key the key.
181      * @param object the object.
182      */

183     public void setObject(Comparable JavaDoc key, Object JavaDoc object) {
184         int keyIndex = getIndex(key);
185         if (keyIndex >= 0) {
186             KeyedObject ko = (KeyedObject) this.data.get(keyIndex);
187             ko.setObject(object);
188         }
189         else {
190             KeyedObject ko = new KeyedObject(key, object);
191             this.data.add(ko);
192         }
193     }
194
195     /**
196      * Removes a value from the collection.
197      *
198      * @param index the index of the item to remove.
199      */

200     public void removeValue(int index) {
201         this.data.remove(index);
202     }
203
204     /**
205      * Removes a value from the collection.
206      *
207      * @param key the key of the item to remove.
208      */

209     public void removeValue(Comparable JavaDoc key) {
210         removeValue(getIndex(key));
211     }
212     
213     /**
214      * Returns a clone of this object.
215      *
216      * @return A clone.
217      *
218      * @throws CloneNotSupportedException if there is a problem cloning.
219      */

220     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
221         KeyedObjects clone = (KeyedObjects) super.clone();
222         clone.data = new java.util.ArrayList JavaDoc();
223         Iterator JavaDoc iterator = this.data.iterator();
224         while (iterator.hasNext()) {
225             KeyedObject ko = (KeyedObject) iterator.next();
226             clone.data.add(ko.clone());
227         }
228         return clone;
229     }
230     
231     /**
232      * Tests if this object is equal to another.
233      *
234      * @param o the other object.
235      *
236      * @return A boolean.
237      */

238     public boolean equals(Object JavaDoc o) {
239
240         if (o == null) {
241             return false;
242         }
243         if (o == this) {
244             return true;
245         }
246
247         if (!(o instanceof KeyedObjects)) {
248             return false;
249         }
250
251         KeyedObjects kos = (KeyedObjects) o;
252         int count = getItemCount();
253         if (count != kos.getItemCount()) {
254             return false;
255         }
256
257         for (int i = 0; i < count; i++) {
258             Comparable JavaDoc k1 = getKey(i);
259             Comparable JavaDoc k2 = kos.getKey(i);
260             if (!k1.equals(k2)) {
261                 return false;
262             }
263             Object JavaDoc o1 = getObject(i);
264             Object JavaDoc o2 = kos.getObject(i);
265             if (o1 == null) {
266                 if (o2 != null) {
267                     return false;
268                 }
269             }
270             else {
271                 if (!o1.equals(o2)) {
272                     return false;
273                 }
274             }
275         }
276         return true;
277
278     }
279     
280 }
281
Popular Tags