1 76 77 package org.jfree.data.time; 78 79 import java.io.Serializable ; 80 import java.util.ArrayList ; 81 import java.util.Calendar ; 82 import java.util.Collections ; 83 import java.util.Iterator ; 84 import java.util.List ; 85 import java.util.TimeZone ; 86 87 import org.jfree.data.DomainInfo; 88 import org.jfree.data.Range; 89 import org.jfree.data.general.DatasetChangeEvent; 90 import org.jfree.data.xy.AbstractIntervalXYDataset; 91 import org.jfree.data.xy.IntervalXYDataset; 92 import org.jfree.data.xy.XYDataset; 93 import org.jfree.util.ObjectUtilities; 94 95 101 public class TimeSeriesCollection extends AbstractIntervalXYDataset 102 implements XYDataset, 103 IntervalXYDataset, 104 DomainInfo, 105 Serializable { 106 107 108 private static final long serialVersionUID = 834149929022371137L; 109 110 111 private List data; 112 113 114 private Calendar workingCalendar; 115 116 121 private TimePeriodAnchor xPosition; 122 123 128 private boolean domainIsPointsInTime; 129 130 133 public TimeSeriesCollection() { 134 this(null, TimeZone.getDefault()); 135 } 136 137 143 public TimeSeriesCollection(TimeZone zone) { 144 this(null, zone); 145 } 146 147 153 public TimeSeriesCollection(TimeSeries series) { 154 this(series, TimeZone.getDefault()); 155 } 156 157 166 public TimeSeriesCollection(TimeSeries series, TimeZone zone) { 167 168 if (zone == null) { 169 zone = TimeZone.getDefault(); 170 } 171 this.workingCalendar = Calendar.getInstance(zone); 172 this.data = new ArrayList (); 173 if (series != null) { 174 this.data.add(series); 175 series.addChangeListener(this); 176 } 177 this.xPosition = TimePeriodAnchor.START; 178 this.domainIsPointsInTime = true; 179 180 } 181 182 191 public boolean getDomainIsPointsInTime() { 192 return this.domainIsPointsInTime; 193 } 194 195 201 public void setDomainIsPointsInTime(boolean flag) { 202 this.domainIsPointsInTime = flag; 203 notifyListeners(new DatasetChangeEvent(this, this)); 204 } 205 206 213 public TimePeriodAnchor getXPosition() { 214 return this.xPosition; 215 } 216 217 224 public void setXPosition(TimePeriodAnchor anchor) { 225 if (anchor == null) { 226 throw new IllegalArgumentException ("Null 'anchor' argument."); 227 } 228 this.xPosition = anchor; 229 notifyListeners(new DatasetChangeEvent(this, this)); 230 } 231 232 237 public List getSeries() { 238 return Collections.unmodifiableList(this.data); 239 } 240 241 246 public int getSeriesCount() { 247 return this.data.size(); 248 } 249 250 257 public TimeSeries getSeries(int series) { 258 if ((series < 0) || (series >= getSeriesCount())) { 259 throw new IllegalArgumentException ( 260 "The 'series' argument is out of bounds (" + series + ")." 261 ); 262 } 263 return (TimeSeries) this.data.get(series); 264 } 265 266 274 public TimeSeries getSeries(String key) { 275 TimeSeries result = null; 276 Iterator iterator = this.data.iterator(); 277 while (iterator.hasNext()) { 278 TimeSeries series = (TimeSeries) iterator.next(); 279 Comparable k = series.getKey(); 280 if (k != null && k.equals(key)) { 281 result = series; 282 } 283 } 284 return result; 285 } 286 287 294 public Comparable getSeriesKey(int series) { 295 return getSeries(series).getKey(); 298 } 299 300 306 public void addSeries(TimeSeries series) { 307 if (series == null) { 308 throw new IllegalArgumentException ("Null 'series' argument."); 309 } 310 this.data.add(series); 311 series.addChangeListener(this); 312 fireDatasetChanged(); 313 } 314 315 321 public void removeSeries(TimeSeries series) { 322 if (series == null) { 323 throw new IllegalArgumentException ("Null 'series' argument."); 324 } 325 this.data.remove(series); 326 series.removeChangeListener(this); 327 fireDatasetChanged(); 328 } 329 330 335 public void removeSeries(int index) { 336 TimeSeries series = getSeries(index); 337 if (series != null) { 338 removeSeries(series); 339 } 340 } 341 342 346 public void removeAllSeries() { 347 348 for (int i = 0; i < this.data.size(); i++) { 351 TimeSeries series = (TimeSeries) this.data.get(i); 352 series.removeChangeListener(this); 353 } 354 355 this.data.clear(); 357 fireDatasetChanged(); 358 359 } 360 361 369 public int getItemCount(int series) { 370 return getSeries(series).getItemCount(); 371 } 372 373 381 public double getXValue(int series, int item) { 382 TimeSeries s = (TimeSeries) this.data.get(series); 383 TimeSeriesDataItem i = s.getDataItem(item); 384 RegularTimePeriod period = i.getPeriod(); 385 return getX(period); 386 } 387 388 396 public Number getX(int series, int item) { 397 TimeSeries ts = (TimeSeries) this.data.get(series); 398 TimeSeriesDataItem dp = ts.getDataItem(item); 399 RegularTimePeriod period = dp.getPeriod(); 400 return new Long (getX(period)); 401 } 402 403 410 protected synchronized long getX(RegularTimePeriod period) { 411 412 long result = 0L; 413 if (this.xPosition == TimePeriodAnchor.START) { 414 result = period.getFirstMillisecond(this.workingCalendar); 415 } 416 else if (this.xPosition == TimePeriodAnchor.MIDDLE) { 417 result = period.getMiddleMillisecond(this.workingCalendar); 418 } 419 else if (this.xPosition == TimePeriodAnchor.END) { 420 result = period.getLastMillisecond(this.workingCalendar); 421 } 422 return result; 423 424 } 425 426 434 public synchronized Number getStartX(int series, int item) { 435 TimeSeries ts = (TimeSeries) this.data.get(series); 436 TimeSeriesDataItem dp = ts.getDataItem(item); 437 return new Long (dp.getPeriod().getFirstMillisecond( 438 this.workingCalendar) 439 ); 440 } 441 442 450 public synchronized Number getEndX(int series, int item) { 451 TimeSeries ts = (TimeSeries) this.data.get(series); 452 TimeSeriesDataItem dp = ts.getDataItem(item); 453 return new Long (dp.getPeriod().getLastMillisecond( 454 this.workingCalendar) 455 ); 456 } 457 458 466 public Number getY(int series, int item) { 467 TimeSeries ts = (TimeSeries) this.data.get(series); 468 TimeSeriesDataItem dp = ts.getDataItem(item); 469 return dp.getValue(); 470 } 471 472 480 public Number getStartY(int series, int item) { 481 return getY(series, item); 482 } 483 484 492 public Number getEndY(int series, int item) { 493 return getY(series, item); 494 } 495 496 497 507 public int[] getSurroundingItems(int series, long milliseconds) { 508 int[] result = new int[] {-1, -1}; 509 TimeSeries timeSeries = getSeries(series); 510 for (int i = 0; i < timeSeries.getItemCount(); i++) { 511 Number x = getX(series, i); 512 long m = x.longValue(); 513 if (m <= milliseconds) { 514 result[0] = i; 515 } 516 if (m >= milliseconds) { 517 result[1] = i; 518 break; 519 } 520 } 521 return result; 522 } 523 524 532 public double getDomainLowerBound(boolean includeInterval) { 533 double result = Double.NaN; 534 Range r = getDomainBounds(includeInterval); 535 if (r != null) { 536 result = r.getLowerBound(); 537 } 538 return result; 539 } 540 541 549 public double getDomainUpperBound(boolean includeInterval) { 550 double result = Double.NaN; 551 Range r = getDomainBounds(includeInterval); 552 if (r != null) { 553 result = r.getUpperBound(); 554 } 555 return result; 556 } 557 558 566 public Range getDomainBounds(boolean includeInterval) { 567 Range result = null; 568 Iterator iterator = this.data.iterator(); 569 while (iterator.hasNext()) { 570 TimeSeries series = (TimeSeries) iterator.next(); 571 int count = series.getItemCount(); 572 if (count > 0) { 573 RegularTimePeriod start = series.getTimePeriod(0); 574 RegularTimePeriod end = series.getTimePeriod(count - 1); 575 Range temp; 576 if (!includeInterval || this.domainIsPointsInTime) { 577 temp = new Range(getX(start), getX(end)); 578 } 579 else { 580 temp = new Range( 581 start.getFirstMillisecond(this.workingCalendar), 582 end.getLastMillisecond(this.workingCalendar) 583 ); 584 } 585 result = Range.combine(result, temp); 586 } 587 } 588 return result; 589 } 590 591 598 public boolean equals(Object obj) { 599 if (obj == this) { 600 return true; 601 } 602 if (!(obj instanceof TimeSeriesCollection)) { 603 return false; 604 } 605 TimeSeriesCollection that = (TimeSeriesCollection) obj; 606 if (this.xPosition != that.xPosition) { 607 return false; 608 } 609 if (this.domainIsPointsInTime != that.domainIsPointsInTime) { 610 return false; 611 } 612 if (!ObjectUtilities.equal(this.data, that.data)) { 613 return false; 614 } 615 return true; 616 } 617 618 623 public int hashCode() { 624 int result; 625 result = this.data.hashCode(); 626 result = 29 * result + (this.workingCalendar != null 627 ? this.workingCalendar.hashCode() : 0); 628 result = 29 * result + (this.xPosition != null 629 ? this.xPosition.hashCode() : 0); 630 result = 29 * result + (this.domainIsPointsInTime ? 1 : 0); 631 return result; 632 } 633 634 } 635 | Popular Tags |