KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MatrixSeriesCollection.java
24  * ---------------------------
25  * (C) Copyright 2003 by Barak Naveh and Contributors.
26  *
27  * Original Author: Barak Naveh;;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: MatrixSeriesCollection.java,v 1.4 2003/09/03 15:08:51 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
35  *
36  */

37  
38 package org.jfree.data;
39
40 import java.io.Serializable JavaDoc;
41 import java.util.List JavaDoc;
42
43 import org.jfree.util.ObjectUtils;
44
45 /**
46  * Represents a collection of MatrixSeries that can be used as a dataset.
47  *
48  * @author Barak Naveh
49  *
50  * @see org.jfree.data.MatrixSeries
51  */

52 public class MatrixSeriesCollection extends AbstractSeriesDataset
53     implements XYZDataset, Serializable JavaDoc {
54     /** The series that are included in the collection. */
55     private List JavaDoc m_data;
56
57     /**
58      * Constructs an empty dataset.
59      */

60     public MatrixSeriesCollection() {
61         this(null);
62     }
63
64
65     /**
66      * Constructs a dataset and populates it with a single matrix series.
67      *
68      * @param series the time series.
69      */

70     public MatrixSeriesCollection(MatrixSeries series) {
71         this.m_data = new java.util.ArrayList JavaDoc();
72
73         if (series != null) {
74             m_data.add(series);
75             series.addChangeListener(this);
76         }
77     }
78
79     /**
80      * Returns the number of items in the specified series.
81      *
82      * @param seriesIndex zero-based series index.
83      *
84      * @return the number of items in the specified series.
85      */

86     public int getItemCount(int seriesIndex) {
87         return getSeries(seriesIndex).getItemCount();
88     }
89
90
91     /**
92      * Returns the series having the specified index.
93      *
94      * @param seriesIndex zero-based series index.
95      *
96      * @return The series.
97      *
98      * @throws IllegalArgumentException
99      */

100     public MatrixSeries getSeries(int seriesIndex) {
101         if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) {
102             throw new IllegalArgumentException JavaDoc(
103                 "MatrixSeriesCollection.getSeries(...): index outside valid range.");
104         }
105
106         MatrixSeries series = (MatrixSeries) m_data.get(seriesIndex);
107
108         return series;
109     }
110
111
112     /**
113      * Returns the number of series in the collection.
114      *
115      * @return the number of series in the collection.
116      */

117     public int getSeriesCount() {
118         return this.m_data.size();
119     }
120
121
122     /**
123      * Returns the name of a series.
124      *
125      * @param seriesIndex zero-based series index.
126      *
127      * @return the name of a series.
128      */

129     public String JavaDoc getSeriesName(int seriesIndex) {
130         return getSeries(seriesIndex).getName();
131     }
132
133
134     /**
135      * Returns the j index value of the specified Mij matrix item in the
136      * specified matrix series.
137      *
138      * @param seriesIndex zero-based series index.
139      * @param itemIndex zero-based item index.
140      *
141      * @return the j index value for the specified matrix item.
142      *
143      * @see org.jfree.data.XYDataset#getXValue(int, int)
144      */

145     public Number JavaDoc getXValue(int seriesIndex, int itemIndex) {
146         MatrixSeries series = (MatrixSeries) m_data.get(seriesIndex);
147         int x = series.getItemColumn(itemIndex);
148
149         return new Integer JavaDoc(x); // I know it's bad to create object. better idea?
150
}
151
152
153     /**
154      * Returns the i index value of the specified Mij matrix item in the
155      * specified matrix series.
156      *
157      * @param seriesIndex zero-based series index.
158      * @param itemIndex zero-based item index.
159      *
160      * @return the i index value for the specified matrix item.
161      *
162      * @see org.jfree.data.XYDataset#getYValue(int, int)
163      */

164     public Number JavaDoc getYValue(int seriesIndex, int itemIndex) {
165         MatrixSeries series = (MatrixSeries) m_data.get(seriesIndex);
166         int y = series.getItemRow(itemIndex);
167
168         return new Integer JavaDoc(y); // I know it's bad to create object. better idea?
169
}
170
171
172     /**
173      * Returns the Mij item value of the specified Mij matrix item in the
174      * specified matrix series.
175      *
176      * @param seriesIndex the series (zero-based index).
177      * @param itemIndex zero-based item index.
178      *
179      * @return the Mij item value for the specified matrix item.
180      *
181      * @see org.jfree.data.XYZDataset#getZValue(int, int)
182      */

183     public Number JavaDoc getZValue(int seriesIndex, int itemIndex) {
184         MatrixSeries series = (MatrixSeries) m_data.get(seriesIndex);
185         Number JavaDoc z = series.getItem(itemIndex);
186
187         return z;
188     }
189
190
191     /**
192      * Adds a series to the collection.
193      * <P>
194      * Notifies all registered listeners that the dataset has changed.
195      * </p>
196      *
197      * @param series the series.
198      *
199      * @throws IllegalArgumentException
200      */

201     public void addSeries(MatrixSeries series) {
202         // check arguments...
203
if (series == null) {
204             throw new IllegalArgumentException JavaDoc(
205                 "MatrixSeriesCollection.addSeries(...): cannot add null series.");
206         }
207
208         // add the series...
209
m_data.add(series);
210         series.addChangeListener(this);
211         fireDatasetChanged();
212     }
213
214
215     /**
216      * Tests this collection for equality with an arbitrary object.
217      *
218      * @param obj the object.
219      *
220      * @return A boolean.
221      */

222     public boolean equals(Object JavaDoc obj) {
223         if (obj == null) {
224             return false;
225         }
226
227         if (obj == this) {
228             return true;
229         }
230
231         if (obj instanceof MatrixSeriesCollection) {
232             MatrixSeriesCollection c = (MatrixSeriesCollection) obj;
233
234             return ObjectUtils.equal(m_data, c.m_data);
235         }
236
237         return false;
238     }
239
240
241     /**
242      * Removes all the series from the collection.
243      * <P>
244      * Notifies all registered listeners that the dataset has changed.
245      * </p>
246      */

247     public void removeAllSeries() {
248         // Unregister the collection as a change listener to each series inmthe collection.
249
for (int i = 0; i < this.m_data.size(); i++) {
250             MatrixSeries series = (MatrixSeries) m_data.get(i);
251             series.removeChangeListener(this);
252         }
253
254         // Remove all the series from the collection and notify listeners.
255
m_data.clear();
256         fireDatasetChanged();
257     }
258
259
260     /**
261      * Removes a series from the collection.
262      * <P>
263      * Notifies all registered listeners that the dataset has changed.
264      * </p>
265      *
266      * @param series the series.
267      *
268      * @throws IllegalArgumentException
269      */

270     public void removeSeries(MatrixSeries series) {
271         // check arguments...
272
if (series == null) {
273             throw new IllegalArgumentException JavaDoc(
274                 "MatrixSeriesCollection.removeSeries(...): cannot remove null series.");
275         }
276
277         // remove the series...
278
if (m_data.contains(series)) {
279             series.removeChangeListener(this);
280             m_data.remove(series);
281             fireDatasetChanged();
282         }
283     }
284
285
286     /**
287      * Removes a series from the collection.
288      * <P>
289      * Notifies all registered listeners that the dataset has changed.
290      * </p>
291      *
292      * @param seriesIndex the series (zero based index).
293      *
294      * @throws IllegalArgumentException
295      */

296     public void removeSeries(int seriesIndex) {
297         // check arguments...
298
if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) {
299             throw new IllegalArgumentException JavaDoc(
300                 "MatrixSeriesCollection.removeSeries(...): index outside valid range.");
301         }
302
303         // fetch the series, remove the change listener, then remove the series.
304
MatrixSeries series = (MatrixSeries) m_data.get(seriesIndex);
305         series.removeChangeListener(this);
306         m_data.remove(seriesIndex);
307         fireDatasetChanged();
308     }
309     
310 }
311
Popular Tags