1:
44:
45: package ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51:
52:
53:
57: public class KeyedObjects2D implements Cloneable, Serializable {
58:
59:
60: private static final long serialVersionUID = -1015873563138522374L;
61:
62:
63: private List rowKeys;
64:
65:
66: private List columnKeys;
67:
68:
69: private List rows;
70:
71:
74: public KeyedObjects2D() {
75: this.rowKeys = new java.util.ArrayList();
76: this.columnKeys = new java.util.ArrayList();
77: this.rows = new java.util.ArrayList();
78: }
79:
80:
85: public int getRowCount() {
86: return this.rowKeys.size();
87: }
88:
89:
94: public int getColumnCount() {
95: return this.columnKeys.size();
96: }
97:
98:
106: public Object getObject(int row, int column) {
107:
108: Object result = null;
109: KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
110: if (rowData != null) {
111: Comparable columnKey = (Comparable) this.columnKeys.get(column);
112: if (columnKey != null) {
113: result = rowData.getObject(columnKey);
114: }
115: }
116: return result;
117:
118: }
119:
120:
127: public Comparable getRowKey(int row) {
128: return (Comparable) this.rowKeys.get(row);
129: }
130:
131:
138: public int getRowIndex(Comparable key) {
139: return this.rowKeys.indexOf(key);
140: }
141:
142:
147: public List getRowKeys() {
148: return Collections.unmodifiableList(this.rowKeys);
149: }
150:
151:
158: public Comparable getColumnKey(int column) {
159: return (Comparable) this.columnKeys.get(column);
160: }
161:
162:
169: public int getColumnIndex(Comparable key) {
170: return this.columnKeys.indexOf(key);
171: }
172:
173:
178: public List getColumnKeys() {
179: return Collections.unmodifiableList(this.columnKeys);
180: }
181:
182:
190: public Object getObject(Comparable rowKey, Comparable columnKey) {
191:
192: Object result = null;
193: int row = this.rowKeys.indexOf(rowKey);
194: if (row >= 0) {
195: KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
196: result = rowData.getObject(columnKey);
197: }
198: return result;
199:
200: }
201:
202:
209: public void addObject(Object object,
210: Comparable rowKey,
211: Comparable columnKey) {
212: setObject(object, rowKey, columnKey);
213: }
214:
215:
222: public void setObject(Object object,
223: Comparable rowKey,
224: Comparable columnKey) {
225:
226: KeyedObjects row;
227: int rowIndex = this.rowKeys.indexOf(rowKey);
228: if (rowIndex >= 0) {
229: row = (KeyedObjects) this.rows.get(rowIndex);
230: }
231: else {
232: this.rowKeys.add(rowKey);
233: row = new KeyedObjects();
234: this.rows.add(row);
235: }
236: row.setObject(columnKey, object);
237: int columnIndex = this.columnKeys.indexOf(columnKey);
238: if (columnIndex < 0) {
239: this.columnKeys.add(columnKey);
240: }
241:
242: }
243:
244:
250: public void removeObject(Comparable rowKey, Comparable columnKey) {
251: setObject(null, rowKey, columnKey);
252:
253:
254: }
255:
256:
261: public void removeRow(int rowIndex) {
262: this.rowKeys.remove(rowIndex);
263: this.rows.remove(rowIndex);
264: }
265:
266:
271: public void removeRow(Comparable rowKey) {
272: removeRow(getRowIndex(rowKey));
273: }
274:
275:
280: public void removeColumn(int columnIndex) {
281: Comparable columnKey = getColumnKey(columnIndex);
282: removeColumn(columnKey);
283: }
284:
285:
290: public void removeColumn(Comparable columnKey) {
291: Iterator iterator = this.rows.iterator();
292: while (iterator.hasNext()) {
293: KeyedObjects rowData = (KeyedObjects) iterator.next();
294: rowData.removeValue(columnKey);
295: }
296: this.columnKeys.remove(columnKey);
297: }
298:
299:
306: public boolean equals(Object obj) {
307:
308: if (obj == null) {
309: return false;
310: }
311:
312: if (obj == this) {
313: return true;
314: }
315:
316: if (!(obj instanceof KeyedObjects2D)) {
317: return false;
318: }
319:
320: KeyedObjects2D ko2D = (KeyedObjects2D) obj;
321: if (!getRowKeys().equals(ko2D.getRowKeys())) {
322: return false;
323: }
324: if (!getColumnKeys().equals(ko2D.getColumnKeys())) {
325: return false;
326: }
327: int rowCount = getRowCount();
328: if (rowCount != ko2D.getRowCount()) {
329: return false;
330: }
331:
332: int colCount = getColumnCount();
333: if (colCount != ko2D.getColumnCount()) {
334: return false;
335: }
336:
337: for (int r = 0; r < rowCount; r++) {
338: for (int c = 0; c < colCount; c++) {
339: Object v1 = getObject(r, c);
340: Object v2 = ko2D.getObject(r, c);
341: if (v1 == null) {
342: if (v2 != null) {
343: return false;
344: }
345: }
346: else {
347: if (!v1.equals(v2)) {
348: return false;
349: }
350: }
351: }
352: }
353: return true;
354: }
355:
356:
361: public int hashCode() {
362: int result;
363: result = this.rowKeys.hashCode();
364: result = 29 * result + this.columnKeys.hashCode();
365: result = 29 * result + this.rows.hashCode();
366: return result;
367: }
368:
369:
377: public Object clone() throws CloneNotSupportedException {
378: return super.clone();
379: }
380:
381: }