KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > KeyedValues


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.audit;
11
12 import java.io.*;
13 import java.util.*;
14 import org.jgap.util.*;
15
16 /**
17  * A collection of (key, value) tupels
18  *
19  * @author Klaus Meffert
20  * @since 2.3
21  */

22 public class KeyedValues
23     implements ICloneable, Serializable {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.6 $";
26
27   /** Data storage */
28   private List m_data;
29
30   /**
31    * Creates a new collection (initially empty).
32    *
33    * @author Klaus Meffert
34    * @since 2.3
35    */

36   public KeyedValues() {
37     m_data = Collections.synchronizedList(new ArrayList());
38   }
39
40   /**
41    * @return number of items in the collection
42    *
43    * @author Klaus Meffert
44    * @since 2.3
45    */

46   public int size() {
47     return m_data.size();
48   }
49
50   /**
51    * @param a_index the index of the item to return the value for
52    * @return the value at given index
53    *
54    * @author Klaus Meffert
55    * @since 2.3
56    */

57   public Number JavaDoc getValue(final int a_index) {
58     Number JavaDoc result;
59     final KeyedValue kval = (KeyedValue) m_data.get(a_index);
60     if (kval != null) {
61       result = kval.getValue();
62     }
63     else {
64       result = null;
65     }
66     return result;
67   }
68
69   /**
70    * @param a_index the item index to retrieve the key for, starting at 0
71    *
72    * @return the row key for item at given index
73    *
74    * @author Klaus Meffert
75    * @since 2.3
76    */

77   public Comparable JavaDoc getKey(final int a_index) {
78     Comparable JavaDoc result;
79     final KeyedValue item = (KeyedValue) m_data.get(a_index);
80     if (item != null) {
81       result = item.getKey();
82     }
83     else {
84       result = null;
85     }
86     return result;
87   }
88
89   /**
90    * @param a_key the key to search for
91    *
92    * @return index for a given key or -1 if the key is not found
93    *
94    * @author Klaus Meffert
95    * @since 2.3
96    */

97   public int getIndex(final Comparable JavaDoc a_key) {
98     int i = 0;
99     final Iterator iterator = m_data.iterator();
100     while (iterator.hasNext()) {
101       final KeyedValue kv = (KeyedValue) iterator.next();
102       if (kv.getKey() != null) {
103         if (kv.getKey().equals(a_key)) {
104           return i;
105         }
106       }
107       else {
108         if (a_key == null) {
109           return i;
110         }
111       }
112       i++;
113     }
114     // key not found
115
return -1;
116   }
117
118   /**
119    * Returns the keys for the values in the collection
120    *
121    * @return The keys (never null)
122    *
123    * @author Klaus Meffert
124    * @since 2.3
125    */

126   public List getKeys() {
127     final List result = new java.util.ArrayList JavaDoc();
128     final Iterator iterator = m_data.iterator();
129     while (iterator.hasNext()) {
130       final KeyedValue kv = (KeyedValue) iterator.next();
131       result.add(kv.getKey());
132     }
133     return result;
134   }
135
136   /**
137    * Returns the value for a given key. For unknown keys, null is returned
138    *
139    * @param a_key the key
140    *
141    * @return the value for the key
142    *
143    * @author Klaus Meffert
144    * @since 2.3
145    */

146   public Number JavaDoc getValue(final Comparable JavaDoc a_key) {
147     Number JavaDoc result;
148     final int index = getIndex(a_key);
149     if (index >= 0) {
150       result = getValue(index);
151     }
152     else {
153       result = null;
154     }
155     return result;
156   }
157
158   /**
159    * Updates an existing value, or adds a new value to the collection
160    *
161    * @param a_key the key
162    * @param a_value the value
163    *
164    * @author Klaus Meffert
165    * @since 2.3
166    */

167   public void setValue(final Comparable JavaDoc a_key, final Number JavaDoc a_value) {
168     final int keyIndex = getIndex(a_key);
169     if (keyIndex >= 0) {
170       final KeyedValue kv = (KeyedValue) m_data.get(keyIndex);
171       kv.setValue(a_value);
172     }
173     else {
174       final KeyedValue kv = new KeyedValue(a_key, a_value);
175       m_data.add(kv);
176     }
177   }
178
179   /**
180    * Tests if this object is equal to another
181    *
182    * @param a_obj the other object
183    *
184    * @return true: this object is equal to the other one
185    *
186    * @author Klaus Meffert
187    * @since 2.3
188    */

189   public boolean equals(final Object JavaDoc a_obj) {
190     if (a_obj == null) {
191       return false;
192     }
193     if (a_obj == this) {
194       return true;
195     }
196     if (! (a_obj instanceof KeyedValues)) {
197       return false;
198     }
199     final KeyedValues kvs = (KeyedValues) a_obj;
200     final int count = size();
201     if (count != kvs.size()) {
202       return false;
203     }
204     for (int i = 0; i < count; i++) {
205       final Comparable JavaDoc k1 = getKey(i);
206       final Comparable JavaDoc k2 = kvs.getKey(i);
207       if (!k1.equals(k2)) {
208         return false;
209       }
210       final Number JavaDoc v1 = getValue(i);
211       final Number JavaDoc v2 = kvs.getValue(i);
212       if (v1 == null) {
213         if (v2 != null) {
214           return false;
215         }
216       }
217       else {
218         if (!v1.equals(v2)) {
219           return false;
220         }
221       }
222     }
223     return true;
224   }
225
226   /**
227    * @return hash code of the instance
228    *
229    * @author Klaus Meffert
230    * @since 2.3
231    */

232   public int hashCode() {
233     if (m_data.size() == 0) {
234       return -29;
235     }
236     else {
237       return m_data.hashCode();
238     }
239   }
240
241   /**
242    * @return clone of the current instance
243    *
244    * @author Klaus Meffert
245    * @since 2.3
246    */

247   public Object JavaDoc clone() {
248     try {
249       final KeyedValues clone = (KeyedValues)super.clone();
250       clone.m_data = Collections.synchronizedList(new ArrayList());
251       final Iterator iterator = m_data.iterator();
252       while (iterator.hasNext()) {
253         final KeyedValue kv = (KeyedValue) iterator.next();
254         clone.m_data.add(kv.clone());
255       }
256       return clone;
257     } catch (CloneNotSupportedException JavaDoc cex) {
258       throw new CloneException(cex);
259     }
260   }
261 }
262
Popular Tags