1 42 43 package org.jfree.data; 44 45 import java.io.Serializable ; 46 import java.util.Collections ; 47 import java.util.List ; 48 49 import org.jfree.data.general.Series; 50 import org.jfree.data.general.SeriesChangeEvent; 51 import org.jfree.data.general.SeriesException; 52 import org.jfree.util.ObjectUtilities; 53 54 59 public class ComparableObjectSeries extends Series 60 implements Cloneable , Serializable { 61 62 63 protected List data; 64 65 66 private int maximumItemCount = Integer.MAX_VALUE; 67 68 69 private boolean autoSort; 70 71 72 private boolean allowDuplicateXValues; 73 74 81 public ComparableObjectSeries(Comparable key) { 82 this(key, true, true); 83 } 84 85 95 public ComparableObjectSeries(Comparable key, boolean autoSort, 96 boolean allowDuplicateXValues) { 97 super(key); 98 this.data = new java.util.ArrayList (); 99 this.autoSort = autoSort; 100 this.allowDuplicateXValues = allowDuplicateXValues; 101 } 102 103 110 public boolean getAutoSort() { 111 return this.autoSort; 112 } 113 114 120 public boolean getAllowDuplicateXValues() { 121 return this.allowDuplicateXValues; 122 } 123 124 129 public int getItemCount() { 130 return this.data.size(); 131 } 132 133 140 public int getMaximumItemCount() { 141 return this.maximumItemCount; 142 } 143 144 158 public void setMaximumItemCount(int maximum) { 159 this.maximumItemCount = maximum; 160 boolean dataRemoved = false; 161 while (this.data.size() > maximum) { 162 this.data.remove(0); 163 dataRemoved = true; 164 } 165 if (dataRemoved) { 166 fireSeriesChanged(); 167 } 168 } 169 170 180 protected void add(Comparable x, Object y) { 181 add(x, y, true); 183 } 184 185 198 protected void add(Comparable x, Object y, boolean notify) { 199 ComparableObjectItem item = new ComparableObjectItem(x, y); 201 add(item, notify); 202 } 203 204 213 protected void add(ComparableObjectItem item, boolean notify) { 214 215 if (item == null) { 216 throw new IllegalArgumentException ("Null 'item' argument."); 217 } 218 219 if (this.autoSort) { 220 int index = Collections.binarySearch(this.data, item); 221 if (index < 0) { 222 this.data.add(-index - 1, item); 223 } 224 else { 225 if (this.allowDuplicateXValues) { 226 int size = this.data.size(); 228 while (index < size 229 && item.compareTo(this.data.get(index)) == 0) { 230 index++; 231 } 232 if (index < this.data.size()) { 233 this.data.add(index, item); 234 } 235 else { 236 this.data.add(item); 237 } 238 } 239 else { 240 throw new SeriesException("X-value already exists."); 241 } 242 } 243 } 244 else { 245 if (!this.allowDuplicateXValues) { 246 int index = indexOf(item.getComparable()); 249 if (index >= 0) { 250 throw new SeriesException("X-value already exists."); 251 } 252 } 253 this.data.add(item); 254 } 255 if (getItemCount() > this.maximumItemCount) { 256 this.data.remove(0); 257 } 258 if (notify) { 259 fireSeriesChanged(); 260 } 261 } 262 263 273 public int indexOf(Comparable x) { 274 if (this.autoSort) { 275 return Collections.binarySearch(this.data, new ComparableObjectItem(x, null)); 276 } 277 else { 278 for (int i = 0; i < this.data.size(); i++) { 279 ComparableObjectItem item = (ComparableObjectItem) this.data.get(i); 280 if (item.getComparable().equals(x)) { 281 return i; 282 } 283 } 284 return -1; 285 } 286 } 287 288 297 protected void update(Comparable x, Object y) { 298 int index = indexOf(x); 299 if (index < 0) { 300 throw new SeriesException("No observation for x = " + x); 301 } 302 else { 303 ComparableObjectItem item = getDataItem(index); 304 item.setObject(y); 305 fireSeriesChanged(); 306 } 307 } 308 309 316 protected void updateByIndex(int index, Object y) { 317 ComparableObjectItem item = getDataItem(index); 318 item.setObject(y); 319 fireSeriesChanged(); 320 } 321 322 329 protected ComparableObjectItem getDataItem(int index) { 330 return (ComparableObjectItem) this.data.get(index); 331 } 332 333 340 protected void delete(int start, int end) { 341 for (int i = start; i <= end; i++) { 342 this.data.remove(start); 343 } 344 fireSeriesChanged(); 345 } 346 347 350 protected void clear() { 351 if (this.data.size() > 0) { 352 this.data.clear(); 353 fireSeriesChanged(); 354 } 355 } 356 357 365 protected ComparableObjectItem remove(int index) { 366 ComparableObjectItem result = (ComparableObjectItem) this.data.remove(index); 367 fireSeriesChanged(); 368 return result; 369 } 370 371 379 public ComparableObjectItem remove(Comparable x) { 380 return remove(indexOf(x)); 381 } 382 383 391 public boolean equals(Object obj) { 392 if (obj == this) { 393 return true; 394 } 395 if (!(obj instanceof ComparableObjectSeries)) { 396 return false; 397 } 398 if (!super.equals(obj)) { 399 return false; 400 } 401 ComparableObjectSeries that = (ComparableObjectSeries) obj; 402 if (this.maximumItemCount != that.maximumItemCount) { 403 return false; 404 } 405 if (this.autoSort != that.autoSort) { 406 return false; 407 } 408 if (this.allowDuplicateXValues != that.allowDuplicateXValues) { 409 return false; 410 } 411 if (!ObjectUtilities.equal(this.data, that.data)) { 412 return false; 413 } 414 return true; 415 } 416 417 422 public int hashCode() { 423 int result = super.hashCode(); 424 result = 29 * result + (this.data != null ? this.data.hashCode() : 0); 425 result = 29 * result + this.maximumItemCount; 426 result = 29 * result + (this.autoSort ? 1 : 0); 427 result = 29 * result + (this.allowDuplicateXValues ? 1 : 0); 428 return result; 429 } 430 431 } 432 | Popular Tags |