1 71 72 package org.jfree.data.xy; 73 74 import java.io.Serializable ; 75 import java.util.Collections ; 76 import java.util.List ; 77 78 import org.jfree.data.general.Series; 79 import org.jfree.data.general.SeriesChangeEvent; 80 import org.jfree.data.general.SeriesException; 81 import org.jfree.util.ObjectUtilities; 82 83 90 public class XYSeries extends Series implements Cloneable , Serializable { 91 92 93 static final long serialVersionUID = -5908509288197150436L; 94 95 99 100 protected List data; 101 102 103 private int maximumItemCount = Integer.MAX_VALUE; 104 105 106 private boolean autoSort; 107 108 109 private boolean allowDuplicateXValues; 110 111 118 public XYSeries(Comparable key) { 119 this(key, true, true); 120 } 121 122 130 public XYSeries(Comparable key, boolean autoSort) { 131 this(key, autoSort, true); 132 } 133 134 144 public XYSeries(Comparable key, 145 boolean autoSort, 146 boolean allowDuplicateXValues) { 147 super(key); 148 this.data = new java.util.ArrayList (); 149 this.autoSort = autoSort; 150 this.allowDuplicateXValues = allowDuplicateXValues; 151 } 152 153 160 public boolean getAutoSort() { 161 return this.autoSort; 162 } 163 164 170 public boolean getAllowDuplicateXValues() { 171 return this.allowDuplicateXValues; 172 } 173 174 179 public int getItemCount() { 180 return this.data.size(); 181 } 182 183 189 public List getItems() { 190 return Collections.unmodifiableList(this.data); 191 } 192 193 200 public int getMaximumItemCount() { 201 return this.maximumItemCount; 202 } 203 204 218 public void setMaximumItemCount(int maximum) { 219 this.maximumItemCount = maximum; 220 boolean dataRemoved = false; 221 while (this.data.size() > maximum) { 222 this.data.remove(0); 223 dataRemoved = true; 224 } 225 if (dataRemoved) { 226 fireSeriesChanged(); 227 } 228 } 229 230 236 public void add(XYDataItem item) { 237 add(item, true); 239 } 240 241 248 public void add(double x, double y) { 249 add(new Double (x), new Double (y), true); 250 } 251 252 262 public void add(double x, double y, boolean notify) { 263 add(new Double (x), new Double (y), notify); 264 } 265 266 274 public void add(double x, Number y) { 275 add(new Double (x), y); 276 } 277 278 289 public void add(double x, Number y, boolean notify) { 290 add(new Double (x), y, notify); 291 } 292 293 303 public void add(Number x, Number y) { 304 add(x, y, true); 306 } 307 308 321 public void add(Number x, Number y, boolean notify) { 322 XYDataItem item = new XYDataItem(x, y); 324 add(item, notify); 325 } 326 327 336 public void add(XYDataItem item, boolean notify) { 337 338 if (item == null) { 339 throw new IllegalArgumentException ("Null 'item' argument."); 340 } 341 342 if (this.autoSort) { 343 int index = Collections.binarySearch(this.data, item); 344 if (index < 0) { 345 this.data.add(-index - 1, item); 346 } 347 else { 348 if (this.allowDuplicateXValues) { 349 int size = this.data.size(); 351 while (index < size 352 && item.compareTo(this.data.get(index)) == 0) { 353 index++; 354 } 355 if (index < this.data.size()) { 356 this.data.add(index, item); 357 } 358 else { 359 this.data.add(item); 360 } 361 } 362 else { 363 throw new SeriesException("X-value already exists."); 364 } 365 } 366 } 367 else { 368 if (!this.allowDuplicateXValues) { 369 int index = indexOf(item.getX()); 372 if (index >= 0) { 373 throw new SeriesException("X-value already exists."); 374 } 375 } 376 this.data.add(item); 377 } 378 if (getItemCount() > this.maximumItemCount) { 379 this.data.remove(0); 380 } 381 if (notify) { 382 fireSeriesChanged(); 383 } 384 } 385 386 393 public void delete(int start, int end) { 394 for (int i = start; i <= end; i++) { 395 this.data.remove(start); 396 } 397 fireSeriesChanged(); 398 } 399 400 408 public XYDataItem remove(int index) { 409 XYDataItem result = (XYDataItem) this.data.remove(index); 410 fireSeriesChanged(); 411 return result; 412 } 413 414 422 public XYDataItem remove(Number x) { 423 return remove(indexOf(x)); 424 } 425 426 429 public void clear() { 430 if (this.data.size() > 0) { 431 this.data.clear(); 432 fireSeriesChanged(); 433 } 434 } 435 436 443 public XYDataItem getDataItem(int index) { 444 return (XYDataItem) this.data.get(index); 445 } 446 447 454 public Number getX(int index) { 455 return getDataItem(index).getX(); 456 } 457 458 465 public Number getY(int index) { 466 return getDataItem(index).getY(); 467 } 468 469 479 public void update(int index, Number y) { 480 XYDataItem item = getDataItem(index); 481 item.setY(y); 482 fireSeriesChanged(); 483 } 484 485 494 public void updateByIndex(int index, Number y) { 495 update(index, y); 496 } 497 498 507 public void update(Number x, Number y) { 508 int index = indexOf(x); 509 if (index < 0) { 510 throw new SeriesException("No observation for x = " + x); 511 } 512 else { 513 XYDataItem item = getDataItem(index); 514 item.setY(y); 515 fireSeriesChanged(); 516 } 517 } 518 519 530 public XYDataItem addOrUpdate(Number x, Number y) { 531 if (x == null) { 532 throw new IllegalArgumentException ("Null 'x' argument."); 533 } 534 XYDataItem overwritten = null; 535 int index = indexOf(x); 536 if (index >= 0) { 537 XYDataItem existing = (XYDataItem) this.data.get(index); 538 try { 539 overwritten = (XYDataItem) existing.clone(); 540 } 541 catch (CloneNotSupportedException e) { 542 throw new SeriesException("Couldn't clone XYDataItem!"); 543 } 544 existing.setY(y); 545 } 546 else { 547 if (this.autoSort) { 552 this.data.add(-index - 1, new XYDataItem(x, y)); 553 } 554 else { 555 this.data.add(new XYDataItem(x, y)); 556 } 557 if (getItemCount() > this.maximumItemCount) { 559 this.data.remove(0); 560 } 561 } 562 fireSeriesChanged(); 563 return overwritten; 564 } 565 566 576 public int indexOf(Number x) { 577 if (this.autoSort) { 578 return Collections.binarySearch(this.data, new XYDataItem(x, null)); 579 } 580 else { 581 for (int i = 0; i < this.data.size(); i++) { 582 XYDataItem item = (XYDataItem) this.data.get(i); 583 if (item.getX().equals(x)) { 584 return i; 585 } 586 } 587 return -1; 588 } 589 } 590 591 598 public Object clone() throws CloneNotSupportedException { 599 Object clone = createCopy(0, getItemCount() - 1); 600 return clone; 601 } 602 603 613 public XYSeries createCopy(int start, int end) 614 throws CloneNotSupportedException { 615 616 XYSeries copy = (XYSeries) super.clone(); 617 copy.data = new java.util.ArrayList (); 618 if (this.data.size() > 0) { 619 for (int index = start; index <= end; index++) { 620 XYDataItem item = (XYDataItem) this.data.get(index); 621 XYDataItem clone = (XYDataItem) item.clone(); 622 try { 623 copy.add(clone); 624 } 625 catch (SeriesException e) { 626 System.err.println("Unable to add cloned data item."); 627 } 628 } 629 } 630 return copy; 631 632 } 633 634 642 public boolean equals(Object obj) { 643 if (obj == this) { 644 return true; 645 } 646 if (!(obj instanceof XYSeries)) { 647 return false; 648 } 649 if (!super.equals(obj)) { 650 return false; 651 } 652 XYSeries that = (XYSeries) obj; 653 if (this.maximumItemCount != that.maximumItemCount) { 654 return false; 655 } 656 if (this.autoSort != that.autoSort) { 657 return false; 658 } 659 if (this.allowDuplicateXValues != that.allowDuplicateXValues) { 660 return false; 661 } 662 if (!ObjectUtilities.equal(this.data, that.data)) { 663 return false; 664 } 665 return true; 666 } 667 668 673 public int hashCode() { 674 int result = super.hashCode(); 675 result = 29 * result + (this.data != null ? this.data.hashCode() : 0); 676 result = 29 * result + this.maximumItemCount; 677 result = 29 * result + (this.autoSort ? 1 : 0); 678 result = 29 * result + (this.allowDuplicateXValues ? 1 : 0); 679 return result; 680 } 681 682 } 683 684 | Popular Tags |