001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert 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 * MatrixSeriesCollection.java 029 * --------------------------- 030 * (C) Copyright 2003-present, by Barak Naveh and Contributors. 031 * 032 * Original Author: Barak Naveh; 033 * Contributor(s): David Gilbert; 034 * 035 */ 036 037package org.jfree.data.xy; 038 039import java.io.Serializable; 040import java.util.List; 041import java.util.Objects; 042import org.jfree.chart.util.ObjectUtils; 043import org.jfree.chart.util.Args; 044import org.jfree.chart.util.PublicCloneable; 045 046/** 047 * Represents a collection of {@link MatrixSeries} that can be used as a 048 * dataset. 049 * 050 * @see org.jfree.data.xy.MatrixSeries 051 */ 052public class MatrixSeriesCollection extends AbstractXYZDataset 053 implements XYZDataset, PublicCloneable, Serializable { 054 055 /** For serialization. */ 056 private static final long serialVersionUID = -3197705779242543945L; 057 058 /** The series that are included in the collection. */ 059 private List seriesList; 060 061 /** 062 * Constructs an empty dataset. 063 */ 064 public MatrixSeriesCollection() { 065 this(null); 066 } 067 068 069 /** 070 * Constructs a dataset and populates it with a single matrix series. 071 * 072 * @param series the time series. 073 */ 074 public MatrixSeriesCollection(MatrixSeries series) { 075 this.seriesList = new java.util.ArrayList(); 076 077 if (series != null) { 078 this.seriesList.add(series); 079 series.addChangeListener(this); 080 } 081 } 082 083 /** 084 * Returns the number of items in the specified series. 085 * 086 * @param seriesIndex zero-based series index. 087 * 088 * @return The number of items in the specified series. 089 */ 090 @Override 091 public int getItemCount(int seriesIndex) { 092 return getSeries(seriesIndex).getItemCount(); 093 } 094 095 096 /** 097 * Returns the series having the specified index. 098 * 099 * @param seriesIndex zero-based series index. 100 * 101 * @return The series. 102 */ 103 public MatrixSeries getSeries(int seriesIndex) { 104 if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) { 105 throw new IllegalArgumentException("Index outside valid range."); 106 } 107 MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex); 108 return series; 109 } 110 111 112 /** 113 * Returns the number of series in the collection. 114 * 115 * @return The number of series in the collection. 116 */ 117 @Override 118 public int getSeriesCount() { 119 return this.seriesList.size(); 120 } 121 122 123 /** 124 * Returns the key for a series. 125 * 126 * @param seriesIndex zero-based series index. 127 * 128 * @return The key for a series. 129 */ 130 @Override 131 public Comparable getSeriesKey(int seriesIndex) { 132 return getSeries(seriesIndex).getKey(); 133 } 134 135 136 /** 137 * Returns the j index value of the specified Mij matrix item in the 138 * specified matrix series. 139 * 140 * @param seriesIndex zero-based series index. 141 * @param itemIndex zero-based item index. 142 * 143 * @return The j index value for the specified matrix item. 144 * 145 * @see org.jfree.data.xy.XYDataset#getXValue(int, int) 146 */ 147 @Override 148 public Number getX(int seriesIndex, int itemIndex) { 149 MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex); 150 return series.getItemColumn(itemIndex); 151 } 152 153 154 /** 155 * Returns the i index value of the specified Mij matrix item in the 156 * specified matrix series. 157 * 158 * @param seriesIndex zero-based series index. 159 * @param itemIndex zero-based item index. 160 * 161 * @return The i index value for the specified matrix item. 162 * 163 * @see org.jfree.data.xy.XYDataset#getYValue(int, int) 164 */ 165 @Override 166 public Number getY(int seriesIndex, int itemIndex) { 167 MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex); 168 int y = series.getItemRow(itemIndex); 169 170 return y; // I know it's bad to create object. better idea? 171 } 172 173 174 /** 175 * Returns the Mij item value of the specified Mij matrix item in the 176 * specified matrix series. 177 * 178 * @param seriesIndex the series (zero-based index). 179 * @param itemIndex zero-based item index. 180 * 181 * @return The Mij item value for the specified matrix item. 182 * 183 * @see org.jfree.data.xy.XYZDataset#getZValue(int, int) 184 */ 185 @Override 186 public Number getZ(int seriesIndex, int itemIndex) { 187 MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex); 188 Number z = series.getItem(itemIndex); 189 return z; 190 } 191 192 193 /** 194 * Adds a series to the collection. 195 * <P> 196 * Notifies all registered listeners that the dataset has changed. 197 * </p> 198 * 199 * @param series the series ({@code null} not permitted). 200 */ 201 public void addSeries(MatrixSeries series) { 202 Args.nullNotPermitted(series, "series"); 203 // FIXME: Check that there isn't already a series with the same key 204 205 // add the series... 206 this.seriesList.add(series); 207 series.addChangeListener(this); 208 fireDatasetChanged(); 209 } 210 211 212 /** 213 * Tests this collection for equality with an arbitrary object. 214 * 215 * @param obj the object. 216 * 217 * @return A boolean. 218 */ 219 @Override 220 public boolean equals(Object obj) { 221 if (obj == null) { 222 return false; 223 } 224 225 if (obj == this) { 226 return true; 227 } 228 229 if (obj instanceof MatrixSeriesCollection) { 230 MatrixSeriesCollection c = (MatrixSeriesCollection) obj; 231 232 return Objects.equals(this.seriesList, c.seriesList); 233 } 234 235 return false; 236 } 237 238 /** 239 * Returns a hash code. 240 * 241 * @return A hash code. 242 */ 243 @Override 244 public int hashCode() { 245 return (this.seriesList != null ? this.seriesList.hashCode() : 0); 246 } 247 248 /** 249 * Returns a clone of this instance. 250 * 251 * @return A clone. 252 * 253 * @throws CloneNotSupportedException if there is a problem. 254 */ 255 @Override 256 public Object clone() throws CloneNotSupportedException { 257 MatrixSeriesCollection clone = (MatrixSeriesCollection) super.clone(); 258 clone.seriesList = (List) ObjectUtils.deepClone(this.seriesList); 259 return clone; 260 } 261 262 /** 263 * Removes all the series from the collection. 264 * <P> 265 * Notifies all registered listeners that the dataset has changed. 266 * </p> 267 */ 268 public void removeAllSeries() { 269 // Unregister the collection as a change listener to each series in 270 // the collection. 271 for (int i = 0; i < this.seriesList.size(); i++) { 272 MatrixSeries series = (MatrixSeries) this.seriesList.get(i); 273 series.removeChangeListener(this); 274 } 275 276 // Remove all the series from the collection and notify listeners. 277 this.seriesList.clear(); 278 fireDatasetChanged(); 279 } 280 281 282 /** 283 * Removes a series from the collection. 284 * <P> 285 * Notifies all registered listeners that the dataset has changed. 286 * </p> 287 * 288 * @param series the series ({@code null}). 289 */ 290 public void removeSeries(MatrixSeries series) { 291 Args.nullNotPermitted(series, "series"); 292 if (this.seriesList.contains(series)) { 293 series.removeChangeListener(this); 294 this.seriesList.remove(series); 295 fireDatasetChanged(); 296 } 297 } 298 299 300 /** 301 * Removes a series from the collection. 302 * <P> 303 * Notifies all registered listeners that the dataset has changed. 304 * 305 * @param seriesIndex the series (zero based index). 306 */ 307 public void removeSeries(int seriesIndex) { 308 // check arguments... 309 if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) { 310 throw new IllegalArgumentException("Index outside valid range."); 311 } 312 313 // fetch the series, remove the change listener, then remove the series. 314 MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex); 315 series.removeChangeListener(this); 316 this.seriesList.remove(seriesIndex); 317 fireDatasetChanged(); 318 } 319 320}