001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 *
027 * ------------------------------
028 * XIntervalSeriesCollection.java
029 * ------------------------------
030 * (C) Copyright 2006, 2007, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 20-Oct-2006 : Version 1 (DG);
038 * 27-Nov-2006 : Added clone() override (DG);
039 *
040 */
041
042package org.jfree.data.xy;
043
044import java.io.Serializable;
045import java.util.List;
046
047import org.jfree.data.general.DatasetChangeEvent;
048import org.jfree.util.ObjectUtilities;
049
050/**
051 * A collection of {@link XIntervalSeries} objects.
052 *
053 * @since 1.0.3
054 *
055 * @see XIntervalSeries
056 */
057public class XIntervalSeriesCollection extends AbstractIntervalXYDataset
058                                implements IntervalXYDataset, Serializable {
059
060    /** Storage for the data series. */
061    private List data;
062    
063    /** 
064     * Creates a new instance of <code>XIntervalSeriesCollection</code>. 
065     */
066    public XIntervalSeriesCollection() {
067        this.data = new java.util.ArrayList();
068    }
069
070    /**
071     * Adds a series to the collection and sends a {@link DatasetChangeEvent} 
072     * to all registered listeners.
073     *
074     * @param series  the series (<code>null</code> not permitted).
075     */
076    public void addSeries(XIntervalSeries series) {
077        if (series == null) {
078            throw new IllegalArgumentException("Null 'series' argument.");
079        }
080        this.data.add(series);
081        series.addChangeListener(this);
082        fireDatasetChanged();
083    }
084
085    /**
086     * Returns the number of series in the collection.
087     *
088     * @return The series count.
089     */
090    public int getSeriesCount() {
091        return this.data.size();
092    }
093
094    /**
095     * Returns a series from the collection.
096     *
097     * @param series  the series index (zero-based).
098     *
099     * @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}