001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [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 * YIntervalSeriesCollection.java
029 * ------------------------------
030 * (C) Copyright 2006-2013, 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 * 20-Feb-2007 : Added getYValue(), getStartYValue() and getEndYValue()
040 *               methods (DG);
041 * 18-Jan-2008 : Added removeSeries() and removeAllSeries() methods (DG);
042 * 22-Apr-2008 : Implemented PublicCloneable (DG);
043 * 02-Jul-2013 : Use ParamChecks (DG);
044 *
045 */
046
047package org.jfree.data.xy;
048
049import java.io.Serializable;
050import java.util.List;
051import org.jfree.chart.util.ParamChecks;
052
053import org.jfree.data.general.DatasetChangeEvent;
054import org.jfree.util.ObjectUtilities;
055import org.jfree.util.PublicCloneable;
056
057/**
058 * A collection of {@link YIntervalSeries} objects.
059 *
060 * @since 1.0.3
061 *
062 * @see YIntervalSeries
063 */
064public class YIntervalSeriesCollection extends AbstractIntervalXYDataset
065        implements IntervalXYDataset, PublicCloneable, Serializable {
066
067    /** Storage for the data series. */
068    private List data;
069
070    /**
071     * Creates a new instance of <code>YIntervalSeriesCollection</code>.
072     */
073    public YIntervalSeriesCollection() {
074        this.data = new java.util.ArrayList();
075    }
076
077    /**
078     * Adds a series to the collection and sends a {@link DatasetChangeEvent}
079     * to all registered listeners.
080     *
081     * @param series  the series (<code>null</code> not permitted).
082     */
083    public void addSeries(YIntervalSeries series) {
084        ParamChecks.nullNotPermitted(series, "series");
085        this.data.add(series);
086        series.addChangeListener(this);
087        fireDatasetChanged();
088    }
089
090    /**
091     * Returns the number of series in the collection.
092     *
093     * @return The series count.
094     */
095    @Override
096    public int getSeriesCount() {
097        return this.data.size();
098    }
099
100    /**
101     * Returns a series from the collection.
102     *
103     * @param series  the series index (zero-based).
104     *
105     * @return The series.
106     *
107     * @throws IllegalArgumentException if <code>series</code> is not in the
108     *     range <code>0</code> to <code>getSeriesCount() - 1</code>.
109     */
110    public YIntervalSeries getSeries(int series) {
111        if ((series < 0) || (series >= getSeriesCount())) {
112            throw new IllegalArgumentException("Series index out of bounds");
113        }
114        return (YIntervalSeries) this.data.get(series);
115    }
116
117    /**
118     * Returns the key for a series.
119     *
120     * @param series  the series index (in the range <code>0</code> to
121     *     <code>getSeriesCount() - 1</code>).
122     *
123     * @return The key for a series.
124     *
125     * @throws IllegalArgumentException if <code>series</code> is not in the
126     *     specified range.
127     */
128    @Override
129    public Comparable getSeriesKey(int series) {
130        // defer argument checking
131        return getSeries(series).getKey();
132    }
133
134    /**
135     * Returns the number of items in the specified series.
136     *
137     * @param series  the series (zero-based index).
138     *
139     * @return The item count.
140     *
141     * @throws IllegalArgumentException if <code>series</code> is not in the
142     *     range <code>0</code> to <code>getSeriesCount() - 1</code>.
143     */
144    @Override
145    public int getItemCount(int series) {
146        // defer argument checking
147        return getSeries(series).getItemCount();
148    }
149
150    /**
151     * Returns the x-value for an item within a series.
152     *
153     * @param series  the series index.
154     * @param item  the item index.
155     *
156     * @return The x-value.
157     */
158    @Override
159    public Number getX(int series, int item) {
160        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
161        return s.getX(item);
162    }
163
164    /**
165     * Returns the y-value (as a double primitive) for an item within a
166     * series.
167     *
168     * @param series  the series index (zero-based).
169     * @param item  the item index (zero-based).
170     *
171     * @return The value.
172     */
173    @Override
174    public double getYValue(int series, int item) {
175        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
176        return s.getYValue(item);
177    }
178
179    /**
180     * Returns the start y-value (as a double primitive) for an item within a
181     * series.
182     *
183     * @param series  the series index (zero-based).
184     * @param item  the item index (zero-based).
185     *
186     * @return The value.
187     */
188    @Override
189    public double getStartYValue(int series, int item) {
190        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
191        return s.getYLowValue(item);
192    }
193
194    /**
195     * Returns the end y-value (as a double primitive) for an item within a
196     * series.
197     *
198     * @param series  the series (zero-based index).
199     * @param item  the item (zero-based index).
200     *
201     * @return The value.
202     */
203    @Override
204    public double getEndYValue(int series, int item) {
205        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
206        return s.getYHighValue(item);
207    }
208
209    /**
210     * Returns the y-value for an item within a series.
211     *
212     * @param series  the series index.
213     * @param item  the item index.
214     *
215     * @return The y-value.
216     */
217    @Override
218    public Number getY(int series, int item) {
219        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
220        return new Double(s.getYValue(item));
221    }
222
223    /**
224     * Returns the start x-value for an item within a series.  This method
225     * maps directly to {@link #getX(int, int)}.
226     *
227     * @param series  the series index.
228     * @param item  the item index.
229     *
230     * @return The x-value.
231     */
232    @Override
233    public Number getStartX(int series, int item) {
234        return getX(series, item);
235    }
236
237    /**
238     * Returns the end x-value for an item within a series.  This method
239     * maps directly to {@link #getX(int, int)}.
240     *
241     * @param series  the series index.
242     * @param item  the item index.
243     *
244     * @return The x-value.
245     */
246    @Override
247    public Number getEndX(int series, int item) {
248        return getX(series, item);
249    }
250
251    /**
252     * Returns the start y-value for an item within a series.
253     *
254     * @param series  the series index.
255     * @param item  the item index.
256     *
257     * @return The start y-value.
258     */
259    @Override
260    public Number getStartY(int series, int item) {
261        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
262        return new Double(s.getYLowValue(item));
263    }
264
265    /**
266     * Returns the end y-value for an item within a series.
267     *
268     * @param series  the series index.
269     * @param item  the item index.
270     *
271     * @return The end y-value.
272     */
273    @Override
274    public Number getEndY(int series, int item) {
275        YIntervalSeries s = (YIntervalSeries) this.data.get(series);
276        return new Double(s.getYHighValue(item));
277    }
278
279    /**
280     * Removes a series from the collection and sends a
281     * {@link DatasetChangeEvent} to all registered listeners.
282     *
283     * @param series  the series index (zero-based).
284     *
285     * @since 1.0.10
286     */
287    public void removeSeries(int series) {
288        if ((series < 0) || (series >= getSeriesCount())) {
289            throw new IllegalArgumentException("Series index out of bounds.");
290        }
291        YIntervalSeries ts = (YIntervalSeries) this.data.get(series);
292        ts.removeChangeListener(this);
293        this.data.remove(series);
294        fireDatasetChanged();
295    }
296
297    /**
298     * Removes a series from the collection and sends a
299     * {@link DatasetChangeEvent} to all registered listeners.
300     *
301     * @param series  the series (<code>null</code> not permitted).
302     *
303     * @since 1.0.10
304     */
305    public void removeSeries(YIntervalSeries series) {
306        ParamChecks.nullNotPermitted(series, "series");
307        if (this.data.contains(series)) {
308            series.removeChangeListener(this);
309            this.data.remove(series);
310            fireDatasetChanged();
311        }
312    }
313
314    /**
315     * Removes all the series from the collection and sends a
316     * {@link DatasetChangeEvent} to all registered listeners.
317     *
318     * @since 1.0.10
319     */
320    public void removeAllSeries() {
321        // Unregister the collection as a change listener to each series in
322        // the collection.
323        for (int i = 0; i < this.data.size(); i++) {
324          YIntervalSeries series = (YIntervalSeries) this.data.get(i);
325          series.removeChangeListener(this);
326        }
327        this.data.clear();
328        fireDatasetChanged();
329    }
330
331    /**
332     * Tests this instance for equality with an arbitrary object.
333     *
334     * @param obj  the object (<code>null</code> permitted).
335     *
336     * @return A boolean.
337     */
338    @Override
339    public boolean equals(Object obj) {
340        if (obj == this) {
341            return true;
342        }
343        if (!(obj instanceof YIntervalSeriesCollection)) {
344            return false;
345        }
346        YIntervalSeriesCollection that = (YIntervalSeriesCollection) obj;
347        return ObjectUtilities.equal(this.data, that.data);
348    }
349
350    /**
351     * Returns a clone of this instance.
352     *
353     * @return A clone.
354     *
355     * @throws CloneNotSupportedException if there is a problem.
356     */
357    @Override
358    public Object clone() throws CloneNotSupportedException {
359        YIntervalSeriesCollection clone
360                = (YIntervalSeriesCollection) super.clone();
361        clone.data = (List) ObjectUtilities.deepClone(this.data);
362        return clone;
363    }
364
365}