Source for org.jfree.data.xy.XIntervalSeriesCollection

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it
  10:  * under the terms of the GNU Lesser General Public License as published by
  11:  * the Free Software Foundation; either version 2.1 of the License, or
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  22:  * USA.
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------------------
  28:  * XIntervalSeriesCollection.java
  29:  * ------------------------------
  30:  * (C) Copyright 2006-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 20-Oct-2006 : Version 1 (DG);
  38:  * 27-Nov-2006 : Added clone() override (DG);
  39:  * 18-Jan-2008 : Added removeSeries() and removeAllSeries() methods (DG);
  40:  * 22-Apr-2008 : Implemented PublicCloneable (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.data.xy;
  45: 
  46: import java.io.Serializable;
  47: import java.util.List;
  48: 
  49: import org.jfree.data.general.DatasetChangeEvent;
  50: import org.jfree.util.ObjectUtilities;
  51: import org.jfree.util.PublicCloneable;
  52: 
  53: /**
  54:  * A collection of {@link XIntervalSeries} objects.
  55:  *
  56:  * @since 1.0.3
  57:  *
  58:  * @see XIntervalSeries
  59:  */
  60: public class XIntervalSeriesCollection extends AbstractIntervalXYDataset
  61:         implements IntervalXYDataset, PublicCloneable, Serializable {
  62: 
  63:     /** Storage for the data series. */
  64:     private List data;
  65: 
  66:     /**
  67:      * Creates a new instance of <code>XIntervalSeriesCollection</code>.
  68:      */
  69:     public XIntervalSeriesCollection() {
  70:         this.data = new java.util.ArrayList();
  71:     }
  72: 
  73:     /**
  74:      * Adds a series to the collection and sends a {@link DatasetChangeEvent}
  75:      * to all registered listeners.
  76:      *
  77:      * @param series  the series (<code>null</code> not permitted).
  78:      */
  79:     public void addSeries(XIntervalSeries series) {
  80:         if (series == null) {
  81:             throw new IllegalArgumentException("Null 'series' argument.");
  82:         }
  83:         this.data.add(series);
  84:         series.addChangeListener(this);
  85:         fireDatasetChanged();
  86:     }
  87: 
  88:     /**
  89:      * Returns the number of series in the collection.
  90:      *
  91:      * @return The series count.
  92:      */
  93:     public int getSeriesCount() {
  94:         return this.data.size();
  95:     }
  96: 
  97:     /**
  98:      * Returns a series from the collection.
  99:      *
 100:      * @param series  the series index (zero-based).
 101:      *
 102:      * @return The series.
 103:      *
 104:      * @throws IllegalArgumentException if <code>series</code> is not in the
 105:      *     range <code>0</code> to <code>getSeriesCount() - 1</code>.
 106:      */
 107:     public XIntervalSeries getSeries(int series) {
 108:         if ((series < 0) || (series >= getSeriesCount())) {
 109:             throw new IllegalArgumentException("Series index out of bounds");
 110:         }
 111:         return (XIntervalSeries) this.data.get(series);
 112:     }
 113: 
 114:     /**
 115:      * Returns the key for a series.
 116:      *
 117:      * @param series  the series index (in the range <code>0</code> to
 118:      *     <code>getSeriesCount() - 1</code>).
 119:      *
 120:      * @return The key for a series.
 121:      *
 122:      * @throws IllegalArgumentException if <code>series</code> is not in the
 123:      *     specified range.
 124:      */
 125:     public Comparable getSeriesKey(int series) {
 126:         // defer argument checking
 127:         return getSeries(series).getKey();
 128:     }
 129: 
 130:     /**
 131:      * Returns the number of items in the specified series.
 132:      *
 133:      * @param series  the series (zero-based index).
 134:      *
 135:      * @return The item count.
 136:      *
 137:      * @throws IllegalArgumentException if <code>series</code> is not in the
 138:      *     range <code>0</code> to <code>getSeriesCount() - 1</code>.
 139:      */
 140:     public int getItemCount(int series) {
 141:         // defer argument checking
 142:         return getSeries(series).getItemCount();
 143:     }
 144: 
 145:     /**
 146:      * Returns the x-value for an item within a series.
 147:      *
 148:      * @param series  the series index.
 149:      * @param item  the item index.
 150:      *
 151:      * @return The x-value.
 152:      */
 153:     public Number getX(int series, int item) {
 154:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 155:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 156:         return di.getX();
 157:     }
 158: 
 159:     /**
 160:      * Returns the start x-value (as a double primitive) for an item within a
 161:      * series.
 162:      *
 163:      * @param series  the series index (zero-based).
 164:      * @param item  the item index (zero-based).
 165:      *
 166:      * @return The value.
 167:      */
 168:     public double getStartXValue(int series, int item) {
 169:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 170:         return s.getXLowValue(item);
 171:     }
 172: 
 173:     /**
 174:      * Returns the end x-value (as a double primitive) for an item within a
 175:      * series.
 176:      *
 177:      * @param series  the series (zero-based index).
 178:      * @param item  the item (zero-based index).
 179:      *
 180:      * @return The value.
 181:      */
 182:     public double getEndXValue(int series, int item) {
 183:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 184:         return s.getXHighValue(item);
 185:     }
 186: 
 187:     /**
 188:      * Returns the y-value (as a double primitive) for an item within a
 189:      * series.
 190:      *
 191:      * @param series  the series index (zero-based).
 192:      * @param item  the item index (zero-based).
 193:      *
 194:      * @return The value.
 195:      */
 196:     public double getYValue(int series, int item) {
 197:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 198:         return s.getYValue(item);
 199:     }
 200: 
 201:     /**
 202:      * Returns the y-value for an item within a series.
 203:      *
 204:      * @param series  the series index.
 205:      * @param item  the item index.
 206:      *
 207:      * @return The y-value.
 208:      */
 209:     public Number getY(int series, int item) {
 210:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 211:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 212:         return new Double(di.getYValue());
 213:     }
 214: 
 215:     /**
 216:      * Returns the start x-value for an item within a series.
 217:      *
 218:      * @param series  the series index.
 219:      * @param item  the item index.
 220:      *
 221:      * @return The x-value.
 222:      */
 223:     public Number getStartX(int series, int item) {
 224:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 225:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 226:         return new Double(di.getXLowValue());
 227:     }
 228: 
 229:     /**
 230:      * Returns the end x-value for an item within a series.
 231:      *
 232:      * @param series  the series index.
 233:      * @param item  the item index.
 234:      *
 235:      * @return The x-value.
 236:      */
 237:     public Number getEndX(int series, int item) {
 238:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 239:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 240:         return new Double(di.getXHighValue());
 241:     }
 242: 
 243:     /**
 244:      * Returns the start y-value for an item within a series.  This method
 245:      * maps directly to {@link #getY(int, int)}.
 246:      *
 247:      * @param series  the series index.
 248:      * @param item  the item index.
 249:      *
 250:      * @return The start y-value.
 251:      */
 252:     public Number getStartY(int series, int item) {
 253:         return getY(series, item);
 254:     }
 255: 
 256:     /**
 257:      * Returns the end y-value for an item within a series.  This method
 258:      * maps directly to {@link #getY(int, int)}.
 259:      *
 260:      * @param series  the series index.
 261:      * @param item  the item index.
 262:      *
 263:      * @return The end y-value.
 264:      */
 265:     public Number getEndY(int series, int item) {
 266:         return getY(series, 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:         XIntervalSeries ts = (XIntervalSeries) 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</code> not permitted).
 292:      *
 293:      * @since 1.0.10
 294:      */
 295:     public void removeSeries(XIntervalSeries series) {
 296:         if (series == null) {
 297:             throw new IllegalArgumentException("Null 'series' argument.");
 298:         }
 299:         if (this.data.contains(series)) {
 300:             series.removeChangeListener(this);
 301:             this.data.remove(series);
 302:             fireDatasetChanged();
 303:         }
 304:     }
 305: 
 306:     /**
 307:      * Removes all the series from the collection and sends a
 308:      * {@link DatasetChangeEvent} to all registered listeners.
 309:      *
 310:      * @since 1.0.10
 311:      */
 312:     public void removeAllSeries() {
 313:         // Unregister the collection as a change listener to each series in
 314:         // the collection.
 315:         for (int i = 0; i < this.data.size(); i++) {
 316:           XIntervalSeries series = (XIntervalSeries) this.data.get(i);
 317:           series.removeChangeListener(this);
 318:         }
 319:         this.data.clear();
 320:         fireDatasetChanged();
 321:     }
 322: 
 323:     /**
 324:      * Tests this instance for equality with an arbitrary object.
 325:      *
 326:      * @param obj  the object (<code>null</code> permitted).
 327:      *
 328:      * @return A boolean.
 329:      */
 330:     public boolean equals(Object obj) {
 331:         if (obj == this) {
 332:             return true;
 333:         }
 334:         if (!(obj instanceof XIntervalSeriesCollection)) {
 335:             return false;
 336:         }
 337:         XIntervalSeriesCollection that = (XIntervalSeriesCollection) obj;
 338:         return ObjectUtilities.equal(this.data, that.data);
 339:     }
 340: 
 341:     /**
 342:      * Returns a clone of this instance.
 343:      *
 344:      * @return A clone.
 345:      *
 346:      * @throws CloneNotSupportedException if there is a problem.
 347:      */
 348:     public Object clone() throws CloneNotSupportedException {
 349:         XIntervalSeriesCollection clone
 350:                 = (XIntervalSeriesCollection) super.clone();
 351:         clone.data = (List) ObjectUtilities.deepClone(this.data);
 352:         return clone;
 353:     }
 354: 
 355: }