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     * YIntervalSeriesCollection.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: YIntervalSeriesCollection.java,v 1.1.2.5 2007/02/20 16:22:01 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 20-Oct-2006 : Version 1 (DG);
040     * 27-Nov-2006 : Added clone() override (DG);
041     * 20-Feb-2007 : Added getYValue(), getStartYValue() and getEndYValue() 
042     *               methods (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 YIntervalSeries} objects.
056     *
057     * @since 1.0.3
058     *
059     * @see YIntervalSeries
060     */
061    public class YIntervalSeriesCollection 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>YIntervalSeriesCollection</code>. 
069         */
070        public YIntervalSeriesCollection() {
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(YIntervalSeries 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 YIntervalSeries getSeries(int series) {
109            if ((series < 0) || (series >= getSeriesCount())) {
110                throw new IllegalArgumentException("Series index out of bounds");
111            }
112            return (YIntervalSeries) 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            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
156            return s.getX(item);
157        }
158    
159        /**
160         * Returns the y-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 getYValue(int series, int item) {
169            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
170            return s.getYValue(item);
171        }
172    
173        /**
174         * Returns the start y-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 getStartYValue(int series, int item) {
183            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
184            return s.getYLowValue(item);
185        }
186    
187        /**
188         * Returns the end y-value (as a double primitive) for an item within a 
189         * series.
190         * 
191         * @param series  the series (zero-based index).
192         * @param item  the item (zero-based index).
193         * 
194         * @return The value.
195         */
196        public double getEndYValue(int series, int item) {
197            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
198            return s.getYHighValue(item);
199        }
200    
201        /**
202         * Returns the y-value for an item within a series.
203         *
204         * @param series  the series index.
205         * @param item  the item index.
206         *
207         * @return The y-value.
208         */
209        public Number getY(int series, int item) {
210            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
211            return new Double(s.getYValue(item));
212        }
213    
214        /**
215         * Returns the start x-value for an item within a series.  This method
216         * maps directly to {@link #getX(int, int)}.
217         *
218         * @param series  the series index.
219         * @param item  the item index.
220         *
221         * @return The x-value.
222         */
223        public Number getStartX(int series, int item) {
224            return getX(series, item);
225        }
226    
227        /**
228         * Returns the end x-value for an item within a series.  This method
229         * maps directly to {@link #getX(int, int)}.
230         *
231         * @param series  the series index.
232         * @param item  the item index.
233         *
234         * @return The x-value.
235         */
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        public Number getStartY(int series, int item) {
249            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
250            return new Double(s.getYLowValue(item));
251        }
252    
253        /**
254         * Returns the end y-value for an item within a series.
255         *
256         * @param series  the series index.
257         * @param item  the item index.
258         *
259         * @return The end y-value.
260         */
261        public Number getEndY(int series, int item) {
262            YIntervalSeries s = (YIntervalSeries) this.data.get(series);
263            return new Double(s.getYHighValue(item));
264        }
265        
266        /**
267         * Tests this instance for equality with an arbitrary object.
268         *
269         * @param obj  the object (<code>null</code> permitted).
270         *
271         * @return A boolean. 
272         */
273        public boolean equals(Object obj) {
274            if (obj == this) {
275                return true;
276            }
277            if (!(obj instanceof YIntervalSeriesCollection)) {
278                return false;
279            }
280            YIntervalSeriesCollection that = (YIntervalSeriesCollection) obj;
281            return ObjectUtilities.equal(this.data, that.data);
282        }
283        
284        /**
285         * Returns a clone of this instance.
286         * 
287         * @return A clone.
288         * 
289         * @throws CloneNotSupportedException if there is a problem.
290         */
291        public Object clone() throws CloneNotSupportedException {
292            YIntervalSeriesCollection clone 
293                    = (YIntervalSeriesCollection) super.clone();
294            clone.data = (List) ObjectUtilities.deepClone(this.data);
295            return clone;
296        }
297        
298    }