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: * MatrixSeriesCollection.java
029: * ---------------------------
030: * (C) Copyright 2003-2007, by Barak Naveh and Contributors.
031: *
032: * Original Author: Barak Naveh;;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: MatrixSeriesCollection.java,v 1.7.2.3 2007/02/02 15:14:53 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
040: * 05-May-2004 : Now extends AbstractXYZDataset (DG);
041: * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
042: * ------------- JFREECHART 1.0.x ---------------------------------------------
043: * 27-Nov-2006 : Added clone() override (DG);
044: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
045: *
046: */
047:
048: package org.jfree.data.xy;
049:
050: import java.io.Serializable;
051: import java.util.List;
052:
053: import org.jfree.util.ObjectUtilities;
054:
055: /**
056: * Represents a collection of {@link MatrixSeries} that can be used as a
057: * dataset.
058: *
059: * @see org.jfree.data.xy.MatrixSeries
060: */
061: public class MatrixSeriesCollection extends AbstractXYZDataset
062: implements XYZDataset, Serializable {
063:
064: /** For serialization. */
065: private static final long serialVersionUID = -3197705779242543945L;
066:
067: /** The series that are included in the collection. */
068: private List seriesList;
069:
070: /**
071: * Constructs an empty dataset.
072: */
073: public MatrixSeriesCollection() {
074: this (null);
075: }
076:
077: /**
078: * Constructs a dataset and populates it with a single matrix series.
079: *
080: * @param series the time series.
081: */
082: public MatrixSeriesCollection(MatrixSeries series) {
083: this .seriesList = new java.util.ArrayList();
084:
085: if (series != null) {
086: this .seriesList.add(series);
087: series.addChangeListener(this );
088: }
089: }
090:
091: /**
092: * Returns the number of items in the specified series.
093: *
094: * @param seriesIndex zero-based series index.
095: *
096: * @return The number of items in the specified series.
097: */
098: public int getItemCount(int seriesIndex) {
099: return getSeries(seriesIndex).getItemCount();
100: }
101:
102: /**
103: * Returns the series having the specified index.
104: *
105: * @param seriesIndex zero-based series index.
106: *
107: * @return The series.
108: *
109: * @throws IllegalArgumentException
110: */
111: public MatrixSeries getSeries(int seriesIndex) {
112: if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) {
113: throw new IllegalArgumentException(
114: "Index outside valid range.");
115: }
116:
117: MatrixSeries series = (MatrixSeries) this .seriesList
118: .get(seriesIndex);
119:
120: return series;
121: }
122:
123: /**
124: * Returns the number of series in the collection.
125: *
126: * @return The number of series in the collection.
127: */
128: public int getSeriesCount() {
129: return this .seriesList.size();
130: }
131:
132: /**
133: * Returns the key for a series.
134: *
135: * @param seriesIndex zero-based series index.
136: *
137: * @return The key for a series.
138: */
139: public Comparable getSeriesKey(int seriesIndex) {
140: return getSeries(seriesIndex).getKey();
141: }
142:
143: /**
144: * Returns the j index value of the specified Mij matrix item in the
145: * specified matrix series.
146: *
147: * @param seriesIndex zero-based series index.
148: * @param itemIndex zero-based item index.
149: *
150: * @return The j index value for the specified matrix item.
151: *
152: * @see org.jfree.data.xy.XYDataset#getXValue(int, int)
153: */
154: public Number getX(int seriesIndex, int itemIndex) {
155: MatrixSeries series = (MatrixSeries) this .seriesList
156: .get(seriesIndex);
157: int x = series.getItemColumn(itemIndex);
158:
159: return new Integer(x); // I know it's bad to create object. better idea?
160: }
161:
162: /**
163: * Returns the i index value of the specified Mij matrix item in the
164: * specified matrix series.
165: *
166: * @param seriesIndex zero-based series index.
167: * @param itemIndex zero-based item index.
168: *
169: * @return The i index value for the specified matrix item.
170: *
171: * @see org.jfree.data.xy.XYDataset#getYValue(int, int)
172: */
173: public Number getY(int seriesIndex, int itemIndex) {
174: MatrixSeries series = (MatrixSeries) this .seriesList
175: .get(seriesIndex);
176: int y = series.getItemRow(itemIndex);
177:
178: return new Integer(y); // I know it's bad to create object. better idea?
179: }
180:
181: /**
182: * Returns the Mij item value of the specified Mij matrix item in the
183: * specified matrix series.
184: *
185: * @param seriesIndex the series (zero-based index).
186: * @param itemIndex zero-based item index.
187: *
188: * @return The Mij item value for the specified matrix item.
189: *
190: * @see org.jfree.data.xy.XYZDataset#getZValue(int, int)
191: */
192: public Number getZ(int seriesIndex, int itemIndex) {
193: MatrixSeries series = (MatrixSeries) this .seriesList
194: .get(seriesIndex);
195: Number z = series.getItem(itemIndex);
196: return z;
197: }
198:
199: /**
200: * Adds a series to the collection.
201: * <P>
202: * Notifies all registered listeners that the dataset has changed.
203: * </p>
204: *
205: * @param series the series.
206: *
207: * @throws IllegalArgumentException
208: */
209: public void addSeries(MatrixSeries series) {
210: // check arguments...
211: if (series == null) {
212: throw new IllegalArgumentException(
213: "Cannot add null series.");
214: }
215: // FIXME: Check that there isn't already a series with the same key
216:
217: // add the series...
218: this .seriesList.add(series);
219: series.addChangeListener(this );
220: fireDatasetChanged();
221: }
222:
223: /**
224: * Tests this collection for equality with an arbitrary object.
225: *
226: * @param obj the object.
227: *
228: * @return A boolean.
229: */
230: public boolean equals(Object obj) {
231: if (obj == null) {
232: return false;
233: }
234:
235: if (obj == this ) {
236: return true;
237: }
238:
239: if (obj instanceof MatrixSeriesCollection) {
240: MatrixSeriesCollection c = (MatrixSeriesCollection) obj;
241:
242: return ObjectUtilities.equal(this .seriesList, c.seriesList);
243: }
244:
245: return false;
246: }
247:
248: /**
249: * Returns a hash code.
250: *
251: * @return A hash code.
252: */
253: public int hashCode() {
254: return (this .seriesList != null ? this .seriesList.hashCode()
255: : 0);
256: }
257:
258: /**
259: * Returns a clone of this instance.
260: *
261: * @return A clone.
262: *
263: * @throws CloneNotSupportedException if there is a problem.
264: */
265: public Object clone() throws CloneNotSupportedException {
266: MatrixSeriesCollection clone = (MatrixSeriesCollection) super
267: .clone();
268: clone.seriesList = (List) ObjectUtilities
269: .deepClone(this .seriesList);
270: return clone;
271: }
272:
273: /**
274: * Removes all the series from the collection.
275: * <P>
276: * Notifies all registered listeners that the dataset has changed.
277: * </p>
278: */
279: public void removeAllSeries() {
280: // Unregister the collection as a change listener to each series in
281: // the collection.
282: for (int i = 0; i < this .seriesList.size(); i++) {
283: MatrixSeries series = (MatrixSeries) this .seriesList.get(i);
284: series.removeChangeListener(this );
285: }
286:
287: // Remove all the series from the collection and notify listeners.
288: this .seriesList.clear();
289: fireDatasetChanged();
290: }
291:
292: /**
293: * Removes a series from the collection.
294: * <P>
295: * Notifies all registered listeners that the dataset has changed.
296: * </p>
297: *
298: * @param series the series.
299: *
300: * @throws IllegalArgumentException
301: */
302: public void removeSeries(MatrixSeries series) {
303: // check arguments...
304: if (series == null) {
305: throw new IllegalArgumentException(
306: "Cannot remove null series.");
307: }
308:
309: // remove the series...
310: if (this .seriesList.contains(series)) {
311: series.removeChangeListener(this );
312: this .seriesList.remove(series);
313: fireDatasetChanged();
314: }
315: }
316:
317: /**
318: * Removes a series from the collection.
319: * <P>
320: * Notifies all registered listeners that the dataset has changed.
321: *
322: * @param seriesIndex the series (zero based index).
323: *
324: * @throws IllegalArgumentException
325: */
326: public void removeSeries(int seriesIndex) {
327: // check arguments...
328: if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) {
329: throw new IllegalArgumentException(
330: "Index outside valid range.");
331: }
332:
333: // fetch the series, remove the change listener, then remove the series.
334: MatrixSeries series = (MatrixSeries) this.seriesList
335: .get(seriesIndex);
336: series.removeChangeListener(this);
337: this.seriesList.remove(seriesIndex);
338: fireDatasetChanged();
339: }
340:
341: }
|