001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * XYSeries.java
029 * -------------
030 * (C) Copyright 2001-2008, Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Aaron Metzger;
034 * Jonathan Gabbai;
035 * Richard Atkinson;
036 * Michel Santos;
037 * Ted Schwartz (fix for bug 1955483);
038 *
039 * Changes
040 * -------
041 * 15-Nov-2001 : Version 1 (DG);
042 * 03-Apr-2002 : Added an add(double, double) method (DG);
043 * 29-Apr-2002 : Added a clear() method (ARM);
044 * 06-Jun-2002 : Updated Javadoc comments (DG);
045 * 29-Aug-2002 : Modified to give user control over whether or not duplicate
046 * x-values are allowed (DG);
047 * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
048 * 11-Nov-2002 : Added maximum item count, code contributed by Jonathan
049 * Gabbai (DG);
050 * 26-Mar-2003 : Implemented Serializable (DG);
051 * 04-Aug-2003 : Added getItems() method (DG);
052 * 15-Aug-2003 : Changed 'data' from private to protected, added new add()
053 * methods with a 'notify' argument (DG);
054 * 22-Sep-2003 : Added getAllowDuplicateXValues() method (RA);
055 * 29-Jan-2004 : Added autoSort attribute, based on a contribution by
056 * Michel Santos - see patch 886740 (DG);
057 * 03-Feb-2004 : Added indexOf() method (DG);
058 * 16-Feb-2004 : Added remove() method (DG);
059 * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.xy (DG);
060 * 21-Feb-2005 : Added update(Number, Number) and addOrUpdate(Number, Number)
061 * methods (DG);
062 * 03-May-2005 : Added a new constructor, fixed the setMaximumItemCount()
063 * method to remove items (and notify listeners) if necessary,
064 * fixed the add() and addOrUpdate() methods to handle unsorted
065 * series (DG);
066 * ------------- JFreeChart 1.0.x ---------------------------------------------
067 * 11-Jan-2005 : Renamed update(int, Number) --> updateByIndex() (DG);
068 * 15-Jan-2007 : Added toArray() method (DG);
069 * 31-Oct-2007 : Implemented faster hashCode() (DG);
070 * 22-Nov-2007 : Reimplemented clone() (DG);
071 * 01-May-2008 : Fixed bug 1955483 in addOrUpdate() method, thanks to
072 * Ted Schwartz (DG);
073 * 24-Nov-2008 : Further fix for 1955483 (DG);
074 *
075 */
076
077 package org.jfree.data.xy;
078
079 import java.io.Serializable;
080 import java.util.Collections;
081 import java.util.List;
082
083 import org.jfree.data.general.Series;
084 import org.jfree.data.general.SeriesChangeEvent;
085 import org.jfree.data.general.SeriesException;
086 import org.jfree.util.ObjectUtilities;
087
088 /**
089 * Represents a sequence of zero or more data items in the form (x, y). By
090 * default, items in the series will be sorted into ascending order by x-value,
091 * and duplicate x-values are permitted. Both the sorting and duplicate
092 * defaults can be changed in the constructor. Y-values can be
093 * <code>null</code> to represent missing values.
094 */
095 public class XYSeries extends Series implements Cloneable, Serializable {
096
097 /** For serialization. */
098 static final long serialVersionUID = -5908509288197150436L;
099
100 // In version 0.9.12, in response to several developer requests, I changed
101 // the 'data' attribute from 'private' to 'protected', so that others can
102 // make subclasses that work directly with the underlying data structure.
103
104 /** Storage for the data items in the series. */
105 protected List data;
106
107 /** The maximum number of items for the series. */
108 private int maximumItemCount = Integer.MAX_VALUE;
109
110 /** A flag that controls whether the items are automatically sorted. */
111 private boolean autoSort;
112
113 /** A flag that controls whether or not duplicate x-values are allowed. */
114 private boolean allowDuplicateXValues;
115
116 /**
117 * Creates a new empty series. By default, items added to the series will
118 * be sorted into ascending order by x-value, and duplicate x-values will
119 * be allowed (these defaults can be modified with another constructor.
120 *
121 * @param key the series key (<code>null</code> not permitted).
122 */
123 public XYSeries(Comparable key) {
124 this(key, true, true);
125 }
126
127 /**
128 * Constructs a new empty series, with the auto-sort flag set as requested,
129 * and duplicate values allowed.
130 *
131 * @param key the series key (<code>null</code> not permitted).
132 * @param autoSort a flag that controls whether or not the items in the
133 * series are sorted.
134 */
135 public XYSeries(Comparable key, boolean autoSort) {
136 this(key, autoSort, true);
137 }
138
139 /**
140 * Constructs a new xy-series that contains no data. You can specify
141 * whether or not duplicate x-values are allowed for the series.
142 *
143 * @param key the series key (<code>null</code> not permitted).
144 * @param autoSort a flag that controls whether or not the items in the
145 * series are sorted.
146 * @param allowDuplicateXValues a flag that controls whether duplicate
147 * x-values are allowed.
148 */
149 public XYSeries(Comparable key,
150 boolean autoSort,
151 boolean allowDuplicateXValues) {
152 super(key);
153 this.data = new java.util.ArrayList();
154 this.autoSort = autoSort;
155 this.allowDuplicateXValues = allowDuplicateXValues;
156 }
157
158 /**
159 * Returns the flag that controls whether the items in the series are
160 * automatically sorted. There is no setter for this flag, it must be
161 * defined in the series constructor.
162 *
163 * @return A boolean.
164 */
165 public boolean getAutoSort() {
166 return this.autoSort;
167 }
168
169 /**
170 * Returns a flag that controls whether duplicate x-values are allowed.
171 * This flag can only be set in the constructor.
172 *
173 * @return A boolean.
174 */
175 public boolean getAllowDuplicateXValues() {
176 return this.allowDuplicateXValues;
177 }
178
179 /**
180 * Returns the number of items in the series.
181 *
182 * @return The item count.
183 */
184 public int getItemCount() {
185 return this.data.size();
186 }
187
188 /**
189 * Returns the list of data items for the series (the list contains
190 * {@link XYDataItem} objects and is unmodifiable).
191 *
192 * @return The list of data items.
193 */
194 public List getItems() {
195 return Collections.unmodifiableList(this.data);
196 }
197
198 /**
199 * Returns the maximum number of items that will be retained in the series.
200 * The default value is <code>Integer.MAX_VALUE</code>.
201 *
202 * @return The maximum item count.
203 * @see #setMaximumItemCount(int)
204 */
205 public int getMaximumItemCount() {
206 return this.maximumItemCount;
207 }
208
209 /**
210 * Sets the maximum number of items that will be retained in the series.
211 * If you add a new item to the series such that the number of items will
212 * exceed the maximum item count, then the first element in the series is
213 * automatically removed, ensuring that the maximum item count is not
214 * exceeded.
215 * <p>
216 * Typically this value is set before the series is populated with data,
217 * but if it is applied later, it may cause some items to be removed from
218 * the series (in which case a {@link SeriesChangeEvent} will be sent to
219 * all registered listeners.
220 *
221 * @param maximum the maximum number of items for the series.
222 */
223 public void setMaximumItemCount(int maximum) {
224 this.maximumItemCount = maximum;
225 boolean dataRemoved = false;
226 while (this.data.size() > maximum) {
227 this.data.remove(0);
228 dataRemoved = true;
229 }
230 if (dataRemoved) {
231 fireSeriesChanged();
232 }
233 }
234
235 /**
236 * Adds a data item to the series and sends a {@link SeriesChangeEvent} to
237 * all registered listeners.
238 *
239 * @param item the (x, y) item (<code>null</code> not permitted).
240 */
241 public void add(XYDataItem item) {
242 // argument checking delegated...
243 add(item, true);
244 }
245
246 /**
247 * Adds a data item to the series and sends a {@link SeriesChangeEvent} to
248 * all registered listeners.
249 *
250 * @param x the x value.
251 * @param y the y value.
252 */
253 public void add(double x, double y) {
254 add(new Double(x), new Double(y), true);
255 }
256
257 /**
258 * Adds a data item to the series and, if requested, sends a
259 * {@link SeriesChangeEvent} to all registered listeners.
260 *
261 * @param x the x value.
262 * @param y the y value.
263 * @param notify a flag that controls whether or not a
264 * {@link SeriesChangeEvent} is sent to all registered
265 * listeners.
266 */
267 public void add(double x, double y, boolean notify) {
268 add(new Double(x), new Double(y), notify);
269 }
270
271 /**
272 * Adds a data item to the series and sends a {@link SeriesChangeEvent} to
273 * all registered listeners. The unusual pairing of parameter types is to
274 * make it easier to add <code>null</code> y-values.
275 *
276 * @param x the x value.
277 * @param y the y value (<code>null</code> permitted).
278 */
279 public void add(double x, Number y) {
280 add(new Double(x), y);
281 }
282
283 /**
284 * Adds a data item to the series and, if requested, sends a
285 * {@link SeriesChangeEvent} to all registered listeners. The unusual
286 * pairing of parameter types is to make it easier to add null y-values.
287 *
288 * @param x the x value.
289 * @param y the y value (<code>null</code> permitted).
290 * @param notify a flag that controls whether or not a
291 * {@link SeriesChangeEvent} is sent to all registered
292 * listeners.
293 */
294 public void add(double x, Number y, boolean notify) {
295 add(new Double(x), y, notify);
296 }
297
298 /**
299 * Adds a new data item to the series (in the correct position if the
300 * <code>autoSort</code> flag is set for the series) and sends a
301 * {@link SeriesChangeEvent} to all registered listeners.
302 * <P>
303 * Throws an exception if the x-value is a duplicate AND the
304 * allowDuplicateXValues flag is false.
305 *
306 * @param x the x-value (<code>null</code> not permitted).
307 * @param y the y-value (<code>null</code> permitted).
308 *
309 * @throws SeriesException if the x-value is a duplicate and the
310 * <code>allowDuplicateXValues</code> flag is not set for this series.
311 */
312 public void add(Number x, Number y) {
313 // argument checking delegated...
314 add(x, y, true);
315 }
316
317 /**
318 * Adds new data to the series and, if requested, sends a
319 * {@link SeriesChangeEvent} to all registered listeners.
320 * <P>
321 * Throws an exception if the x-value is a duplicate AND the
322 * allowDuplicateXValues flag is false.
323 *
324 * @param x the x-value (<code>null</code> not permitted).
325 * @param y the y-value (<code>null</code> permitted).
326 * @param notify a flag the controls whether or not a
327 * {@link SeriesChangeEvent} is sent to all registered
328 * listeners.
329 */
330 public void add(Number x, Number y, boolean notify) {
331 // delegate argument checking to XYDataItem...
332 XYDataItem item = new XYDataItem(x, y);
333 add(item, notify);
334 }
335
336 /**
337 * Adds a data item to the series and, if requested, sends a
338 * {@link SeriesChangeEvent} to all registered listeners.
339 *
340 * @param item the (x, y) item (<code>null</code> not permitted).
341 * @param notify a flag that controls whether or not a
342 * {@link SeriesChangeEvent} is sent to all registered
343 * listeners.
344 */
345 public void add(XYDataItem item, boolean notify) {
346
347 if (item == null) {
348 throw new IllegalArgumentException("Null 'item' argument.");
349 }
350
351 if (this.autoSort) {
352 int index = Collections.binarySearch(this.data, item);
353 if (index < 0) {
354 this.data.add(-index - 1, item);
355 }
356 else {
357 if (this.allowDuplicateXValues) {
358 // need to make sure we are adding *after* any duplicates
359 int size = this.data.size();
360 while (index < size
361 && item.compareTo(this.data.get(index)) == 0) {
362 index++;
363 }
364 if (index < this.data.size()) {
365 this.data.add(index, item);
366 }
367 else {
368 this.data.add(item);
369 }
370 }
371 else {
372 throw new SeriesException("X-value already exists.");
373 }
374 }
375 }
376 else {
377 if (!this.allowDuplicateXValues) {
378 // can't allow duplicate values, so we need to check whether
379 // there is an item with the given x-value already
380 int index = indexOf(item.getX());
381 if (index >= 0) {
382 throw new SeriesException("X-value already exists.");
383 }
384 }
385 this.data.add(item);
386 }
387 if (getItemCount() > this.maximumItemCount) {
388 this.data.remove(0);
389 }
390 if (notify) {
391 fireSeriesChanged();
392 }
393 }
394
395 /**
396 * Deletes a range of items from the series and sends a
397 * {@link SeriesChangeEvent} to all registered listeners.
398 *
399 * @param start the start index (zero-based).
400 * @param end the end index (zero-based).
401 */
402 public void delete(int start, int end) {
403 for (int i = start; i <= end; i++) {
404 this.data.remove(start);
405 }
406 fireSeriesChanged();
407 }
408
409 /**
410 * Removes the item at the specified index and sends a
411 * {@link SeriesChangeEvent} to all registered listeners.
412 *
413 * @param index the index.
414 *
415 * @return The item removed.
416 */
417 public XYDataItem remove(int index) {
418 XYDataItem result = (XYDataItem) this.data.remove(index);
419 fireSeriesChanged();
420 return result;
421 }
422
423 /**
424 * Removes the item with the specified x-value and sends a
425 * {@link SeriesChangeEvent} to all registered listeners.
426 *
427 * @param x the x-value.
428
429 * @return The item removed.
430 */
431 public XYDataItem remove(Number x) {
432 return remove(indexOf(x));
433 }
434
435 /**
436 * Removes all data items from the series.
437 */
438 public void clear() {
439 if (this.data.size() > 0) {
440 this.data.clear();
441 fireSeriesChanged();
442 }
443 }
444
445 /**
446 * Return the data item with the specified index.
447 *
448 * @param index the index.
449 *
450 * @return The data item with the specified index.
451 */
452 public XYDataItem getDataItem(int index) {
453 return (XYDataItem) this.data.get(index);
454 }
455
456 /**
457 * Returns the x-value at the specified index.
458 *
459 * @param index the index (zero-based).
460 *
461 * @return The x-value (never <code>null</code>).
462 */
463 public Number getX(int index) {
464 return getDataItem(index).getX();
465 }
466
467 /**
468 * Returns the y-value at the specified index.
469 *
470 * @param index the index (zero-based).
471 *
472 * @return The y-value (possibly <code>null</code>).
473 */
474 public Number getY(int index) {
475 return getDataItem(index).getY();
476 }
477
478 /**
479 * Updates the value of an item in the series and sends a
480 * {@link SeriesChangeEvent} to all registered listeners.
481 *
482 * @param index the item (zero based index).
483 * @param y the new value (<code>null</code> permitted).
484 *
485 * @deprecated Renamed {@link #updateByIndex(int, Number)} to avoid
486 * confusion with the {@link #update(Number, Number)} method.
487 */
488 public void update(int index, Number y) {
489 XYDataItem item = getDataItem(index);
490 item.setY(y);
491 fireSeriesChanged();
492 }
493
494 /**
495 * Updates the value of an item in the series and sends a
496 * {@link SeriesChangeEvent} to all registered listeners.
497 *
498 * @param index the item (zero based index).
499 * @param y the new value (<code>null</code> permitted).
500 *
501 * @since 1.0.1
502 */
503 public void updateByIndex(int index, Number y) {
504 update(index, y);
505 }
506
507 /**
508 * Updates an item in the series.
509 *
510 * @param x the x-value (<code>null</code> not permitted).
511 * @param y the y-value (<code>null</code> permitted).
512 *
513 * @throws SeriesException if there is no existing item with the specified
514 * x-value.
515 */
516 public void update(Number x, Number y) {
517 int index = indexOf(x);
518 if (index < 0) {
519 throw new SeriesException("No observation for x = " + x);
520 }
521 else {
522 XYDataItem item = getDataItem(index);
523 item.setY(y);
524 fireSeriesChanged();
525 }
526 }
527
528 /**
529 * Adds or updates an item in the series and sends a
530 * {@link SeriesChangeEvent} to all registered listeners.
531 *
532 * @param x the x-value.
533 * @param y the y-value.
534 *
535 * @return The item that was overwritten, if any.
536 *
537 * @since 1.0.10
538 */
539 public XYDataItem addOrUpdate(double x, double y) {
540 return addOrUpdate(new Double(x), new Double(y));
541 }
542
543 /**
544 * Adds or updates an item in the series and sends a
545 * {@link SeriesChangeEvent} to all registered listeners.
546 *
547 * @param x the x-value (<code>null</code> not permitted).
548 * @param y the y-value (<code>null</code> permitted).
549 *
550 * @return A copy of the overwritten data item, or <code>null</code> if no
551 * item was overwritten.
552 */
553 public XYDataItem addOrUpdate(Number x, Number y) {
554 if (x == null) {
555 throw new IllegalArgumentException("Null 'x' argument.");
556 }
557 if (this.allowDuplicateXValues) {
558 add(x, y);
559 return null;
560 }
561
562 // if we get to here, we know that duplicate X values are not permitted
563 XYDataItem overwritten = null;
564 int index = indexOf(x);
565 if (index >= 0) {
566 XYDataItem existing = (XYDataItem) this.data.get(index);
567 try {
568 overwritten = (XYDataItem) existing.clone();
569 }
570 catch (CloneNotSupportedException e) {
571 throw new SeriesException("Couldn't clone XYDataItem!");
572 }
573 existing.setY(y);
574 }
575 else {
576 // if the series is sorted, the negative index is a result from
577 // Collections.binarySearch() and tells us where to insert the
578 // new item...otherwise it will be just -1 and we should just
579 // append the value to the list...
580 if (this.autoSort) {
581 this.data.add(-index - 1, new XYDataItem(x, y));
582 }
583 else {
584 this.data.add(new XYDataItem(x, y));
585 }
586 // check if this addition will exceed the maximum item count...
587 if (getItemCount() > this.maximumItemCount) {
588 this.data.remove(0);
589 }
590 }
591 fireSeriesChanged();
592 return overwritten;
593 }
594
595 /**
596 * Returns the index of the item with the specified x-value, or a negative
597 * index if the series does not contain an item with that x-value. Be
598 * aware that for an unsorted series, the index is found by iterating
599 * through all items in the series.
600 *
601 * @param x the x-value (<code>null</code> not permitted).
602 *
603 * @return The index.
604 */
605 public int indexOf(Number x) {
606 if (this.autoSort) {
607 return Collections.binarySearch(this.data, new XYDataItem(x, null));
608 }
609 else {
610 for (int i = 0; i < this.data.size(); i++) {
611 XYDataItem item = (XYDataItem) this.data.get(i);
612 if (item.getX().equals(x)) {
613 return i;
614 }
615 }
616 return -1;
617 }
618 }
619
620 /**
621 * Returns a new array containing the x and y values from this series.
622 *
623 * @return A new array containing the x and y values from this series.
624 *
625 * @since 1.0.4
626 */
627 public double[][] toArray() {
628 int itemCount = getItemCount();
629 double[][] result = new double[2][itemCount];
630 for (int i = 0; i < itemCount; i++) {
631 result[0][i] = this.getX(i).doubleValue();
632 Number y = getY(i);
633 if (y != null) {
634 result[1][i] = y.doubleValue();
635 }
636 else {
637 result[1][i] = Double.NaN;
638 }
639 }
640 return result;
641 }
642
643 /**
644 * Returns a clone of the series.
645 *
646 * @return A clone of the series.
647 *
648 * @throws CloneNotSupportedException if there is a cloning problem.
649 */
650 public Object clone() throws CloneNotSupportedException {
651 XYSeries clone = (XYSeries) super.clone();
652 clone.data = (List) ObjectUtilities.deepClone(this.data);
653 return clone;
654 }
655
656 /**
657 * Creates a new series by copying a subset of the data in this time series.
658 *
659 * @param start the index of the first item to copy.
660 * @param end the index of the last item to copy.
661 *
662 * @return A series containing a copy of this series from start until end.
663 *
664 * @throws CloneNotSupportedException if there is a cloning problem.
665 */
666 public XYSeries createCopy(int start, int end)
667 throws CloneNotSupportedException {
668
669 XYSeries copy = (XYSeries) super.clone();
670 copy.data = new java.util.ArrayList();
671 if (this.data.size() > 0) {
672 for (int index = start; index <= end; index++) {
673 XYDataItem item = (XYDataItem) this.data.get(index);
674 XYDataItem clone = (XYDataItem) item.clone();
675 try {
676 copy.add(clone);
677 }
678 catch (SeriesException e) {
679 System.err.println("Unable to add cloned data item.");
680 }
681 }
682 }
683 return copy;
684
685 }
686
687 /**
688 * Tests this series for equality with an arbitrary object.
689 *
690 * @param obj the object to test against for equality
691 * (<code>null</code> permitted).
692 *
693 * @return A boolean.
694 */
695 public boolean equals(Object obj) {
696 if (obj == this) {
697 return true;
698 }
699 if (!(obj instanceof XYSeries)) {
700 return false;
701 }
702 if (!super.equals(obj)) {
703 return false;
704 }
705 XYSeries that = (XYSeries) obj;
706 if (this.maximumItemCount != that.maximumItemCount) {
707 return false;
708 }
709 if (this.autoSort != that.autoSort) {
710 return false;
711 }
712 if (this.allowDuplicateXValues != that.allowDuplicateXValues) {
713 return false;
714 }
715 if (!ObjectUtilities.equal(this.data, that.data)) {
716 return false;
717 }
718 return true;
719 }
720
721 /**
722 * Returns a hash code.
723 *
724 * @return A hash code.
725 */
726 public int hashCode() {
727 int result = super.hashCode();
728 // it is too slow to look at every data item, so let's just look at
729 // the first, middle and last items...
730 int count = getItemCount();
731 if (count > 0) {
732 XYDataItem item = getDataItem(0);
733 result = 29 * result + item.hashCode();
734 }
735 if (count > 1) {
736 XYDataItem item = getDataItem(count - 1);
737 result = 29 * result + item.hashCode();
738 }
739 if (count > 2) {
740 XYDataItem item = getDataItem(count / 2);
741 result = 29 * result + item.hashCode();
742 }
743 result = 29 * result + this.maximumItemCount;
744 result = 29 * result + (this.autoSort ? 1 : 0);
745 result = 29 * result + (this.allowDuplicateXValues ? 1 : 0);
746 return result;
747 }
748
749 }
750