1 52 53 package org.jfree.data.time; 54 55 import java.util.Calendar ; 56 import java.util.List ; 57 import java.util.Locale ; 58 import java.util.TimeZone ; 59 60 import org.jfree.data.DefaultKeyedValues2D; 61 import org.jfree.data.DomainInfo; 62 import org.jfree.data.Range; 63 import org.jfree.data.general.DatasetChangeEvent; 64 import org.jfree.data.xy.AbstractIntervalXYDataset; 65 import org.jfree.data.xy.IntervalXYDataset; 66 import org.jfree.data.xy.TableXYDataset; 67 import org.jfree.util.PublicCloneable; 68 69 76 public class TimeTableXYDataset extends AbstractIntervalXYDataset 77 implements Cloneable , PublicCloneable, 78 IntervalXYDataset, 79 DomainInfo, 80 TableXYDataset { 81 82 89 private DefaultKeyedValues2D values; 90 91 96 private boolean domainIsPointsInTime; 97 98 103 private TimePeriodAnchor xPosition; 104 105 106 private Calendar workingCalendar; 107 108 111 public TimeTableXYDataset() { 112 this(TimeZone.getDefault(), Locale.getDefault()); 114 } 115 116 121 public TimeTableXYDataset(TimeZone zone) { 122 this(zone, Locale.getDefault()); 124 } 125 126 132 public TimeTableXYDataset(TimeZone zone, Locale locale) { 133 if (zone == null) { 134 throw new IllegalArgumentException ("Null 'zone' argument."); 135 } 136 if (locale == null) { 137 throw new IllegalArgumentException ("Null 'locale' argument."); 138 } 139 this.values = new DefaultKeyedValues2D(true); 140 this.workingCalendar = Calendar.getInstance(zone, locale); 141 this.xPosition = TimePeriodAnchor.START; 142 } 143 144 155 public boolean getDomainIsPointsInTime() { 156 return this.domainIsPointsInTime; 157 } 158 159 166 public void setDomainIsPointsInTime(boolean flag) { 167 this.domainIsPointsInTime = flag; 168 notifyListeners(new DatasetChangeEvent(this, this)); 169 } 170 171 177 public TimePeriodAnchor getXPosition() { 178 return this.xPosition; 179 } 180 181 187 public void setXPosition(TimePeriodAnchor anchor) { 188 if (anchor == null) { 189 throw new IllegalArgumentException ("Null 'anchor' argument."); 190 } 191 this.xPosition = anchor; 192 notifyListeners(new DatasetChangeEvent(this, this)); 193 } 194 195 204 public void add(TimePeriod period, double y, String seriesName) { 205 add(period, new Double (y), seriesName, true); 206 } 207 208 217 public void add(TimePeriod period, Number y, String seriesName, 218 boolean notify) { 219 this.values.addValue(y, period, seriesName); 220 if (notify) { 221 fireDatasetChanged(); 222 } 223 } 224 225 233 public void remove(TimePeriod period, String seriesName) { 234 remove(period, seriesName, true); 235 } 236 237 246 public void remove(TimePeriod period, String seriesName, boolean notify) { 247 this.values.removeValue(period, seriesName); 248 if (notify) { 249 fireDatasetChanged(); 250 } 251 } 252 253 261 public TimePeriod getTimePeriod(int item) { 262 return (TimePeriod) this.values.getRowKey(item); 263 } 264 265 270 public int getItemCount() { 271 return this.values.getRowCount(); 272 } 273 274 283 public int getItemCount(int series) { 284 return getItemCount(); 285 } 286 287 292 public int getSeriesCount() { 293 return this.values.getColumnCount(); 294 } 295 296 303 public Comparable getSeriesKey(int series) { 304 return this.values.getColumnKey(series); 305 } 306 307 317 public Number getX(int series, int item) { 318 return new Double (getXValue(series, item)); 319 } 320 321 329 public double getXValue(int series, int item) { 330 TimePeriod period = (TimePeriod) this.values.getRowKey(item); 331 return getXValue(period); 332 } 333 334 342 public Number getStartX(int series, int item) { 343 return new Double (getStartXValue(series, item)); 344 } 345 346 355 public double getStartXValue(int series, int item) { 356 TimePeriod period = (TimePeriod) this.values.getRowKey(item); 357 return period.getStart().getTime(); 358 } 359 360 368 public Number getEndX(int series, int item) { 369 return new Double (getEndXValue(series, item)); 370 } 371 372 381 public double getEndXValue(int series, int item) { 382 TimePeriod period = (TimePeriod) this.values.getRowKey(item); 383 return period.getEnd().getTime(); 384 } 385 386 394 public Number getY(int series, int item) { 395 return this.values.getValue(item, series); 396 } 397 398 406 public Number getStartY(int series, int item) { 407 return getY(series, item); 408 } 409 410 418 public Number getEndY(int series, int item) { 419 return getY(series, item); 420 } 421 422 429 private long getXValue(TimePeriod period) { 430 long result = 0L; 431 if (this.xPosition == TimePeriodAnchor.START) { 432 result = period.getStart().getTime(); 433 } 434 else if (this.xPosition == TimePeriodAnchor.MIDDLE) { 435 long t0 = period.getStart().getTime(); 436 long t1 = period.getEnd().getTime(); 437 result = t0 + (t1 - t0) / 2L; 438 } 439 else if (this.xPosition == TimePeriodAnchor.END) { 440 result = period.getEnd().getTime(); 441 } 442 return result; 443 } 444 445 453 public double getDomainLowerBound(boolean includeInterval) { 454 double result = Double.NaN; 455 Range r = getDomainBounds(includeInterval); 456 if (r != null) { 457 result = r.getLowerBound(); 458 } 459 return result; 460 } 461 462 470 public double getDomainUpperBound(boolean includeInterval) { 471 double result = Double.NaN; 472 Range r = getDomainBounds(includeInterval); 473 if (r != null) { 474 result = r.getUpperBound(); 475 } 476 return result; 477 } 478 479 487 public Range getDomainBounds(boolean includeInterval) { 488 List keys = this.values.getRowKeys(); 489 if (keys.isEmpty()) { 490 return null; 491 } 492 493 TimePeriod first = (TimePeriod) keys.get(0); 494 TimePeriod last = (TimePeriod) keys.get(keys.size() - 1); 495 496 if (!includeInterval || this.domainIsPointsInTime) { 497 return new Range(getXValue(first), getXValue(last)); 498 } 499 else { 500 return new Range( 501 first.getStart().getTime(), last.getEnd().getTime() 502 ); 503 } 504 } 505 506 513 public boolean equals(Object obj) { 514 if (obj == this) { 515 return true; 516 } 517 if (!(obj instanceof TimeTableXYDataset)) { 518 return false; 519 } 520 TimeTableXYDataset that = (TimeTableXYDataset) obj; 521 if (this.domainIsPointsInTime != that.domainIsPointsInTime) { 522 return false; 523 } 524 if (this.xPosition != that.xPosition) { 525 return false; 526 } 527 if (!this.workingCalendar.getTimeZone().equals( 528 that.workingCalendar.getTimeZone()) 529 ) { 530 return false; 531 } 532 if (!this.values.equals(that.values)) { 533 return false; 534 } 535 return true; 536 } 537 538 545 public Object clone() throws CloneNotSupportedException { 546 TimeTableXYDataset clone = (TimeTableXYDataset) super.clone(); 547 clone.values = (DefaultKeyedValues2D) this.values.clone(); 548 clone.workingCalendar = (Calendar ) this.workingCalendar.clone(); 549 return clone; 550 } 551 552 } 553 | Popular Tags |