1 43 44 package org.jfree.data; 45 46 import java.io.Serializable ; 47 import java.util.Collections ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 51 52 56 public class KeyedObjects2D implements Cloneable , Serializable { 57 58 59 private static final long serialVersionUID = -1015873563138522374L; 60 61 62 private List rowKeys; 63 64 65 private List columnKeys; 66 67 68 private List rows; 69 70 73 public KeyedObjects2D() { 74 this.rowKeys = new java.util.ArrayList (); 75 this.columnKeys = new java.util.ArrayList (); 76 this.rows = new java.util.ArrayList (); 77 } 78 79 84 public int getRowCount() { 85 return this.rowKeys.size(); 86 } 87 88 93 public int getColumnCount() { 94 return this.columnKeys.size(); 95 } 96 97 105 public Object getObject(int row, int column) { 106 107 Object result = null; 108 KeyedObjects rowData = (KeyedObjects) this.rows.get(row); 109 if (rowData != null) { 110 Comparable columnKey = (Comparable ) this.columnKeys.get(column); 111 if (columnKey != null) { 112 result = rowData.getObject(columnKey); 113 } 114 } 115 return result; 116 117 } 118 119 126 public Comparable getRowKey(int row) { 127 return (Comparable ) this.rowKeys.get(row); 128 } 129 130 137 public int getRowIndex(Comparable key) { 138 return this.rowKeys.indexOf(key); 139 } 140 141 146 public List getRowKeys() { 147 return Collections.unmodifiableList(this.rowKeys); 148 } 149 150 157 public Comparable getColumnKey(int column) { 158 return (Comparable ) this.columnKeys.get(column); 159 } 160 161 168 public int getColumnIndex(Comparable key) { 169 return this.columnKeys.indexOf(key); 170 } 171 172 177 public List getColumnKeys() { 178 return Collections.unmodifiableList(this.columnKeys); 179 } 180 181 189 public Object getObject(Comparable rowKey, Comparable columnKey) { 190 191 Object 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 208 public void addObject(Object object, 209 Comparable rowKey, 210 Comparable columnKey) { 211 setObject(object, rowKey, columnKey); 212 } 213 214 221 public void setObject(Object object, 222 Comparable rowKey, 223 Comparable 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 249 public void removeObject(Comparable rowKey, Comparable columnKey) { 250 setObject(null, rowKey, columnKey); 251 } 254 255 260 public void removeRow(int rowIndex) { 261 this.rowKeys.remove(rowIndex); 262 this.rows.remove(rowIndex); 263 } 264 265 270 public void removeRow(Comparable rowKey) { 271 removeRow(getRowIndex(rowKey)); 272 } 273 274 279 public void removeColumn(int columnIndex) { 280 Comparable columnKey = getColumnKey(columnIndex); 281 removeColumn(columnKey); 282 } 283 284 289 public void removeColumn(Comparable columnKey) { 290 Iterator 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 305 public boolean equals(Object 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 v1 = getObject(r, c); 339 Object 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 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 376 public Object clone() throws CloneNotSupportedException { 377 return super.clone(); 378 } 379 380 } 381 | Popular Tags |