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