1 51 52 package org.jfree.data; 53 54 import java.io.Serializable ; 55 import java.util.Collections ; 56 import java.util.List ; 57 58 import org.jfree.util.ObjectUtils; 59 60 65 public class XYSeries extends Series implements Serializable { 66 67 68 protected List data; 69 70 74 75 private int maximumItemCount = Integer.MAX_VALUE; 76 77 78 private boolean allowDuplicateXValues; 79 80 87 public XYSeries(String name) { 88 this(name, true); 89 } 90 91 98 public XYSeries(String name, boolean allowDuplicateXValues) { 99 super(name); 100 this.allowDuplicateXValues = allowDuplicateXValues; 101 this.data = new java.util.ArrayList (); 102 } 103 104 109 public boolean getAllowDuplicateXValues() { 110 return this.allowDuplicateXValues; 111 } 112 113 118 public int getItemCount() { 119 return this.data.size(); 120 } 121 122 128 public List getItems() { 129 return Collections.unmodifiableList(this.data); 130 } 131 132 139 public int getMaximumItemCount() { 140 return this.maximumItemCount; 141 } 142 143 152 public void setMaximumItemCount(int maximum) { 153 this.maximumItemCount = maximum; 154 } 155 156 164 public void add(XYDataItem item) throws SeriesException { 165 add(item, true); 166 } 167 168 178 public void add(XYDataItem item, boolean notify) throws SeriesException { 179 180 if (item == null) { 182 throw new IllegalArgumentException ("XYSeries.add(...): null item not allowed."); 183 } 184 185 boolean changed = false; 187 int index = Collections.binarySearch(data, item); 188 if (index < 0) { 189 data.add(-index - 1, item); 190 191 if (getItemCount() > this.maximumItemCount) { 193 this.data.remove(0); 194 } 195 changed = true; 196 } 197 else { 198 if (allowDuplicateXValues == true) { 199 data.add(index, item); 200 if (getItemCount() > this.maximumItemCount) { 201 this.data.remove(0); 202 } 203 changed = true; 204 } 205 else { 206 throw new SeriesException("XYSeries.add(...): x-value already exists."); 207 } 208 } 209 210 if (notify && changed) { 211 fireSeriesChanged(); 212 } 213 214 } 215 216 224 public void add(double x, double y) throws SeriesException { 225 add(new Double (x), new Double (y), true); 226 } 227 228 238 public void add(double x, double y, boolean notify) throws SeriesException { 239 add(new Double (x), new Double (y), notify); 240 } 241 242 252 public void add(double x, Number y) throws SeriesException { 253 add(new Double (x), y); 254 } 255 256 268 public void add(double x, Number y, boolean notify) throws SeriesException { 269 add(new Double (x), y, notify); 270 } 271 272 283 public void add(Number x, Number y) throws SeriesException { 284 add(x, y, true); 285 } 286 287 300 public void add(Number x, Number y, boolean notify) throws SeriesException { 301 XYDataItem item = new XYDataItem(x, y); 302 add(item, notify); 303 } 304 305 311 public void delete(int start, int end) { 312 for (int i = start; i <= end; i++) { 313 data.remove(start); 314 } 315 fireSeriesChanged(); 316 } 317 318 321 public void clear() { 322 323 if (this.data.size() > 0) { 324 this.data.clear(); 325 fireSeriesChanged(); 326 } 327 328 } 329 330 338 public XYDataPair getDataPair(int index) { 339 return (XYDataPair) data.get(index); 340 } 341 342 349 public XYDataItem getDataItem(int index) { 350 return (XYDataItem) data.get(index); 351 } 352 353 360 public Number getXValue(int index) { 361 return getDataItem(index).getX(); 362 } 363 364 371 public Number getYValue(int index) { 372 return getDataItem(index).getY(); 373 } 374 375 381 public void update(int index, Number y) { 382 XYDataItem item = getDataItem(index); 383 item.setY(y); 384 fireSeriesChanged(); 385 } 386 387 392 public Object clone() { 393 394 Object clone = createCopy(0, getItemCount() - 1); 395 return clone; 396 397 } 398 399 407 public XYSeries createCopy(int start, int end) { 408 409 XYSeries copy = (XYSeries) super.clone(); 410 413 copy.data = new java.util.ArrayList (); 414 if (data.size() > 0) { 415 for (int index = start; index <= end; index++) { 416 XYDataItem item = (XYDataItem) this.data.get(index); 417 XYDataItem clone = (XYDataItem) item.clone(); 418 try { 419 copy.add(clone); 420 } 421 catch (SeriesException e) { 422 System.err.println("XYSeries.createCopy(): unable to add cloned data pair."); 423 } 424 } 425 } 426 427 return copy; 428 429 } 430 431 438 public boolean equals(Object obj) { 439 440 if (obj == null) { 441 return false; 442 } 443 444 if (obj == this) { 445 return true; 446 } 447 448 if (obj instanceof XYSeries) { 449 XYSeries s = (XYSeries) obj; 450 boolean b0 = ObjectUtils.equal(this.data, s.data); 451 boolean b1 = (this.maximumItemCount == s.maximumItemCount); 452 boolean b2 = (this.allowDuplicateXValues == s.allowDuplicateXValues); 453 return b0 && b1 && b2; 454 } 455 456 return false; 457 458 } 459 460 } 461 462 | Popular Tags |