001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------------------- 028 * TimePeriodValuesCollection.java 029 * ------------------------------- 030 * (C) Copyright 2003-2013, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 22-Apr-2003 : Version 1 (DG); 038 * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG); 039 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 040 * getYValue() (DG); 041 * 06-Oct-2004 : Updated for changes in DomainInfo interface (DG); 042 * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG); 043 * ------------- JFREECHART 1.0.x --------------------------------------------- 044 * 03-Oct-2006 : Deprecated get/setDomainIsPointsInTime() (DG); 045 * 11-Jun-2007 : Fixed bug in getDomainBounds() method, and changed default 046 * value for domainIsPointsInTime to false (DG); 047 * 03-Jul-2013 : Use ParamChecks (DG); 048 * 049 */ 050 051package org.jfree.data.time; 052 053import java.io.Serializable; 054import java.util.Iterator; 055import java.util.List; 056import org.jfree.chart.util.ParamChecks; 057 058import org.jfree.data.DomainInfo; 059import org.jfree.data.Range; 060import org.jfree.data.xy.AbstractIntervalXYDataset; 061import org.jfree.data.xy.IntervalXYDataset; 062import org.jfree.util.ObjectUtilities; 063 064/** 065 * A collection of {@link TimePeriodValues} objects. 066 * <P> 067 * This class implements the {@link org.jfree.data.xy.XYDataset} interface, as 068 * well as the extended {@link IntervalXYDataset} interface. This makes it a 069 * convenient dataset for use with the {@link org.jfree.chart.plot.XYPlot} 070 * class. 071 */ 072public class TimePeriodValuesCollection extends AbstractIntervalXYDataset 073 implements IntervalXYDataset, DomainInfo, Serializable { 074 075 /** For serialization. */ 076 private static final long serialVersionUID = -3077934065236454199L; 077 078 /** Storage for the time series. */ 079 private List data; 080 081 /** 082 * The position within a time period to return as the x-value (START, 083 * MIDDLE or END). 084 */ 085 private TimePeriodAnchor xPosition; 086 087 /** 088 * A flag that indicates that the domain is 'points in time'. If this 089 * flag is true, only the x-value is used to determine the range of values 090 * in the domain, the start and end x-values are ignored. 091 */ 092 private boolean domainIsPointsInTime; 093 094 /** 095 * Constructs an empty dataset. 096 */ 097 public TimePeriodValuesCollection() { 098 this((TimePeriodValues) null); 099 } 100 101 /** 102 * Constructs a dataset containing a single series. Additional series can 103 * be added. 104 * 105 * @param series the series (<code>null</code> ignored). 106 */ 107 public TimePeriodValuesCollection(TimePeriodValues series) { 108 this.data = new java.util.ArrayList(); 109 this.xPosition = TimePeriodAnchor.MIDDLE; 110 this.domainIsPointsInTime = false; 111 if (series != null) { 112 this.data.add(series); 113 series.addChangeListener(this); 114 } 115 } 116 117 /** 118 * Returns the position of the X value within each time period. 119 * 120 * @return The position (never <code>null</code>). 121 * 122 * @see #setXPosition(TimePeriodAnchor) 123 */ 124 public TimePeriodAnchor getXPosition() { 125 return this.xPosition; 126 } 127 128 /** 129 * Sets the position of the x axis within each time period. 130 * 131 * @param position the position (<code>null</code> not permitted). 132 * 133 * @see #getXPosition() 134 */ 135 public void setXPosition(TimePeriodAnchor position) { 136 ParamChecks.nullNotPermitted(position, "position"); 137 this.xPosition = position; 138 } 139 140 /** 141 * Returns the number of series in the collection. 142 * 143 * @return The series count. 144 */ 145 @Override 146 public int getSeriesCount() { 147 return this.data.size(); 148 } 149 150 /** 151 * Returns a series. 152 * 153 * @param series the index of the series (zero-based). 154 * 155 * @return The series. 156 */ 157 public TimePeriodValues getSeries(int series) { 158 if ((series < 0) || (series >= getSeriesCount())) { 159 throw new IllegalArgumentException("Index 'series' out of range."); 160 } 161 return (TimePeriodValues) this.data.get(series); 162 } 163 164 /** 165 * Returns the key for a series. 166 * 167 * @param series the index of the series (zero-based). 168 * 169 * @return The key for a series. 170 */ 171 @Override 172 public Comparable getSeriesKey(int series) { 173 // defer argument checking 174 return getSeries(series).getKey(); 175 } 176 177 /** 178 * Adds a series to the collection. A 179 * {@link org.jfree.data.general.DatasetChangeEvent} is sent to all 180 * registered listeners. 181 * 182 * @param series the time series. 183 */ 184 public void addSeries(TimePeriodValues series) { 185 ParamChecks.nullNotPermitted(series, "series"); 186 this.data.add(series); 187 series.addChangeListener(this); 188 fireDatasetChanged(); 189 } 190 191 /** 192 * Removes the specified series from the collection. 193 * 194 * @param series the series to remove (<code>null</code> not permitted). 195 */ 196 public void removeSeries(TimePeriodValues series) { 197 ParamChecks.nullNotPermitted(series, "series"); 198 this.data.remove(series); 199 series.removeChangeListener(this); 200 fireDatasetChanged(); 201 202 } 203 204 /** 205 * Removes a series from the collection. 206 * 207 * @param index the series index (zero-based). 208 */ 209 public void removeSeries(int index) { 210 TimePeriodValues series = getSeries(index); 211 if (series != null) { 212 removeSeries(series); 213 } 214 } 215 216 /** 217 * Returns the number of items in the specified series. 218 * <P> 219 * This method is provided for convenience. 220 * 221 * @param series the index of the series of interest (zero-based). 222 * 223 * @return The number of items in the specified series. 224 */ 225 @Override 226 public int getItemCount(int series) { 227 return getSeries(series).getItemCount(); 228 } 229 230 /** 231 * Returns the x-value for the specified series and item. 232 * 233 * @param series the series (zero-based index). 234 * @param item the item (zero-based index). 235 * 236 * @return The x-value for the specified series and item. 237 */ 238 @Override 239 public Number getX(int series, int item) { 240 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 241 TimePeriodValue dp = ts.getDataItem(item); 242 TimePeriod period = dp.getPeriod(); 243 return new Long(getX(period)); 244 } 245 246 /** 247 * Returns the x-value for a time period. 248 * 249 * @param period the time period. 250 * 251 * @return The x-value. 252 */ 253 private long getX(TimePeriod period) { 254 255 if (this.xPosition == TimePeriodAnchor.START) { 256 return period.getStart().getTime(); 257 } 258 else if (this.xPosition == TimePeriodAnchor.MIDDLE) { 259 return period.getStart().getTime() 260 / 2 + period.getEnd().getTime() / 2; 261 } 262 else if (this.xPosition == TimePeriodAnchor.END) { 263 return period.getEnd().getTime(); 264 } 265 else { 266 throw new IllegalStateException("TimePeriodAnchor unknown."); 267 } 268 269 } 270 271 /** 272 * Returns the starting X value for the specified series and item. 273 * 274 * @param series the series (zero-based index). 275 * @param item the item (zero-based index). 276 * 277 * @return The starting X value for the specified series and item. 278 */ 279 @Override 280 public Number getStartX(int series, int item) { 281 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 282 TimePeriodValue dp = ts.getDataItem(item); 283 return new Long(dp.getPeriod().getStart().getTime()); 284 } 285 286 /** 287 * Returns the ending X value for the specified series and item. 288 * 289 * @param series the series (zero-based index). 290 * @param item the item (zero-based index). 291 * 292 * @return The ending X value for the specified series and item. 293 */ 294 @Override 295 public Number getEndX(int series, int item) { 296 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 297 TimePeriodValue dp = ts.getDataItem(item); 298 return new Long(dp.getPeriod().getEnd().getTime()); 299 } 300 301 /** 302 * Returns the y-value for the specified series and item. 303 * 304 * @param series the series (zero-based index). 305 * @param item the item (zero-based index). 306 * 307 * @return The y-value for the specified series and item. 308 */ 309 @Override 310 public Number getY(int series, int item) { 311 TimePeriodValues ts = (TimePeriodValues) this.data.get(series); 312 TimePeriodValue dp = ts.getDataItem(item); 313 return dp.getValue(); 314 } 315 316 /** 317 * Returns the starting Y value for the specified series and item. 318 * 319 * @param series the series (zero-based index). 320 * @param item the item (zero-based index). 321 * 322 * @return The starting Y value for the specified series and item. 323 */ 324 @Override 325 public Number getStartY(int series, int item) { 326 return getY(series, item); 327 } 328 329 /** 330 * Returns the ending Y value for the specified series and item. 331 * 332 * @param series the series (zero-based index). 333 * @param item the item (zero-based index). 334 * 335 * @return The ending Y value for the specified series and item. 336 */ 337 @Override 338 public Number getEndY(int series, int item) { 339 return getY(series, item); 340 } 341 342 /** 343 * Returns the minimum x-value in the dataset. 344 * 345 * @param includeInterval a flag that determines whether or not the 346 * x-interval is taken into account. 347 * 348 * @return The minimum value. 349 */ 350 @Override 351 public double getDomainLowerBound(boolean includeInterval) { 352 double result = Double.NaN; 353 Range r = getDomainBounds(includeInterval); 354 if (r != null) { 355 result = r.getLowerBound(); 356 } 357 return result; 358 } 359 360 /** 361 * Returns the maximum x-value in the dataset. 362 * 363 * @param includeInterval a flag that determines whether or not the 364 * x-interval is taken into account. 365 * 366 * @return The maximum value. 367 */ 368 @Override 369 public double getDomainUpperBound(boolean includeInterval) { 370 double result = Double.NaN; 371 Range r = getDomainBounds(includeInterval); 372 if (r != null) { 373 result = r.getUpperBound(); 374 } 375 return result; 376 } 377 378 /** 379 * Returns the range of the values in this dataset's domain. 380 * 381 * @param includeInterval a flag that determines whether or not the 382 * x-interval is taken into account. 383 * 384 * @return The range. 385 */ 386 @Override 387 public Range getDomainBounds(boolean includeInterval) { 388 boolean interval = includeInterval || this.domainIsPointsInTime; 389 Range result = null; 390 Range temp = null; 391 Iterator iterator = this.data.iterator(); 392 while (iterator.hasNext()) { 393 TimePeriodValues series = (TimePeriodValues) iterator.next(); 394 int count = series.getItemCount(); 395 if (count > 0) { 396 TimePeriod start = series.getTimePeriod( 397 series.getMinStartIndex()); 398 TimePeriod end = series.getTimePeriod(series.getMaxEndIndex()); 399 if (!interval) { 400 if (this.xPosition == TimePeriodAnchor.START) { 401 TimePeriod maxStart = series.getTimePeriod( 402 series.getMaxStartIndex()); 403 temp = new Range(start.getStart().getTime(), 404 maxStart.getStart().getTime()); 405 } 406 else if (this.xPosition == TimePeriodAnchor.MIDDLE) { 407 TimePeriod minMiddle = series.getTimePeriod( 408 series.getMinMiddleIndex()); 409 long s1 = minMiddle.getStart().getTime(); 410 long e1 = minMiddle.getEnd().getTime(); 411 TimePeriod maxMiddle = series.getTimePeriod( 412 series.getMaxMiddleIndex()); 413 long s2 = maxMiddle.getStart().getTime(); 414 long e2 = maxMiddle.getEnd().getTime(); 415 temp = new Range(s1 + (e1 - s1) / 2, 416 s2 + (e2 - s2) / 2); 417 } 418 else if (this.xPosition == TimePeriodAnchor.END) { 419 TimePeriod minEnd = series.getTimePeriod( 420 series.getMinEndIndex()); 421 temp = new Range(minEnd.getEnd().getTime(), 422 end.getEnd().getTime()); 423 } 424 } 425 else { 426 temp = new Range(start.getStart().getTime(), 427 end.getEnd().getTime()); 428 } 429 result = Range.combine(result, temp); 430 } 431 } 432 return result; 433 } 434 435 /** 436 * Tests this instance for equality with an arbitrary object. 437 * 438 * @param obj the object (<code>null</code> permitted). 439 * 440 * @return A boolean. 441 */ 442 @Override 443 public boolean equals(Object obj) { 444 if (obj == this) { 445 return true; 446 } 447 if (!(obj instanceof TimePeriodValuesCollection)) { 448 return false; 449 } 450 TimePeriodValuesCollection that = (TimePeriodValuesCollection) obj; 451 if (this.domainIsPointsInTime != that.domainIsPointsInTime) { 452 return false; 453 } 454 if (this.xPosition != that.xPosition) { 455 return false; 456 } 457 if (!ObjectUtilities.equal(this.data, that.data)) { 458 return false; 459 } 460 return true; 461 } 462 463 // --- DEPRECATED METHODS ------------------------------------------------- 464 465 /** 466 * Returns a flag that controls whether the domain is treated as 'points 467 * in time'. This flag is used when determining the max and min values for 468 * the domain. If true, then only the x-values are considered for the max 469 * and min values. If false, then the start and end x-values will also be 470 * taken into consideration 471 * 472 * @return The flag. 473 * 474 * @deprecated This flag is no longer used by JFreeChart (as of version 475 * 1.0.3). 476 */ 477 public boolean getDomainIsPointsInTime() { 478 return this.domainIsPointsInTime; 479 } 480 481 /** 482 * Sets a flag that controls whether the domain is treated as 'points in 483 * time', or time periods. 484 * 485 * @param flag the new value of the flag. 486 * 487 * @deprecated This flag is no longer used by JFreeChart (as of version 488 * 1.0.3). 489 */ 490 public void setDomainIsPointsInTime(boolean flag) { 491 this.domainIsPointsInTime = flag; 492 } 493 494}