1 61 62 package org.jfree.data.xy; 63 64 import java.util.ArrayList ; 65 import java.util.HashSet ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 69 import org.jfree.data.DomainInfo; 70 import org.jfree.data.Range; 71 import org.jfree.data.general.DatasetChangeEvent; 72 import org.jfree.data.general.DatasetUtilities; 73 import org.jfree.data.general.SeriesChangeEvent; 74 import org.jfree.util.ObjectUtilities; 75 76 82 public class DefaultTableXYDataset extends AbstractIntervalXYDataset 83 implements TableXYDataset, 84 IntervalXYDataset, DomainInfo { 85 86 90 private List data = null; 91 92 93 private HashSet xPoints = null; 94 95 96 private boolean propagateEvents = true; 97 98 99 private boolean autoPrune = false; 100 101 102 private IntervalXYDelegate intervalDelegate; 103 104 107 public DefaultTableXYDataset() { 108 this(false); 109 } 110 111 118 public DefaultTableXYDataset(boolean autoPrune) { 119 this.autoPrune = autoPrune; 120 this.data = new ArrayList (); 121 this.xPoints = new HashSet (); 122 this.intervalDelegate = new IntervalXYDelegate(this, false); 123 addChangeListener(this.intervalDelegate); 124 } 125 126 132 public boolean isAutoPrune() { 133 return this.autoPrune; 134 } 135 136 143 public void addSeries(XYSeries series) { 144 if (series == null) { 145 throw new IllegalArgumentException ("Null 'series' argument."); 146 } 147 if (series.getAllowDuplicateXValues()) { 148 throw new IllegalArgumentException ( 149 "Cannot accept XYSeries that allow duplicate values. " 150 + "Use XYSeries(seriesName, <sort>, false) constructor." 151 ); 152 } 153 updateXPoints(series); 154 this.data.add(series); 155 series.addChangeListener(this); 156 fireDatasetChanged(); 157 } 158 159 165 private void updateXPoints(XYSeries series) { 166 if (series == null) { 167 throw new IllegalArgumentException ("Null 'series' not permitted."); 168 } 169 HashSet seriesXPoints = new HashSet (); 170 boolean savedState = this.propagateEvents; 171 this.propagateEvents = false; 172 for (int itemNo = 0; itemNo < series.getItemCount(); itemNo++) { 173 Number xValue = series.getX(itemNo); 174 seriesXPoints.add(xValue); 175 if (!this.xPoints.contains(xValue)) { 176 this.xPoints.add(xValue); 177 int seriesCount = this.data.size(); 178 for (int seriesNo = 0; seriesNo < seriesCount; seriesNo++) { 179 XYSeries dataSeries = (XYSeries) this.data.get(seriesNo); 180 if (!dataSeries.equals(series)) { 181 dataSeries.add(xValue, null); 182 } 183 } 184 } 185 } 186 Iterator iterator = this.xPoints.iterator(); 187 while (iterator.hasNext()) { 188 Number xPoint = (Number ) iterator.next(); 189 if (!seriesXPoints.contains(xPoint)) { 190 series.add(xPoint, null); 191 } 192 } 193 this.propagateEvents = savedState; 194 } 195 196 199 public void updateXPoints() { 200 this.propagateEvents = false; 201 for (int s = 0; s < this.data.size(); s++) { 202 updateXPoints((XYSeries) this.data.get(s)); 203 } 204 if (this.autoPrune) { 205 prune(); 206 } 207 this.propagateEvents = true; 208 } 209 210 215 public int getSeriesCount() { 216 return this.data.size(); 217 } 218 219 224 public int getItemCount() { 225 if (this.xPoints == null) { 226 return 0; 227 } 228 else { 229 return this.xPoints.size(); 230 } 231 } 232 233 240 public XYSeries getSeries(int series) { 241 if ((series < 0) || (series > getSeriesCount())) { 242 throw new IllegalArgumentException ("Index outside valid range."); 243 } 244 245 return (XYSeries) this.data.get(series); 246 } 247 248 255 public Comparable getSeriesKey(int series) { 256 return getSeries(series).getKey(); 258 } 259 260 267 public int getItemCount(int series) { 268 return getSeries(series).getItemCount(); 270 } 271 272 280 public Number getX(int series, int item) { 281 XYSeries s = (XYSeries) this.data.get(series); 282 XYDataItem dataItem = s.getDataItem(item); 283 return dataItem.getX(); 284 } 285 286 294 public Number getStartX(int series, int item) { 295 return this.intervalDelegate.getStartX(series, item); 296 } 297 298 306 public Number getEndX(int series, int item) { 307 return this.intervalDelegate.getEndX(series, item); 308 } 309 310 319 public Number getY(int series, int index) { 320 XYSeries ts = (XYSeries) this.data.get(series); 321 XYDataItem dataItem = ts.getDataItem(index); 322 return dataItem.getY(); 323 } 324 325 333 public Number getStartY(int series, int item) { 334 return getY(series, item); 335 } 336 337 345 public Number getEndY(int series, int item) { 346 return getY(series, item); 347 } 348 349 353 public void removeAllSeries() { 354 355 for (int i = 0; i < this.data.size(); i++) { 358 XYSeries series = (XYSeries) this.data.get(i); 359 series.removeChangeListener(this); 360 } 361 362 this.data.clear(); 364 this.xPoints.clear(); 365 fireDatasetChanged(); 366 } 367 368 374 public void removeSeries(XYSeries series) { 375 376 if (series == null) { 378 throw new IllegalArgumentException ("Null 'series' argument."); 379 } 380 381 if (this.data.contains(series)) { 383 series.removeChangeListener(this); 384 this.data.remove(series); 385 if (this.data.size() == 0) { 386 this.xPoints.clear(); 387 } 388 fireDatasetChanged(); 389 } 390 391 } 392 393 399 public void removeSeries(int series) { 400 401 if ((series < 0) || (series > getSeriesCount())) { 403 throw new IllegalArgumentException ("Index outside valid range."); 404 } 405 406 XYSeries s = (XYSeries) this.data.get(series); 408 s.removeChangeListener(this); 409 this.data.remove(series); 410 if (this.data.size() == 0) { 411 this.xPoints.clear(); 412 } 413 else if (this.autoPrune) { 414 prune(); 415 } 416 fireDatasetChanged(); 417 418 } 419 420 425 public void removeAllValuesForX(Number x) { 426 if (x == null) { 427 throw new IllegalArgumentException ("Null 'x' argument."); 428 } 429 boolean savedState = this.propagateEvents; 430 this.propagateEvents = false; 431 for (int s = 0; s < this.data.size(); s++) { 432 XYSeries series = (XYSeries) this.data.get(s); 433 series.remove(x); 434 } 435 this.propagateEvents = savedState; 436 this.xPoints.remove(x); 437 fireDatasetChanged(); 438 } 439 440 448 protected boolean canPrune(Number x) { 449 for (int s = 0; s < this.data.size(); s++) { 450 XYSeries series = (XYSeries) this.data.get(s); 451 if (series.getY(series.indexOf(x)) != null) { 452 return false; 453 } 454 } 455 return true; 456 } 457 458 461 public void prune() { 462 HashSet hs = (HashSet ) this.xPoints.clone(); 463 Iterator iterator = hs.iterator(); 464 while (iterator.hasNext()) { 465 Number x = (Number ) iterator.next(); 466 if (canPrune(x)) { 467 removeAllValuesForX(x); 468 } 469 } 470 } 471 472 479 public void seriesChanged(SeriesChangeEvent event) { 480 if (this.propagateEvents) { 481 updateXPoints(); 482 fireDatasetChanged(); 483 } 484 } 485 486 493 public boolean equals(Object obj) { 494 if (obj == this) { 495 return true; 496 } 497 if (!(obj instanceof DefaultTableXYDataset)) { 498 return false; 499 } 500 DefaultTableXYDataset that = (DefaultTableXYDataset) obj; 501 if (this.autoPrune != that.autoPrune) { 502 return false; 503 } 504 if (this.propagateEvents != that.propagateEvents) { 505 return false; 506 } 507 if (!this.intervalDelegate.equals(that.intervalDelegate)) { 508 return false; 509 } 510 if (!ObjectUtilities.equal(this.data, that.data)) { 511 return false; 512 } 513 return true; 514 } 515 516 521 public int hashCode() { 522 int result; 523 result = (this.data != null ? this.data.hashCode() : 0); 524 result = 29 * result 525 + (this.xPoints != null ? this.xPoints.hashCode() : 0); 526 result = 29 * result + (this.propagateEvents ? 1 : 0); 527 result = 29 * result + (this.autoPrune ? 1 : 0); 528 return result; 529 } 530 531 539 public double getDomainLowerBound(boolean includeInterval) { 540 return this.intervalDelegate.getDomainLowerBound(includeInterval); 541 } 542 543 551 public double getDomainUpperBound(boolean includeInterval) { 552 return this.intervalDelegate.getDomainUpperBound(includeInterval); 553 } 554 555 563 public Range getDomainBounds(boolean includeInterval) { 564 if (includeInterval) { 565 return this.intervalDelegate.getDomainBounds(includeInterval); 566 } 567 else { 568 return DatasetUtilities.iterateDomainBounds(this, includeInterval); 569 } 570 } 571 572 577 public double getIntervalPositionFactor() { 578 return this.intervalDelegate.getIntervalPositionFactor(); 579 } 580 581 589 public void setIntervalPositionFactor(double d) { 590 this.intervalDelegate.setIntervalPositionFactor(d); 591 fireDatasetChanged(); 592 } 593 594 599 public double getIntervalWidth() { 600 return this.intervalDelegate.getIntervalWidth(); 601 } 602 603 609 public void setIntervalWidth(double d) { 610 this.intervalDelegate.setFixedIntervalWidth(d); 611 fireDatasetChanged(); 612 } 613 614 620 public boolean isAutoWidth() { 621 return this.intervalDelegate.isAutoWidth(); 622 } 623 624 630 public void setAutoWidth(boolean b) { 631 this.intervalDelegate.setAutoWidth(b); 632 fireDatasetChanged(); 633 } 634 635 } 636 | Popular Tags |