package com.positive.charts.data.xy;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.positive.charts.common.SeriesException;
import com.positive.charts.data.general.Series;
import com.positive.charts.event.SeriesChangeEvent;
/**
* Represents a sequence of zero or more data items in the form (x, y). By
* default, items in the series will be sorted into ascending order by x-value,
* and duplicate x-values are permitted. Both the sorting and duplicate defaults
* can be changed in the constructor. Y-values can be <code>null</code> to
* represent missing values.
*/
public class XYSeries extends Series implements Cloneable, Serializable {
/** For serialization. */
static final long serialVersionUID = -5908509288197150436L;
/** Storage for the data items in the series. <XYDataItem> */
protected List data;
/** The maximum number of items for the series. */
private int maximumItemCount = Integer.MAX_VALUE;
/** A flag that controls whether the items are automatically sorted. */
private final boolean autoSort;
/** A flag that controls whether or not duplicate x-values are allowed. */
private final boolean allowDuplicateXValues;
/**
* Creates a new empty series. By default, items added to the series will be
* sorted into ascending order by x-value, and duplicate x-values will be
* allowed (these defaults can be modified with another constructor.
*
* @param key
* the series key (<code>null</code> not permitted).
*/
public XYSeries(final Comparable key) {
this(key, true, true);
}
/**
* Constructs a new empty series, with the auto-sort flag set as requested,
* and duplicate values allowed.
*
* @param key
* the series key (<code>null</code> not permitted).
* @param autoSort
* a flag that controls whether or not the items in the series
* are sorted.
*/
public XYSeries(final Comparable key, final boolean autoSort) {
this(key, autoSort, true);
}
/**
* Constructs a new xy-series that contains no data. You can specify whether
* or not duplicate x-values are allowed for the series.
*
* @param key
* the series key (<code>null</code> not permitted).
* @param autoSort
* a flag that controls whether or not the items in the series
* are sorted.
* @param allowDuplicateXValues
* a flag that controls whether duplicate x-values are allowed.
*/
public XYSeries(final Comparable key, final boolean autoSort,
final boolean allowDuplicateXValues) {
super(key);
this.data = new ArrayList();
this.autoSort = autoSort;
this.allowDuplicateXValues = allowDuplicateXValues;
}
/**
* Adds a data item to the series and sends a {@link SeriesChangeEvent} to
* all registered listeners.
*
* @param x
* the x value.
* @param y
* the y value.
*/
public void add(final double x, final double y) {
this.add(new Double(x), new Double(y), true);
}
/**
* Adds a data item to the series and, if requested, sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param x
* the x value.
* @param y
* the y value.
* @param notify
* a flag that controls whether or not a
* {@link SeriesChangeEvent} is sent to all registered listeners.
*/
public void add(final double x, final double y, final boolean notify) {
this.add(new Double(x), new Double(y), notify);
}
/**
* Adds a data item to the series and sends a {@link SeriesChangeEvent} to
* all registered listeners. The unusual pairing of parameter types is to
* make it easier to add <code>null</code> y-values.
*
* @param x
* the x value.
* @param y
* the y value (<code>null</code> permitted).
*/
public void add(final double x, final Number y) {
this.add(new Double(x), y);
}
/**
* Adds a data item to the series and, if requested, sends a
* {@link SeriesChangeEvent} to all registered listeners. The unusual
* pairing of parameter types is to make it easier to add null y-values.
*
* @param x
* the x value.
* @param y
* the y value (<code>null</code> permitted).
* @param notify
* a flag that controls whether or not a
* {@link SeriesChangeEvent} is sent to all registered listeners.
*/
public void add(final double x, final Number y, final boolean notify) {
this.add(new Double(x), y, notify);
}
/**
* Adds new data to the series and sends a {@link SeriesChangeEvent} to all
* registered listeners.
* <P>
* Throws an exception if the x-value is a duplicate AND the
* allowDuplicateXValues flag is false.
*
* @param x
* the x-value (<code>null</code> not permitted).
* @param y
* the y-value (<code>null</code> permitted).
*/
public void add(final Number x, final Number y) {
// argument checking delegated...
this.add(x, y, true);
}
/**
* Adds new data to the series and, if requested, sends a
* {@link SeriesChangeEvent} to all registered listeners.
* <P>
* Throws an exception if the x-value is a duplicate AND the
* allowDuplicateXValues flag is false.
*
* @param x
* the x-value (<code>null</code> not permitted).
* @param y
* the y-value (<code>null</code> permitted).
* @param notify
* a flag the controls whether or not a {@link SeriesChangeEvent}
* is sent to all registered listeners.
*/
public void add(final Number x, final Number y, final boolean notify) {
// delegate argument checking to XYDataItem...
final XYDataItem item = new XYDataItem(x, y);
this.add(item, notify);
}
/**
* Adds a data item to the series and sends a {@link SeriesChangeEvent} to
* all registered listeners.
*
* @param item
* the (x, y) item (<code>null</code> not permitted).
*/
public void add(final XYDataItem item) {
// argument checking delegated...
this.add(item, true);
}
/**
* Adds a data item to the series and, if requested, sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param item
* the (x, y) item (<code>null</code> not permitted).
* @param notify
* a flag that controls whether or not a
* {@link SeriesChangeEvent} is sent to all registered listeners.
*/
public void add(final XYDataItem item, final boolean notify) {
if (item == null) {
throw new IllegalArgumentException("Null 'item' argument.");
}
if (this.autoSort) {
int index = Collections.binarySearch(this.data, item);
if (index < 0) {
this.data.add(-index - 1, item);
} else {
if (this.allowDuplicateXValues) {
// need to make sure we are adding *after* any duplicates
final int size = this.data.size();
while ((index < size)
&& (item.compareTo(this.data.get(index)) == 0)) {
index++;
}
if (index < this.data.size()) {
this.data.add(index, item);
} else {
this.data.add(item);
}
} else {
throw new SeriesException("X-value already exists.");
}
}
} else {
if (!this.allowDuplicateXValues) {
// can't allow duplicate values, so we need to check whether
// there is an item with the given x-value already
final int index = this.indexOf(item.getX());
if (index >= 0) {
throw new SeriesException("X-value already exists.");
}
}
this.data.add(item);
}
if (this.getItemCount() > this.maximumItemCount) {
this.data.remove(0);
}
if (notify) {
this.fireSeriesChanged();
}
}
/**
* Adds or updates an item in the series and sends a
* {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param x
* the x-value (<code>null</code> not permitted).
* @param y
* the y-value (<code>null</code> permitted).
*
* @return A copy of the overwritten data item, or <code>null</code> if no
* item was overwritten.
*/
public XYDataItem addOrUpdate(final Number x, final Number y) {
if (x == null) {
throw new IllegalArgumentException("Null 'x' argument.");
}
XYDataItem overwritten = null;
final int index = this.indexOf(x);
if (index >= 0) {
final XYDataItem existing = (XYDataItem) this.data.get(index);
try {
overwritten = (XYDataItem) existing.clone();
} catch (final CloneNotSupportedException e) {
throw new SeriesException("Couldn't clone XYDataItem!");
}
existing.setY(y);
} else {
// if the series is sorted, the negative index is a result from
// Collections.binarySearch() and tells us where to insert the
// new item...otherwise it will be just -1 and we should just
// append the value to the list...
if (this.autoSort) {
this.data.add(-index - 1, new XYDataItem(x, y));
} else {
this.data.add(new XYDataItem(x, y));
}
// check if this addition will exceed the maximum item count...
if (this.getItemCount() > this.maximumItemCount) {
this.data.remove(0);
}
}
this.fireSeriesChanged();
return overwritten;
}
/**
* Removes all data items from the series.
*/
public void clear() {
if (this.data.size() > 0) {
this.data.clear();
this.fireSeriesChanged();
}
}
/**
* Returns a clone of the series.
*
* @return A clone of the time series.
*
* @throws CloneNotSupportedException
* if there is a cloning problem.
*/
public Object clone() throws CloneNotSupportedException {
final Object clone = this.createCopy(0, this.getItemCount() - 1);
return clone;
}
/**
* Creates a new series by copying a subset of the data in this time series.
*
* @param start
* the index of the first item to copy.
* @param end
* the index of the last item to copy.
*
* @return A series containing a copy of this series from start until end.
*
* @throws CloneNotSupportedException
* if there is a cloning problem.
*/
public XYSeries createCopy(final int start, final int end)
throws CloneNotSupportedException {
final XYSeries copy = (XYSeries) super.clone();
copy.data = new ArrayList();
if (this.data.size() > 0) {
for (int index = start; index <= end; index++) {
final XYDataItem item = (XYDataItem) this.data.get(index);
final XYDataItem clone = (XYDataItem) item.clone();
try {
copy.add(clone);
} catch (final SeriesException e) {
System.err.println("Unable to add cloned data item.");
}
}
}
return copy;
}
/**
* Deletes a range of items from the series and sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param start
* the start index (zero-based).
* @param end
* the end index (zero-based).
*/
public void delete(final int start, final int end) {
for (int i = start; i <= end; i++) {
this.data.remove(start);
}
this.fireSeriesChanged();
}
/**
* Tests this series for equality with an arbitrary object.
*
* @param obj
* the object to test against for equality (<code>null</code>
* permitted).
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof XYSeries)) {
return false;
}
if (!super.equals(obj)) {
return false;
}
final XYSeries that = (XYSeries) obj;
if (this.maximumItemCount != that.maximumItemCount) {
return false;
}
if (this.autoSort != that.autoSort) {
return false;
}
if (this.allowDuplicateXValues != that.allowDuplicateXValues) {
return false;
}
if (!this.data.equals(that.data)) {
return false;
}
return true;
}
/**
* Returns a flag that controls whether duplicate x-values are allowed. This
* flag can only be set in the constructor.
*
* @return A boolean.
*/
public boolean getAllowDuplicateXValues() {
return this.allowDuplicateXValues;
}
/**
* Returns the flag that controls whether the items in the series are
* automatically sorted. There is no setter for this flag, it must be
* defined in the series constructor.
*
* @return A boolean.
*/
public boolean getAutoSort() {
return this.autoSort;
}
/**
* Return the data item with the specified index.
*
* @param index
* the index.
*
* @return The data item with the specified index.
*/
public XYDataItem getDataItem(final int index) {
return (XYDataItem) this.data.get(index);
}
/**
* Returns the number of items in the series.
*
* @return The item count.
*/
public int getItemCount() {
return this.data.size();
}
/**
* Returns the list of data items for the series (the list contains
* {@link XYDataItem} objects and is unmodifiable).
*
* @return The list of data items.
*/
public List getItems() {
return Collections.unmodifiableList(this.data);
}
/**
* Returns the maximum number of items that will be retained in the series.
* The default value is <code>Integer.MAX_VALUE</code>.
*
* @return The maximum item count.
* @see #setMaximumItemCount(int)
*/
public int getMaximumItemCount() {
return this.maximumItemCount;
}
/**
* Returns the x-value at the specified index.
*
* @param index
* the index (zero-based).
*
* @return The x-value (never <code>null</code>).
*/
public Number getX(final int index) {
return this.getDataItem(index).getX();
}
/**
* Returns the y-value at the specified index.
*
* @param index
* the index (zero-based).
*
* @return The y-value (possibly <code>null</code>).
*/
public Number getY(final int index) {
return this.getDataItem(index).getY();
}
/**
* Returns a hash code.
*
* @return A hash code.
*/
public int hashCode() {
int result = super.hashCode();
result = 29 * result + (this.data != null ? this.data.hashCode() : 0);
result = 29 * result + this.maximumItemCount;
result = 29 * result + (this.autoSort ? 1 : 0);
result = 29 * result + (this.allowDuplicateXValues ? 1 : 0);
return result;
}
/**
* Returns the index of the item with the specified x-value, or a negative
* index if the series does not contain an item with that x-value. Be aware
* that for an unsorted series, the index is found by iterating through all
* items in the series.
*
* @param x
* the x-value (<code>null</code> not permitted).
*
* @return The index.
*/
public int indexOf(final Number x) {
if (this.autoSort) {
return Collections.binarySearch(this.data, new XYDataItem(x, null));
} else {
for (int i = 0; i < this.data.size(); i++) {
final XYDataItem item = (XYDataItem) this.data.get(i);
if (item.getX().equals(x)) {
return i;
}
}
return -1;
}
}
/**
* Removes the item at the specified index and sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param index
* the index.
*
* @return The item removed.
*/
public XYDataItem remove(final int index) {
final XYDataItem result = (XYDataItem) this.data.remove(index);
this.fireSeriesChanged();
return result;
}
/**
* Removes the item with the specified x-value and sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param x
* the x-value.
*
* @return The item removed.
*/
public XYDataItem remove(final Number x) {
return this.remove(this.indexOf(x));
}
/**
* Sets the maximum number of items that will be retained in the series. If
* you add a new item to the series such that the number of items will
* exceed the maximum item count, then the first element in the series is
* automatically removed, ensuring that the maximum item count is not
* exceeded.
* <p>
* Typically this value is set before the series is populated with data, but
* if it is applied later, it may cause some items to be removed from the
* series (in which case a {@link SeriesChangeEvent} will be sent to all
* registered listeners.
*
* @param maximum
* the maximum number of items for the series.
*/
public void setMaximumItemCount(final int maximum) {
this.maximumItemCount = maximum;
boolean dataRemoved = false;
while (this.data.size() > maximum) {
this.data.remove(0);
dataRemoved = true;
}
if (dataRemoved) {
this.fireSeriesChanged();
}
}
/**
* Updates the value of an item in the series and sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param index
* the item (zero based index).
* @param y
* the new value (<code>null</code> permitted).
*
* @deprecated Renamed updateByIndex(int, Number) to avoid confusion with
* the update(Number, Number) method.
*/
public void update(final int index, final Number y) {
final XYDataItem item = this.getDataItem(index);
item.setY(y);
this.fireSeriesChanged();
}
/**
* Updates an item in the series.
*
* @param x
* the x-value (<code>null</code> not permitted).
* @param y
* the y-value (<code>null</code> permitted).
*
* @throws SeriesException
* if there is no existing item with the specified x-value.
*/
public void update(final Number x, final Number y) {
final int index = this.indexOf(x);
if (index < 0) {
throw new SeriesException("No observation for x = " + x);
} else {
final XYDataItem item = this.getDataItem(index);
item.setY(y);
this.fireSeriesChanged();
}
}
/**
* Updates the value of an item in the series and sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param index
* the item (zero based index).
* @param y
* the new value (<code>null</code> permitted).
*
* @since 1.0.1
*/
public void updateByIndex(final int index, final Number y) {
this.update(index, y);
}
}