001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2006, 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 * XIntervalSeriesCollection.java
029 * ------------------------------
030 * (C) Copyright 2006, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * $Id: XIntervalSeriesCollection.java,v 1.1.2.3 2006/11/27 17:27:52 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 20-Oct-2006 : Version 1 (DG);
040 * 27-Nov-2006 : Added clone() override (DG);
041 *
042 */
043
044 package org.jfree.data.xy;
045
046 import java.io.Serializable;
047 import java.util.List;
048
049 import org.jfree.data.general.DatasetChangeEvent;
050 import org.jfree.util.ObjectUtilities;
051
052 /**
053 * A collection of {@link XIntervalSeries} objects.
054 *
055 * @since 1.0.3
056 *
057 * @see XIntervalSeries
058 */
059 public class XIntervalSeriesCollection extends AbstractIntervalXYDataset
060 implements IntervalXYDataset, Serializable {
061
062 /** Storage for the data series. */
063 private List data;
064
065 /**
066 * Creates a new instance of <code>XIntervalSeriesCollection</code>.
067 */
068 public XIntervalSeriesCollection() {
069 this.data = new java.util.ArrayList();
070 }
071
072 /**
073 * Adds a series to the collection and sends a {@link DatasetChangeEvent}
074 * to all registered listeners.
075 *
076 * @param series the series (<code>null</code> not permitted).
077 */
078 public void addSeries(XIntervalSeries series) {
079 if (series == null) {
080 throw new IllegalArgumentException("Null 'series' argument.");
081 }
082 this.data.add(series);
083 series.addChangeListener(this);
084 fireDatasetChanged();
085 }
086
087 /**
088 * Returns the number of series in the collection.
089 *
090 * @return The series count.
091 */
092 public int getSeriesCount() {
093 return this.data.size();
094 }
095
096 /**
097 * Returns a series from the collection.
098 *
099 * @param series the series index (zero-based).
100 *
101 * @return The series.
102 *
103 * @throws IllegalArgumentException if <code>series</code> is not in the
104 * range <code>0</code> to <code>getSeriesCount() - 1</code>.
105 */
106 public XIntervalSeries getSeries(int series) {
107 if ((series < 0) || (series >= getSeriesCount())) {
108 throw new IllegalArgumentException("Series index out of bounds");
109 }
110 return (XIntervalSeries) this.data.get(series);
111 }
112
113 /**
114 * Returns the key for a series.
115 *
116 * @param series the series index (in the range <code>0</code> to
117 * <code>getSeriesCount() - 1</code>).
118 *
119 * @return The key for a series.
120 *
121 * @throws IllegalArgumentException if <code>series</code> is not in the
122 * specified range.
123 */
124 public Comparable getSeriesKey(int series) {
125 // defer argument checking
126 return getSeries(series).getKey();
127 }
128
129 /**
130 * Returns the number of items in the specified series.
131 *
132 * @param series the series (zero-based index).
133 *
134 * @return The item count.
135 *
136 * @throws IllegalArgumentException if <code>series</code> is not in the
137 * range <code>0</code> to <code>getSeriesCount() - 1</code>.
138 */
139 public int getItemCount(int series) {
140 // defer argument checking
141 return getSeries(series).getItemCount();
142 }
143
144 /**
145 * Returns the x-value for an item within a series.
146 *
147 * @param series the series index.
148 * @param item the item index.
149 *
150 * @return The x-value.
151 */
152 public Number getX(int series, int item) {
153 XIntervalSeries s = (XIntervalSeries) this.data.get(series);
154 XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
155 return di.getX();
156 }
157
158 /**
159 * Returns the y-value for an item within a series.
160 *
161 * @param series the series index.
162 * @param item the item index.
163 *
164 * @return The y-value.
165 */
166 public Number getY(int series, int item) {
167 XIntervalSeries s = (XIntervalSeries) this.data.get(series);
168 XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
169 return new Double(di.getYValue());
170 }
171
172 /**
173 * Returns the start x-value for an item within a series.
174 *
175 * @param series the series index.
176 * @param item the item index.
177 *
178 * @return The x-value.
179 */
180 public Number getStartX(int series, int item) {
181 XIntervalSeries s = (XIntervalSeries) this.data.get(series);
182 XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
183 return new Double(di.getXLowValue());
184 }
185
186 /**
187 * Returns the end x-value for an item within a series.
188 *
189 * @param series the series index.
190 * @param item the item index.
191 *
192 * @return The x-value.
193 */
194 public Number getEndX(int series, int item) {
195 XIntervalSeries s = (XIntervalSeries) this.data.get(series);
196 XIntervalDataItem di = (XIntervalDataItem) s.getDataItem(item);
197 return new Double(di.getXHighValue());
198 }
199
200 /**
201 * Returns the start y-value for an item within a series. This method
202 * maps directly to {@link #getY(int, int)}.
203 *
204 * @param series the series index.
205 * @param item the item index.
206 *
207 * @return The start y-value.
208 */
209 public Number getStartY(int series, int item) {
210 return getY(series, item);
211 }
212
213 /**
214 * Returns the end y-value for an item within a series. This method
215 * maps directly to {@link #getY(int, int)}.
216 *
217 * @param series the series index.
218 * @param item the item index.
219 *
220 * @return The end y-value.
221 */
222 public Number getEndY(int series, int item) {
223 return getY(series, item);
224 }
225
226 /**
227 * Tests this instance for equality with an arbitrary object.
228 *
229 * @param obj the object (<code>null</code> permitted).
230 *
231 * @return A boolean.
232 */
233 public boolean equals(Object obj) {
234 if (obj == this) {
235 return true;
236 }
237 if (!(obj instanceof XIntervalSeriesCollection)) {
238 return false;
239 }
240 XIntervalSeriesCollection that = (XIntervalSeriesCollection) obj;
241 return ObjectUtilities.equal(this.data, that.data);
242 }
243
244 /**
245 * Returns a clone of this instance.
246 *
247 * @return A clone.
248 *
249 * @throws CloneNotSupportedException if there is a problem.
250 */
251 public Object clone() throws CloneNotSupportedException {
252 XIntervalSeriesCollection clone
253 = (XIntervalSeriesCollection) super.clone();
254 clone.data = (List) ObjectUtilities.deepClone(this.data);
255 return clone;
256 }
257
258 }