001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * YIntervalSeriesCollection.java 029 * ------------------------------ 030 * (C) Copyright 2006-2020, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.data.xy; 038 039import java.io.Serializable; 040import java.util.List; 041import org.jfree.chart.util.ObjectUtils; 042import org.jfree.chart.util.Args; 043import org.jfree.chart.util.PublicCloneable; 044 045import org.jfree.data.general.DatasetChangeEvent; 046 047/** 048 * A collection of {@link YIntervalSeries} objects. 049 * 050 * @since 1.0.3 051 * 052 * @see YIntervalSeries 053 */ 054public class YIntervalSeriesCollection extends AbstractIntervalXYDataset 055 implements IntervalXYDataset, PublicCloneable, Serializable { 056 057 /** Storage for the data series. */ 058 private List data; 059 060 /** 061 * Creates a new instance of {@code YIntervalSeriesCollection}. 062 */ 063 public YIntervalSeriesCollection() { 064 this.data = new java.util.ArrayList(); 065 } 066 067 /** 068 * Adds a series to the collection and sends a {@link DatasetChangeEvent} 069 * to all registered listeners. 070 * 071 * @param series the series ({@code null} not permitted). 072 */ 073 public void addSeries(YIntervalSeries series) { 074 Args.nullNotPermitted(series, "series"); 075 this.data.add(series); 076 series.addChangeListener(this); 077 fireDatasetChanged(); 078 } 079 080 /** 081 * Returns the number of series in the collection. 082 * 083 * @return The series count. 084 */ 085 @Override 086 public int getSeriesCount() { 087 return this.data.size(); 088 } 089 090 /** 091 * Returns a series from the collection. 092 * 093 * @param series the series index (zero-based). 094 * 095 * @return The series. 096 * 097 * @throws IllegalArgumentException if {@code series} is not in the 098 * range {@code 0} to {@code getSeriesCount() - 1}. 099 */ 100 public YIntervalSeries getSeries(int series) { 101 if ((series < 0) || (series >= getSeriesCount())) { 102 throw new IllegalArgumentException("Series index out of bounds"); 103 } 104 return (YIntervalSeries) this.data.get(series); 105 } 106 107 /** 108 * Returns the key for a series. 109 * 110 * @param series the series index (in the range {@code 0} to 111 * {@code getSeriesCount() - 1}). 112 * 113 * @return The key for a series. 114 * 115 * @throws IllegalArgumentException if {@code series} is not in the 116 * specified range. 117 */ 118 @Override 119 public Comparable getSeriesKey(int series) { 120 // defer argument checking 121 return getSeries(series).getKey(); 122 } 123 124 /** 125 * Returns the number of items in the specified series. 126 * 127 * @param series the series (zero-based index). 128 * 129 * @return The item count. 130 * 131 * @throws IllegalArgumentException if {@code series} is not in the 132 * range {@code 0} to {@code getSeriesCount() - 1}. 133 */ 134 @Override 135 public int getItemCount(int series) { 136 // defer argument checking 137 return getSeries(series).getItemCount(); 138 } 139 140 /** 141 * Returns the x-value for an item within a series. 142 * 143 * @param series the series index. 144 * @param item the item index. 145 * 146 * @return The x-value. 147 */ 148 @Override 149 public Number getX(int series, int item) { 150 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 151 return s.getX(item); 152 } 153 154 /** 155 * Returns the y-value (as a double primitive) for an item within a 156 * series. 157 * 158 * @param series the series index (zero-based). 159 * @param item the item index (zero-based). 160 * 161 * @return The value. 162 */ 163 @Override 164 public double getYValue(int series, int item) { 165 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 166 return s.getYValue(item); 167 } 168 169 /** 170 * Returns the start y-value (as a double primitive) for an item within a 171 * series. 172 * 173 * @param series the series index (zero-based). 174 * @param item the item index (zero-based). 175 * 176 * @return The value. 177 */ 178 @Override 179 public double getStartYValue(int series, int item) { 180 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 181 return s.getYLowValue(item); 182 } 183 184 /** 185 * Returns the end y-value (as a double primitive) for an item within a 186 * series. 187 * 188 * @param series the series (zero-based index). 189 * @param item the item (zero-based index). 190 * 191 * @return The value. 192 */ 193 @Override 194 public double getEndYValue(int series, int item) { 195 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 196 return s.getYHighValue(item); 197 } 198 199 /** 200 * Returns the y-value for an item within a series. 201 * 202 * @param series the series index. 203 * @param item the item index. 204 * 205 * @return The y-value. 206 */ 207 @Override 208 public Number getY(int series, int item) { 209 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 210 return s.getYValue(item); 211 } 212 213 /** 214 * Returns the start x-value for an item within a series. This method 215 * maps directly to {@link #getX(int, int)}. 216 * 217 * @param series the series index. 218 * @param item the item index. 219 * 220 * @return The x-value. 221 */ 222 @Override 223 public Number getStartX(int series, int item) { 224 return getX(series, item); 225 } 226 227 /** 228 * Returns the end x-value for an item within a series. This method 229 * maps directly to {@link #getX(int, int)}. 230 * 231 * @param series the series index. 232 * @param item the item index. 233 * 234 * @return The x-value. 235 */ 236 @Override 237 public Number getEndX(int series, int item) { 238 return getX(series, item); 239 } 240 241 /** 242 * Returns the start y-value for an item within a series. 243 * 244 * @param series the series index. 245 * @param item the item index. 246 * 247 * @return The start y-value. 248 */ 249 @Override 250 public Number getStartY(int series, int item) { 251 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 252 return s.getYLowValue(item); 253 } 254 255 /** 256 * Returns the end y-value for an item within a series. 257 * 258 * @param series the series index. 259 * @param item the item index. 260 * 261 * @return The end y-value. 262 */ 263 @Override 264 public Number getEndY(int series, int item) { 265 YIntervalSeries s = (YIntervalSeries) this.data.get(series); 266 return s.getYHighValue(item); 267 } 268 269 /** 270 * Removes a series from the collection and sends a 271 * {@link DatasetChangeEvent} to all registered listeners. 272 * 273 * @param series the series index (zero-based). 274 * 275 * @since 1.0.10 276 */ 277 public void removeSeries(int series) { 278 if ((series < 0) || (series >= getSeriesCount())) { 279 throw new IllegalArgumentException("Series index out of bounds."); 280 } 281 YIntervalSeries ts = (YIntervalSeries) this.data.get(series); 282 ts.removeChangeListener(this); 283 this.data.remove(series); 284 fireDatasetChanged(); 285 } 286 287 /** 288 * Removes a series from the collection and sends a 289 * {@link DatasetChangeEvent} to all registered listeners. 290 * 291 * @param series the series ({@code null} not permitted). 292 * 293 * @since 1.0.10 294 */ 295 public void removeSeries(YIntervalSeries series) { 296 Args.nullNotPermitted(series, "series"); 297 if (this.data.contains(series)) { 298 series.removeChangeListener(this); 299 this.data.remove(series); 300 fireDatasetChanged(); 301 } 302 } 303 304 /** 305 * Removes all the series from the collection and sends a 306 * {@link DatasetChangeEvent} to all registered listeners. 307 * 308 * @since 1.0.10 309 */ 310 public void removeAllSeries() { 311 // Unregister the collection as a change listener to each series in 312 // the collection. 313 for (int i = 0; i < this.data.size(); i++) { 314 YIntervalSeries series = (YIntervalSeries) this.data.get(i); 315 series.removeChangeListener(this); 316 } 317 this.data.clear(); 318 fireDatasetChanged(); 319 } 320 321 /** 322 * Tests this instance for equality with an arbitrary object. 323 * 324 * @param obj the object ({@code null} permitted). 325 * 326 * @return A boolean. 327 */ 328 @Override 329 public boolean equals(Object obj) { 330 if (obj == this) { 331 return true; 332 } 333 if (!(obj instanceof YIntervalSeriesCollection)) { 334 return false; 335 } 336 YIntervalSeriesCollection that = (YIntervalSeriesCollection) obj; 337 return ObjectUtils.equal(this.data, that.data); 338 } 339 340 /** 341 * Returns a clone of this instance. 342 * 343 * @return A clone. 344 * 345 * @throws CloneNotSupportedException if there is a problem. 346 */ 347 @Override 348 public Object clone() throws CloneNotSupportedException { 349 YIntervalSeriesCollection clone 350 = (YIntervalSeriesCollection) super.clone(); 351 clone.data = (List) ObjectUtils.deepClone(this.data); 352 return clone; 353 } 354 355}