KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > XYSeriesCollection


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------------
23  * XYSeriesCollection.java
24  * -----------------------
25  * (C) Copyright 2001-2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Aaron Metzger;
29  *
30  * $Id: XYSeriesCollection.java,v 1.6 2003/08/20 12:16:13 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 15-Nov-2001 : Version 1 (DG);
35  * 03-Apr-2002 : Added change listener code (DG);
36  * 29-Apr-2002 : Added removeSeries, removeAllSeries methods (ARM);
37  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  * 26-Mar-2003 : Implemented Serializable (DG);
39  * 04-Aug-2003 : Added getSeries() method (DG);
40  *
41  */

42
43 package org.jfree.data;
44
45 import java.io.Serializable JavaDoc;
46 import java.util.Collections JavaDoc;
47 import java.util.List JavaDoc;
48
49 import org.jfree.util.ObjectUtils;
50
51 /**
52  * Represents a collection of XY series that can be used as a dataset.
53  *
54  * @author David Gilbert
55  */

56 public class XYSeriesCollection extends AbstractSeriesDataset
57                                 implements XYDataset, Serializable JavaDoc {
58
59     /** The series that are included in the collection. */
60     private List JavaDoc data;
61
62     /**
63      * Constructs an empty dataset.
64      */

65     public XYSeriesCollection() {
66
67         this.data = new java.util.ArrayList JavaDoc();
68
69     }
70
71     /**
72      * Constructs a dataset and populates it with a single time series.
73      *
74      * @param series the time series.
75      */

76     public XYSeriesCollection(XYSeries series) {
77
78         this.data = new java.util.ArrayList JavaDoc();
79         if (series != null) {
80             data.add(series);
81             series.addChangeListener(this);
82         }
83
84     }
85
86     /**
87      * Adds a series to the collection.
88      * <P>
89      * Notifies all registered listeners that the dataset has changed.
90      *
91      * @param series the series.
92      */

93     public void addSeries(XYSeries series) {
94
95         // check arguments...
96
if (series == null) {
97             throw new IllegalArgumentException JavaDoc(
98                 "XYSeriesCollection.addSeries(...): cannot add null series.");
99         }
100
101         // add the series...
102
data.add(series);
103         series.addChangeListener(this);
104         fireDatasetChanged();
105
106     }
107
108     /**
109      * Returns the number of series in the collection.
110      *
111      * @return the number of series in the collection.
112      */

113     public int getSeriesCount() {
114         return this.data.size();
115     }
116
117     /**
118      * Returns a list of all the series in the collection.
119      *
120      * @return The list (which is unmodifiable).
121      */

122     public List JavaDoc getSeries() {
123         return Collections.unmodifiableList(this.data);
124     }
125
126     /**
127      * Returns a series.
128      *
129      * @param series the series (zero-based index).
130      *
131      * @return The series.
132      */

133     public XYSeries getSeries(int series) {
134
135         // check arguments...
136
if ((series < 0) || (series > getSeriesCount())) {
137             throw new IllegalArgumentException JavaDoc(
138                 "XYSeriesCollection.getSeries(...): index outside valid range.");
139         }
140
141         // fetch the series...
142
XYSeries ts = (XYSeries) data.get(series);
143         return ts;
144
145     }
146
147     /**
148      * Returns the name of a series.
149      *
150      * @param series the series (zero-based index).
151      *
152      * @return the name of a series.
153      */

154     public String JavaDoc getSeriesName(int series) {
155
156         // check arguments...delegated
157

158         // fetch the result...
159
return getSeries(series).getName();
160
161     }
162
163     /**
164      * Returns the number of items in the specified series.
165      *
166      * @param series the series (zero-based index).
167      *
168      * @return the number of items in the specified series.
169      */

170     public int getItemCount(int series) {
171
172         // check arguments...delegated
173

174         // fetch the result...
175
return getSeries(series).getItemCount();
176
177     }
178
179     /**
180      * Returns the x-value for the specified series and item.
181      *
182      * @param series the series (zero-based index).
183      * @param item the item (zero-based index).
184      *
185      * @return the x-value for the specified series and item.
186      */

187     public Number JavaDoc getXValue(int series, int item) {
188
189         XYSeries ts = (XYSeries) data.get(series);
190         XYDataItem xyItem = ts.getDataItem(item);
191         return xyItem.getX();
192
193     }
194
195     /**
196      * Returns the y-value for the specified series and item.
197      *
198      * @param series the series (zero-based index).
199      * @param index the index of the item of interest (zero-based).
200      *
201      * @return the y-value for the specified series and item.
202      */

203     public Number JavaDoc getYValue(int series, int index) {
204
205         XYSeries ts = (XYSeries) data.get(series);
206         XYDataItem xyItem = ts.getDataItem(index);
207         return xyItem.getY();
208
209     }
210
211     /**
212      * Removes all the series from the collection.
213      * <P>
214      * Notifies all registered listeners that the dataset has changed.
215      */

216     public void removeAllSeries() {
217
218
219         // Unregister the collection as a change listener to each series in the collection.
220
for (int i = 0; i < this.data.size(); i++) {
221           XYSeries series = (XYSeries) this.data.get(i);
222           series.removeChangeListener(this);
223         }
224
225         // Remove all the series from the collection and notify listeners.
226
data.clear();
227         fireDatasetChanged();
228
229     }
230
231     /**
232      * Removes a series from the collection.
233      * <P>
234      * Notifies all registered listeners that the dataset has changed.
235      *
236      * @param series the series.
237      */

238     public void removeSeries(XYSeries series) {
239
240         // check arguments...
241
if (series == null) {
242             throw new IllegalArgumentException JavaDoc(
243                 "XYSeriesCollection.removeSeries(...): cannot remove null series.");
244         }
245
246         // remove the series...
247
if (data.contains(series)) {
248             series.removeChangeListener(this);
249             data.remove(series);
250             fireDatasetChanged();
251         }
252
253     }
254
255     /**
256      * Removes a series from the collection.
257      * <P>
258      * Notifies all registered listeners that the dataset has changed.
259      *
260      * @param series the series (zero based index).
261      */

262     public void removeSeries(int series) {
263
264         // check arguments...
265
if ((series < 0) || (series > getSeriesCount())) {
266             throw new IllegalArgumentException JavaDoc(
267                 "XYSeriesCollection.removeSeries(...): index outside valid range.");
268         }
269
270         // fetch the series, remove the change listener, then remove the series.
271
XYSeries ts = (XYSeries) data.get(series);
272         ts.removeChangeListener(this);
273         data.remove(series);
274         fireDatasetChanged();
275
276     }
277
278     /**
279      * Tests this collection for equality with an arbitrary object.
280      *
281      * @param obj the object.
282      *
283      * @return A boolean.
284      */

285     public boolean equals(Object JavaDoc obj) {
286
287         if (obj == null) {
288             return false;
289         }
290
291         if (obj == this) {
292             return true;
293         }
294
295         if (obj instanceof XYSeriesCollection) {
296             XYSeriesCollection c = (XYSeriesCollection) obj;
297             return ObjectUtils.equal(this.data, c.data);
298         }
299
300         return false;
301
302     }
303
304 }
305
Popular Tags