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-2006, 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, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: XIntervalSeriesCollection.java,v 1.1.2.3 2006/11/27 17:27:52 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 20-Oct-2006 : Version 1 (DG);
  40:  * 27-Nov-2006 : Added clone() override (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: 
  52: /**
  53:  * A collection of {@link XIntervalSeries} objects.
  54:  *
  55:  * @since 1.0.3
  56:  *
  57:  * @see XIntervalSeries
  58:  */
  59: public class XIntervalSeriesCollection extends AbstractIntervalXYDataset
  60:                                 implements IntervalXYDataset, Serializable {
  61: 
  62:     /** Storage for the data series. */
  63:     private List data;
  64:     
  65:     /** 
  66:      * Creates a new instance of <code>XIntervalSeriesCollection</code>. 
  67:      */
  68:     public XIntervalSeriesCollection() {
  69:         this.data = new java.util.ArrayList();
  70:     }
  71: 
  72:     /**
  73:      * Adds a series to the collection and sends a {@link DatasetChangeEvent} 
  74:      * to all registered listeners.
  75:      *
  76:      * @param series  the series (<code>null</code> not permitted).
  77:      */
  78:     public void addSeries(XIntervalSeries series) {
  79:         if (series == null) {
  80:             throw new IllegalArgumentException("Null 'series' argument.");
  81:         }
  82:         this.data.add(series);
  83:         series.addChangeListener(this);
  84:         fireDatasetChanged();
  85:     }
  86: 
  87:     /**
  88:      * Returns the number of series in the collection.
  89:      *
  90:      * @return The series count.
  91:      */
  92:     public int getSeriesCount() {
  93:         return this.data.size();
  94:     }
  95: 
  96:     /**
  97:      * Returns a series from the collection.
  98:      *
  99:      * @param series  the series index (zero-based).
 100:      *
 101:      * @return The series.
 102:      * 
 103:      * @throws IllegalArgumentException if <code>series</code> is not in the
 104:      *     range <code>0</code> to <code>getSeriesCount() - 1</code>.
 105:      */
 106:     public XIntervalSeries getSeries(int series) {
 107:         if ((series < 0) || (series >= getSeriesCount())) {
 108:             throw new IllegalArgumentException("Series index out of bounds");
 109:         }
 110:         return (XIntervalSeries) this.data.get(series);
 111:     }
 112: 
 113:     /**
 114:      * Returns the key for a series.
 115:      *
 116:      * @param series  the series index (in the range <code>0</code> to 
 117:      *     <code>getSeriesCount() - 1</code>).
 118:      *
 119:      * @return The key for a series.
 120:      * 
 121:      * @throws IllegalArgumentException if <code>series</code> is not in the
 122:      *     specified range.
 123:      */
 124:     public Comparable getSeriesKey(int series) {
 125:         // defer argument checking
 126:         return getSeries(series).getKey();
 127:     }
 128: 
 129:     /**
 130:      * Returns the number of items in the specified series.
 131:      *
 132:      * @param series  the series (zero-based index).
 133:      *
 134:      * @return The item count.
 135:      * 
 136:      * @throws IllegalArgumentException if <code>series</code> is not in the
 137:      *     range <code>0</code> to <code>getSeriesCount() - 1</code>.
 138:      */
 139:     public int getItemCount(int series) {
 140:         // defer argument checking
 141:         return getSeries(series).getItemCount();
 142:     }
 143: 
 144:     /**
 145:      * Returns the x-value for an item within a series.
 146:      *
 147:      * @param series  the series index.
 148:      * @param item  the item index.
 149:      *
 150:      * @return The x-value.
 151:      */
 152:     public Number getX(int series, int item) {
 153:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 154:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 155:         return di.getX();
 156:     }
 157: 
 158:     /**
 159:      * Returns the y-value for an item within a series.
 160:      *
 161:      * @param series  the series index.
 162:      * @param item  the item index.
 163:      *
 164:      * @return The y-value.
 165:      */
 166:     public Number getY(int series, int item) {
 167:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 168:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 169:         return new Double(di.getYValue());
 170:     }
 171: 
 172:     /**
 173:      * Returns the start x-value for an item within a series.  
 174:      *
 175:      * @param series  the series index.
 176:      * @param item  the item index.
 177:      *
 178:      * @return The x-value.
 179:      */
 180:     public Number getStartX(int series, int item) {
 181:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 182:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 183:         return new Double(di.getXLowValue());
 184:     }
 185: 
 186:     /**
 187:      * Returns the end x-value for an item within a series.  
 188:      *
 189:      * @param series  the series index.
 190:      * @param item  the item index.
 191:      *
 192:      * @return The x-value.
 193:      */
 194:     public Number getEndX(int series, int item) {
 195:         XIntervalSeries s = (XIntervalSeries) this.data.get(series);
 196:         XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
 197:         return new Double(di.getXHighValue());
 198:     }
 199: 
 200:     /**
 201:      * Returns the start y-value for an item within a series.  This method
 202:      * maps directly to {@link #getY(int, int)}.
 203:      *
 204:      * @param series  the series index.
 205:      * @param item  the item index.
 206:      *
 207:      * @return The start y-value.
 208:      */
 209:     public Number getStartY(int series, int item) {
 210:         return getY(series, item);
 211:     }
 212: 
 213:     /**
 214:      * Returns the end y-value for an item within a series.  This method
 215:      * maps directly to {@link #getY(int, int)}.
 216:      *
 217:      * @param series  the series index.
 218:      * @param item  the item index.
 219:      *
 220:      * @return The end y-value.
 221:      */
 222:     public Number getEndY(int series, int item) {
 223:         return getY(series, item);
 224:     }
 225:     
 226:     /**
 227:      * Tests this instance for equality with an arbitrary object.
 228:      *
 229:      * @param obj  the object (<code>null</code> permitted).
 230:      *
 231:      * @return A boolean. 
 232:      */
 233:     public boolean equals(Object obj) {
 234:         if (obj == this) {
 235:             return true;
 236:         }
 237:         if (!(obj instanceof XIntervalSeriesCollection)) {
 238:             return false;
 239:         }
 240:         XIntervalSeriesCollection that = (XIntervalSeriesCollection) obj;
 241:         return ObjectUtilities.equal(this.data, that.data);
 242:     }
 243:     
 244:     /**
 245:      * Returns a clone of this instance.
 246:      * 
 247:      * @return A clone.
 248:      * 
 249:      * @throws CloneNotSupportedException if there is a problem.
 250:      */
 251:     public Object clone() throws CloneNotSupportedException {
 252:         XIntervalSeriesCollection clone 
 253:                 = (XIntervalSeriesCollection) super.clone();
 254:         clone.data = (List) ObjectUtilities.deepClone(this.data);
 255:         return clone;
 256:     }
 257:   
 258: }