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