1 44 45 package org.jfree.data.xy; 46 47 import java.util.ArrayList ; 48 import java.util.Arrays ; 49 import java.util.List ; 50 51 import org.jfree.data.DomainOrder; 52 import org.jfree.data.general.DatasetChangeEvent; 53 54 60 public class DefaultXYDataset extends AbstractXYDataset implements XYDataset { 61 62 66 private List seriesKeys; 67 68 73 private List seriesList; 74 75 79 public DefaultXYDataset() { 80 this.seriesKeys = new java.util.ArrayList (); 81 this.seriesList = new java.util.ArrayList (); 82 } 83 84 89 public int getSeriesCount() { 90 return this.seriesList.size(); 91 } 92 93 104 public Comparable getSeriesKey(int series) { 105 if ((series < 0) || (series >= getSeriesCount())) { 106 throw new IllegalArgumentException ("Series index out of bounds"); 107 } 108 return (Comparable ) this.seriesKeys.get(series); 109 } 110 111 119 public int indexOf(Comparable seriesKey) { 120 return this.seriesKeys.indexOf(seriesKey); 121 } 122 123 130 public DomainOrder getDomainOrder() { 131 return DomainOrder.NONE; 132 } 133 134 145 public int getItemCount(int series) { 146 if ((series < 0) || (series >= getSeriesCount())) { 147 throw new IllegalArgumentException ("Series index out of bounds"); 148 } 149 double[][] seriesArray = (double[][]) this.seriesList.get(series); 150 return seriesArray[0].length; 151 } 152 153 170 public double getXValue(int series, int item) { 171 double[][] seriesData = (double[][]) this.seriesList.get(series); 172 return seriesData[0][item]; 173 } 174 175 192 public Number getX(int series, int item) { 193 return new Double (getXValue(series, item)); 194 } 195 196 213 public double getYValue(int series, int item) { 214 double[][] seriesData = (double[][]) this.seriesList.get(series); 215 return seriesData[1][item]; 216 } 217 218 235 public Number getY(int series, int item) { 236 return new Double (getYValue(series, item)); 237 } 238 239 249 public void addSeries(Comparable seriesKey, double[][] data) { 250 if (seriesKey == null) { 251 throw new IllegalArgumentException ( 252 "The 'seriesKey' cannot be null."); 253 } 254 if (data == null) { 255 throw new IllegalArgumentException ("The 'data' is null."); 256 } 257 if (data.length != 2) { 258 throw new IllegalArgumentException ( 259 "The 'data' array must have length == 2."); 260 } 261 if (data[0].length != data[1].length) { 262 throw new IllegalArgumentException ( 263 "The 'data' array must contain two arrays with equal length."); 264 } 265 int seriesIndex = indexOf(seriesKey); 266 if (seriesIndex == -1) { this.seriesKeys.add(seriesKey); 268 this.seriesList.add(data); 269 } 270 else { this.seriesList.remove(seriesIndex); 272 this.seriesList.add(seriesIndex, data); 273 } 274 notifyListeners(new DatasetChangeEvent(this, this)); 275 } 276 277 284 public void removeSeries(Comparable seriesKey) { 285 int seriesIndex = indexOf(seriesKey); 286 if (seriesIndex >= 0) { 287 this.seriesKeys.remove(seriesIndex); 288 this.seriesList.remove(seriesIndex); 289 notifyListeners(new DatasetChangeEvent(this, this)); 290 } 291 } 292 293 308 public boolean equals(Object obj) { 309 if (obj == this) { 310 return true; 311 } 312 if (!(obj instanceof DefaultXYDataset)) { 313 return false; 314 } 315 DefaultXYDataset that = (DefaultXYDataset) obj; 316 if (!this.seriesKeys.equals(that.seriesKeys)) { 317 return false; 318 } 319 for (int i = 0; i < this.seriesList.size(); i++) { 320 double[][] d1 = (double[][]) this.seriesList.get(i); 321 double[][] d2 = (double[][]) that.seriesList.get(i); 322 double[] d1x = d1[0]; 323 double[] d2x = d2[0]; 324 if (!Arrays.equals(d1x, d2x)) { 325 return false; 326 } 327 double[] d1y = d1[1]; 328 double[] d2y = d2[1]; 329 if (!Arrays.equals(d1y, d2y)) { 330 return false; 331 } 332 } 333 return true; 334 } 335 336 345 public Object clone() throws CloneNotSupportedException { 346 DefaultXYDataset clone = (DefaultXYDataset) super.clone(); 347 clone.seriesKeys = new java.util.ArrayList (this.seriesKeys); 348 clone.seriesList = new ArrayList (this.seriesList.size()); 349 for (int i = 0; i < this.seriesList.size(); i++) { 350 double[][] data = (double[][]) this.seriesList.get(i); 351 double[] x = data[0]; 352 double[] y = data[1]; 353 double[] xx = new double[x.length]; 354 double[] yy = new double[y.length]; 355 System.arraycopy(x, 0, xx, 0, x.length); 356 System.arraycopy(y, 0, yy, 0, y.length); 357 clone.seriesList.add(i, new double[][] {xx, yy}); 358 } 359 return clone; 360 } 361 362 } 363 | Popular Tags |