1 50 51 package org.jfree.data; 52 53 import java.io.Serializable ; 54 import java.util.Collections ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 58 import org.jfree.util.ObjectUtilities; 59 import org.jfree.util.PublicCloneable; 60 61 66 public class DefaultKeyedValues2D implements KeyedValues2D, 67 PublicCloneable, Cloneable , 68 Serializable { 69 70 71 private static final long serialVersionUID = -5514169970951994748L; 72 73 74 private List rowKeys; 75 76 77 private List columnKeys; 78 79 80 private List rows; 81 82 83 private boolean sortRowKeys; 84 85 88 public DefaultKeyedValues2D() { 89 this(false); 90 } 91 92 97 public DefaultKeyedValues2D(boolean sortRowKeys) { 98 this.rowKeys = new java.util.ArrayList (); 99 this.columnKeys = new java.util.ArrayList (); 100 this.rows = new java.util.ArrayList (); 101 this.sortRowKeys = sortRowKeys; 102 } 103 104 109 public int getRowCount() { 110 return this.rowKeys.size(); 111 } 112 113 118 public int getColumnCount() { 119 return this.columnKeys.size(); 120 } 121 122 130 public Number getValue(int row, int column) { 131 Number result = null; 132 DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row); 133 if (rowData != null) { 134 Comparable columnKey = (Comparable ) this.columnKeys.get(column); 135 int index = rowData.getIndex(columnKey); 138 if (index >= 0) { 139 result = rowData.getValue(index); 140 } 141 } 142 return result; 143 } 144 145 152 public Comparable getRowKey(int row) { 153 return (Comparable ) this.rowKeys.get(row); 154 } 155 156 163 public int getRowIndex(Comparable key) { 164 if (key == null) { 165 throw new IllegalArgumentException ("Null 'key' argument."); 166 } 167 if (this.sortRowKeys) { 168 return Collections.binarySearch(this.rowKeys, key); 169 } 170 else { 171 return this.rowKeys.indexOf(key); 172 } 173 } 174 175 180 public List getRowKeys() { 181 return Collections.unmodifiableList(this.rowKeys); 182 } 183 184 191 public Comparable getColumnKey(int column) { 192 return (Comparable ) this.columnKeys.get(column); 193 } 194 195 202 public int getColumnIndex(Comparable key) { 203 if (key == null) { 204 throw new IllegalArgumentException ("Null 'key' argument."); 205 } 206 return this.columnKeys.indexOf(key); 207 } 208 209 214 public List getColumnKeys() { 215 return Collections.unmodifiableList(this.columnKeys); 216 } 217 218 226 public Number getValue(Comparable rowKey, Comparable columnKey) { 227 if (rowKey == null) { 228 throw new IllegalArgumentException ("Null 'rowKey' argument."); 229 } 230 if (columnKey == null) { 231 throw new IllegalArgumentException ("Null 'columnKey' argument."); 232 } 233 Number result = null; 234 int row = getRowIndex(rowKey); 235 236 if (row >= 0) { 237 DefaultKeyedValues rowData 238 = (DefaultKeyedValues) this.rows.get(row); 239 result = rowData.getValue(columnKey); 240 } 241 return result; 242 } 243 244 252 public void addValue(Number value, Comparable rowKey, 253 Comparable columnKey) { 254 setValue(value, rowKey, columnKey); 256 } 257 258 265 public void setValue(Number value, Comparable rowKey, 266 Comparable columnKey) { 267 268 DefaultKeyedValues row; 269 int rowIndex = getRowIndex(rowKey); 270 271 if (rowIndex >= 0) { 272 row = (DefaultKeyedValues) this.rows.get(rowIndex); 273 } 274 else { 275 row = new DefaultKeyedValues(); 276 if (this.sortRowKeys) { 277 rowIndex = -rowIndex - 1; 278 this.rowKeys.add(rowIndex, rowKey); 279 this.rows.add(rowIndex, row); 280 } 281 else { 282 this.rowKeys.add(rowKey); 283 this.rows.add(row); 284 } 285 } 286 row.setValue(columnKey, value); 287 288 int columnIndex = this.columnKeys.indexOf(columnKey); 289 if (columnIndex < 0) { 290 this.columnKeys.add(columnKey); 291 } 292 } 293 294 300 public void removeValue(Comparable rowKey, Comparable columnKey) { 301 setValue(null, rowKey, columnKey); 302 303 boolean allNull = true; 305 int rowIndex = getRowIndex(rowKey); 306 DefaultKeyedValues row = (DefaultKeyedValues) this.rows.get(rowIndex); 307 308 for (int item = 0, itemCount = row.getItemCount(); item < itemCount; 309 item++) { 310 if (row.getValue(item) != null) { 311 allNull = false; 312 break; 313 } 314 } 315 316 if (allNull) { 317 this.rowKeys.remove(rowIndex); 318 this.rows.remove(rowIndex); 319 } 320 321 allNull = true; 323 int columnIndex = getColumnIndex(columnKey); 324 325 for (int item = 0, itemCount = this.rows.size(); item < itemCount; 326 item++) { 327 row = (DefaultKeyedValues) this.rows.get(item); 328 if (row.getValue(columnIndex) != null) { 329 allNull = false; 330 break; 331 } 332 } 333 334 if (allNull) { 335 for (int item = 0, itemCount = this.rows.size(); item < itemCount; 336 item++) { 337 row = (DefaultKeyedValues) this.rows.get(item); 338 row.removeValue(columnIndex); 339 } 340 this.columnKeys.remove(columnIndex); 341 } 342 } 343 344 349 public void removeRow(int rowIndex) { 350 this.rowKeys.remove(rowIndex); 351 this.rows.remove(rowIndex); 352 } 353 354 359 public void removeRow(Comparable rowKey) { 360 removeRow(getRowIndex(rowKey)); 361 } 362 363 368 public void removeColumn(int columnIndex) { 369 Comparable columnKey = getColumnKey(columnIndex); 370 removeColumn(columnKey); 371 } 372 373 378 public void removeColumn(Comparable columnKey) { 379 Iterator iterator = this.rows.iterator(); 380 while (iterator.hasNext()) { 381 DefaultKeyedValues rowData = (DefaultKeyedValues) iterator.next(); 382 rowData.removeValue(columnKey); 383 } 384 this.columnKeys.remove(columnKey); 385 } 386 387 390 public void clear() { 391 this.rowKeys.clear(); 392 this.columnKeys.clear(); 393 this.rows.clear(); 394 } 395 396 403 public boolean equals(Object o) { 404 405 if (o == null) { 406 return false; 407 } 408 if (o == this) { 409 return true; 410 } 411 412 if (!(o instanceof KeyedValues2D)) { 413 return false; 414 } 415 KeyedValues2D kv2D = (KeyedValues2D) o; 416 if (!getRowKeys().equals(kv2D.getRowKeys())) { 417 return false; 418 } 419 if (!getColumnKeys().equals(kv2D.getColumnKeys())) { 420 return false; 421 } 422 int rowCount = getRowCount(); 423 if (rowCount != kv2D.getRowCount()) { 424 return false; 425 } 426 427 int colCount = getColumnCount(); 428 if (colCount != kv2D.getColumnCount()) { 429 return false; 430 } 431 432 for (int r = 0; r < rowCount; r++) { 433 for (int c = 0; c < colCount; c++) { 434 Number v1 = getValue(r, c); 435 Number v2 = kv2D.getValue(r, c); 436 if (v1 == null) { 437 if (v2 != null) { 438 return false; 439 } 440 } 441 else { 442 if (!v1.equals(v2)) { 443 return false; 444 } 445 } 446 } 447 } 448 return true; 449 } 450 451 456 public int hashCode() { 457 int result; 458 result = this.rowKeys.hashCode(); 459 result = 29 * result + this.columnKeys.hashCode(); 460 result = 29 * result + this.rows.hashCode(); 461 return result; 462 } 463 464 472 public Object clone() throws CloneNotSupportedException { 473 DefaultKeyedValues2D clone = (DefaultKeyedValues2D) super.clone(); 474 clone.columnKeys = new java.util.ArrayList (this.columnKeys); 477 clone.rowKeys = new java.util.ArrayList (this.rowKeys); 478 479 clone.rows = (List ) ObjectUtilities.deepClone(this.rows); 481 return clone; 482 } 483 484 } 485 | Popular Tags |