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