KickJava   Java API By Example, From Geeks To Geeks.

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


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 (row, column) tupels
18  *
19  * @author Klaus Meffert
20  * @since 2.3
21  */

22 public class KeyedValues2D
23     implements ICloneable, Serializable {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
26
27   /** The row keys */
28   private List m_rowKeys;
29
30   /** The column keys */
31   private List m_columnKeys;
32
33   /** The row data */
34   private List m_rows;
35
36   /** Should row keys be sorted by their comparable order? */
37   private boolean m_sortRowKeys;
38
39   /**
40    * Constructor setting behavior: non-sorted keys
41    *
42    * @author Klaus Meffert
43    * @since 2.3
44    */

45   public KeyedValues2D() {
46     this(false);
47   }
48
49   /**
50    * Constructor.
51    *
52    * @param a_sortRowKeys true: row keys should be sorted
53    *
54    * @author Klaus Meffert
55    * @since 2.3
56    */

57   public KeyedValues2D(final boolean a_sortRowKeys) {
58     m_rowKeys = Collections.synchronizedList(new ArrayList());
59     m_columnKeys = Collections.synchronizedList(new ArrayList());
60     m_rows = Collections.synchronizedList(new ArrayList());
61     m_sortRowKeys = a_sortRowKeys;
62   }
63
64   /**
65    * @return the number of rows
66    *
67    * @author Klaus Meffert
68    * @since 2.3
69    */

70   public int getRowCount() {
71     return m_rowKeys.size();
72   }
73
74   /**
75    * @return the number of columns
76    *
77    * @author Klaus Meffert
78    * @since 2.3
79    */

80   public int getColumnCount() {
81     return m_columnKeys.size();
82   }
83
84   /**
85    * Returns the value for a given row and column
86    *
87    * @param a_row the row index
88    * @param a_column the column index
89    *
90    * @return value at given row and column
91    *
92    * @author Klaus Meffert
93    * @since 2.3
94    */

95   public Number JavaDoc getValue(final int a_row, final int a_column) {
96     Number JavaDoc result = null;
97     final KeyedValues rowData = (KeyedValues) m_rows.get(a_row);
98     final Comparable JavaDoc columnKey = (Comparable JavaDoc) m_columnKeys.get(a_column);
99     if (columnKey != null) {
100       result = rowData.getValue(columnKey);
101     }
102     return result;
103   }
104
105   /**
106    * @param a_row the row index, starting at 0
107    *
108    * @return key for the given row
109    *
110    * @author Klaus Meffert
111    * @since 2.3
112    */

113   public Comparable JavaDoc getRowKey(final int a_row) {
114     return (Comparable JavaDoc) m_rowKeys.get(a_row);
115   }
116
117   /**
118    * @param a_key the row key
119    *
120    * @return row index for the given key
121    *
122    * @author Klaus Meffert
123    * @since 2.3
124    */

125   public int getRowIndex(final Comparable JavaDoc a_key) {
126     if (m_sortRowKeys) {
127       return Collections.binarySearch(m_rowKeys, a_key);
128     }
129     else {
130       return m_rowKeys.indexOf(a_key);
131     }
132   }
133
134   /**
135    * @return row keys
136    *
137    * @author Klaus Meffert
138    * @since 2.3
139    */

140   public List getRowKeys() {
141     return Collections.unmodifiableList(m_rowKeys);
142   }
143
144   /**
145    * @param a_column the column
146    *
147    * @return key for the given column
148    *
149    * @author Klaus Meffert
150    * @since 2.3
151    */

152   public Comparable JavaDoc getColumnKey(final int a_column) {
153     return (Comparable JavaDoc) m_columnKeys.get(a_column);
154   }
155
156   /**
157    * @return the column keys
158    *
159    * @author Klaus Meffert
160    * @since 2.3
161    */

162   public List getColumnKeys() {
163     return Collections.unmodifiableList(m_columnKeys);
164   }
165
166   /**
167    * @param a_rowKey the row key
168    * @param a_columnKey the column key
169    *
170    * @return value for the given row and column keys
171    *
172    * @author Klaus Meffert
173    * @since 2.3
174    */

175   public Number JavaDoc getValue(final Comparable JavaDoc a_rowKey,
176                          final Comparable JavaDoc a_columnKey) {
177     Number JavaDoc result;
178     final int row = getRowIndex(a_rowKey);
179     if (row >= 0) {
180       final KeyedValues rowData = (KeyedValues) m_rows.get(row);
181       result = rowData.getValue(a_columnKey);
182     }
183     else {
184       result = null;
185     }
186     return result;
187   }
188
189   /**
190    * Sets a value
191    *
192    * @param a_value the value
193    * @param a_rowKey the row key
194    * @param a_columnKey the column key
195    *
196    * @author Klaus Meffert
197    * @since 2.3
198    */

199   public void setValue(final Number JavaDoc a_value, final Comparable JavaDoc a_rowKey,
200                        final Comparable JavaDoc a_columnKey) {
201     final KeyedValues row;
202     int rowIndex = getRowIndex(a_rowKey);
203     if (rowIndex >= 0) {
204       row = (KeyedValues) m_rows.get(rowIndex);
205     }
206     else {
207       row = new KeyedValues();
208       if (m_sortRowKeys) {
209         rowIndex = -rowIndex - 1;
210         m_rowKeys.add(rowIndex, a_rowKey);
211         m_rows.add(rowIndex, row);
212       }
213       else {
214         m_rowKeys.add(a_rowKey);
215         m_rows.add(row);
216       }
217     }
218     row.setValue(a_columnKey, a_value);
219     final int columnIndex = m_columnKeys.indexOf(a_columnKey);
220     if (columnIndex < 0) {
221       m_columnKeys.add(a_columnKey);
222     }
223   }
224
225   /**
226    * Tests if this object is equal to another
227    *
228    * @param a_object other object.
229    *
230    * @return true: this object is equal to the other one
231    *
232    * @author Klaus Meffert
233    * @since 2.3
234    */

235   public boolean equals(final Object JavaDoc a_object) {
236     if (a_object == null) {
237       return false;
238     }
239     if (a_object == this) {
240       return true;
241     }
242     if (! (a_object instanceof KeyedValues2D)) {
243       return false;
244     }
245     final KeyedValues2D kv2D = (KeyedValues2D) a_object;
246     if (!getRowKeys().equals(kv2D.getRowKeys())) {
247       return false;
248     }
249     if (!getColumnKeys().equals(kv2D.getColumnKeys())) {
250       return false;
251     }
252     int rowCount = getRowCount();
253     int colCount = getColumnCount();
254     for (int r = 0; r < rowCount; r++) {
255       for (int c = 0; c < colCount; c++) {
256         final Number JavaDoc v1 = getValue(r, c);
257         final Number JavaDoc v2 = kv2D.getValue(r, c);
258         if (v1 == null) {
259           if (v2 != null) {
260             return false;
261           }
262         }
263         else {
264           if (!v1.equals(v2)) {
265             return false;
266           }
267         }
268       }
269     }
270     return true;
271   }
272
273   /**
274    * @return hash code of the instance
275    *
276    * @author Klaus Meffert
277    * @since 2.3
278    */

279   public int hashCode() {
280     int result;
281     result = m_rowKeys.hashCode();
282     result = 29 * result + m_columnKeys.hashCode();
283     result = 31 * result + m_rows.hashCode();
284     return result;
285   }
286
287   /**
288    * @return clone of the current instance
289    *
290    * @author Klaus Meffert
291    * @since 2.3
292    */

293   public Object JavaDoc clone() {
294     try {
295       return super.clone();
296     } catch (CloneNotSupportedException JavaDoc cex) {
297       throw new CloneException(cex);
298     }
299   }
300 }
301
Popular Tags