1 46 47 package org.jfree.data.time; 48 49 import java.io.Serializable ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 import org.jfree.data.DomainInfo; 54 import org.jfree.data.Range; 55 import org.jfree.data.xy.AbstractIntervalXYDataset; 56 import org.jfree.data.xy.IntervalXYDataset; 57 import org.jfree.util.ObjectUtilities; 58 59 67 public class TimePeriodValuesCollection extends AbstractIntervalXYDataset 68 implements IntervalXYDataset, 69 DomainInfo, 70 Serializable { 71 72 73 private static final long serialVersionUID = -3077934065236454199L; 74 75 76 private List data; 77 78 82 private TimePeriodAnchor xPosition; 83 84 89 private boolean domainIsPointsInTime; 90 91 94 public TimePeriodValuesCollection() { 95 this((TimePeriodValues) null); 96 } 97 98 104 public TimePeriodValuesCollection(TimePeriodValues series) { 105 this.data = new java.util.ArrayList (); 106 this.xPosition = TimePeriodAnchor.MIDDLE; 107 this.domainIsPointsInTime = true; 108 if (series != null) { 109 this.data.add(series); 110 series.addChangeListener(this); 111 } 112 } 113 114 119 public TimePeriodAnchor getXPosition() { 120 return this.xPosition; 121 } 122 123 128 public void setXPosition(TimePeriodAnchor position) { 129 if (position == null) { 130 throw new IllegalArgumentException ("Null 'position' argument."); 131 } 132 this.xPosition = position; 133 } 134 135 144 public boolean getDomainIsPointsInTime() { 145 return this.domainIsPointsInTime; 146 } 147 148 154 public void setDomainIsPointsInTime(boolean flag) { 155 this.domainIsPointsInTime = flag; 156 } 157 158 163 public int getSeriesCount() { 164 return this.data.size(); 165 } 166 167 174 public TimePeriodValues getSeries(int series) { 175 176 if ((series < 0) || (series > getSeriesCount())) { 177 throw new IllegalArgumentException ("Index 'series' out of range."); 178 } 179 180 return (TimePeriodValues) this.data.get(series); 181 182 } 183 184 191 public Comparable getSeriesKey(int series) { 192 return getSeries(series).getKey(); 194 } 195 196 203 public void addSeries(TimePeriodValues series) { 204 205 if (series == null) { 206 throw new IllegalArgumentException ("Null 'series' argument."); 207 } 208 209 this.data.add(series); 210 series.addChangeListener(this); 211 fireDatasetChanged(); 212 213 } 214 215 220 public void removeSeries(TimePeriodValues series) { 221 222 if (series == null) { 223 throw new IllegalArgumentException ("Null 'series' argument."); 224 } 225 this.data.remove(series); 226 series.removeChangeListener(this); 227 fireDatasetChanged(); 228 229 } 230 231 236 public void removeSeries(int index) { 237 TimePeriodValues series = getSeries(index); 238 if (series != null) { 239 removeSeries(series); 240 } 241 } 242 243 252 public int getItemCount(int series) { 253 return getSeries(series).getItemCount(); 254 } 255 256 264 public Number getX(int series, int item) { 265 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 266 TimePeriodValue dp = ts.getDataItem(item); 267 TimePeriod period = dp.getPeriod(); 268 return new Long (getX(period)); 269 } 270 271 278 private long getX(TimePeriod period) { 279 280 if (this.xPosition == TimePeriodAnchor.START) { 281 return period.getStart().getTime(); 282 } 283 else if (this.xPosition == TimePeriodAnchor.MIDDLE) { 284 return period.getStart().getTime() 285 / 2 + period.getEnd().getTime() / 2; 286 } 287 else if (this.xPosition == TimePeriodAnchor.END) { 288 return period.getEnd().getTime(); 289 } 290 else { 291 throw new IllegalStateException ("TimePeriodAnchor unknown."); 292 } 293 294 } 295 296 304 public Number getStartX(int series, int item) { 305 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 306 TimePeriodValue dp = ts.getDataItem(item); 307 return new Long (dp.getPeriod().getStart().getTime()); 308 } 309 310 318 public Number getEndX(int series, int item) { 319 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 320 TimePeriodValue dp = ts.getDataItem(item); 321 return new Long (dp.getPeriod().getEnd().getTime()); 322 } 323 324 332 public Number getY(int series, int item) { 333 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 334 TimePeriodValue dp = ts.getDataItem(item); 335 return dp.getValue(); 336 } 337 338 346 public Number getStartY(int series, int item) { 347 return getY(series, item); 348 } 349 350 358 public Number getEndY(int series, int item) { 359 return getY(series, item); 360 } 361 362 370 public double getDomainLowerBound(boolean includeInterval) { 371 double result = Double.NaN; 372 Range r = getDomainBounds(includeInterval); 373 if (r != null) { 374 result = r.getLowerBound(); 375 } 376 return result; 377 } 378 379 387 public double getDomainUpperBound(boolean includeInterval) { 388 double result = Double.NaN; 389 Range r = getDomainBounds(includeInterval); 390 if (r != null) { 391 result = r.getUpperBound(); 392 } 393 return result; 394 } 395 396 404 public Range getDomainBounds(boolean includeInterval) { 405 Range result = null; 406 Range temp = null; 407 Iterator iterator = this.data.iterator(); 408 while (iterator.hasNext()) { 409 TimePeriodValues series = (TimePeriodValues) iterator.next(); 410 int count = series.getItemCount(); 411 if (count > 0) { 412 TimePeriod start = series.getTimePeriod( 413 series.getMinStartIndex() 414 ); 415 TimePeriod end = series.getTimePeriod(series.getMaxEndIndex()); 416 if (this.domainIsPointsInTime) { 417 if (this.xPosition == TimePeriodAnchor.START) { 418 TimePeriod maxStart = series.getTimePeriod( 419 series.getMaxStartIndex() 420 ); 421 temp = new Range( 422 start.getStart().getTime(), 423 maxStart.getStart().getTime() 424 ); 425 } 426 else if (this.xPosition == TimePeriodAnchor.MIDDLE) { 427 TimePeriod minMiddle = series.getTimePeriod( 428 series.getMinMiddleIndex() 429 ); 430 long s1 = minMiddle.getStart().getTime(); 431 long e1 = minMiddle.getEnd().getTime(); 432 TimePeriod maxMiddle = series.getTimePeriod( 433 series.getMaxMiddleIndex() 434 ); 435 long s2 = maxMiddle.getStart().getTime(); 436 long e2 = maxMiddle.getEnd().getTime(); 437 temp = new Range( 438 s1 + (e1 - s1) / 2, s2 + (e2 - s2) / 2 439 ); 440 } 441 else if (this.xPosition == TimePeriodAnchor.END) { 442 TimePeriod minEnd = series.getTimePeriod( 443 series.getMinEndIndex() 444 ); 445 temp = new Range( 446 minEnd.getEnd().getTime(), end.getEnd().getTime() 447 ); 448 } 449 } 450 else { 451 temp = new Range( 452 start.getStart().getTime(), end.getEnd().getTime() 453 ); 454 } 455 result = Range.combine(result, temp); 456 } 457 } 458 return result; 459 } 460 461 468 public boolean equals(Object obj) { 469 if (obj == this) { 470 return true; 471 } 472 if (!(obj instanceof TimePeriodValuesCollection)) { 473 return false; 474 } 475 TimePeriodValuesCollection that = (TimePeriodValuesCollection) obj; 476 if (this.domainIsPointsInTime != that.domainIsPointsInTime) { 477 return false; 478 } 479 if (this.xPosition != that.xPosition) { 480 return false; 481 } 482 if (!ObjectUtilities.equal(this.data, that.data)) { 483 return false; 484 } 485 return true; 486 } 487 } 488 | Popular Tags |