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