Example usage for org.jfree.data.time TimeSeriesDataItem clone

List of usage examples for org.jfree.data.time TimeSeriesDataItem clone

Introduction

In this page you can find the example usage for org.jfree.data.time TimeSeriesDataItem clone.

Prototype

@Override
public Object clone() 

Source Link

Document

Clones the data item.

Usage

From source file:org.jfree.data.time.TimeSeries.java

/**
 * Returns a data item from the dataset.  Note that the returned object
 * is a clone of the item in the series, so modifying it will have no 
 * effect on the data series.//from  ww w  .j  a va2  s  .  c  o  m
 * 
 * @param index  the item index.
 * 
 * @return The data item.
 */
public TimeSeriesDataItem getDataItem(int index) {
    TimeSeriesDataItem item = (TimeSeriesDataItem) this.data.get(index);
    return (TimeSeriesDataItem) item.clone();
}

From source file:org.jfree.data.time.TimeSeries.java

/**
 * Creates a new timeseries by copying a subset of the data in this time
 * series.//from   w ww  . j ava 2  s  .  co m
 *
 * @param start  the index of the first time period to copy.
 * @param end  the index of the last time period to copy.
 *
 * @return A series containing a copy of this times series from start until
 *         end.
 *
 * @throws CloneNotSupportedException if there is a cloning problem.
 */
public TimeSeries createCopy(int start, int end) throws CloneNotSupportedException {
    if (start < 0) {
        throw new IllegalArgumentException("Requires start >= 0.");
    }
    if (end < start) {
        throw new IllegalArgumentException("Requires start <= end.");
    }
    TimeSeries copy = (TimeSeries) super.clone();
    copy.minY = Double.NaN;
    copy.maxY = Double.NaN;
    copy.data = new java.util.ArrayList();
    if (this.data.size() > 0) {
        for (int index = start; index <= end; index++) {
            TimeSeriesDataItem item = (TimeSeriesDataItem) this.data.get(index);
            TimeSeriesDataItem clone = (TimeSeriesDataItem) item.clone();
            try {
                copy.add(clone);
            } catch (SeriesException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return copy;
}

From source file:org.jfree.data.time.TimeSeries.java

/**
 * Adds or updates an item in the times series and sends a
 * {@link SeriesChangeEvent} to all registered listeners.
 *
 * @param item  the data item (<code>null</code> not permitted).
 *
 * @return A copy of the overwritten data item, or <code>null</code> if no
 *         item was overwritten.//from  w  ww .  java 2  s  .  com
 *
 * @since 1.0.14
 */
public TimeSeriesDataItem addOrUpdate(TimeSeriesDataItem item) {

    ParamChecks.nullNotPermitted(item, "item");
    Class periodClass = item.getPeriod().getClass();
    if (this.timePeriodClass == null) {
        this.timePeriodClass = periodClass;
    } else if (!this.timePeriodClass.equals(periodClass)) {
        String msg = "You are trying to add data where the time " + "period class is " + periodClass.getName()
                + ", but the TimeSeries is expecting an instance of " + this.timePeriodClass.getName() + ".";
        throw new SeriesException(msg);
    }
    TimeSeriesDataItem overwritten = null;
    int index = Collections.binarySearch(this.data, item);
    if (index >= 0) {
        TimeSeriesDataItem existing = (TimeSeriesDataItem) this.data.get(index);
        overwritten = (TimeSeriesDataItem) existing.clone();
        // figure out if we need to iterate through all the y-values
        // to find the revised minY / maxY
        boolean iterate = false;
        Number oldYN = existing.getValue();
        double oldY = oldYN != null ? oldYN.doubleValue() : Double.NaN;
        if (!Double.isNaN(oldY)) {
            iterate = oldY <= this.minY || oldY >= this.maxY;
        }
        existing.setValue(item.getValue());
        if (iterate) {
            updateMinMaxYByIteration();
        } else if (item.getValue() != null) {
            double yy = item.getValue().doubleValue();
            this.minY = minIgnoreNaN(this.minY, yy);
            this.maxY = maxIgnoreNaN(this.maxY, yy);
        }
    } else {
        item = (TimeSeriesDataItem) item.clone();
        this.data.add(-index - 1, item);
        updateBoundsForAddedItem(item);

        // check if this addition will exceed the maximum item count...
        if (getItemCount() > this.maximumItemCount) {
            TimeSeriesDataItem d = (TimeSeriesDataItem) this.data.remove(0);
            updateBoundsForRemovedItem(d);
        }
    }
    removeAgedItems(false); // remove old items if necessary, but
                            // don't notify anyone, because that
                            // happens next anyway...
    fireSeriesChanged();
    return overwritten;

}

From source file:org.jfree.data.time.TimeSeries.java

/**
 * Adds a data item to the series and sends a {@link SeriesChangeEvent} to
 * all registered listeners.//from w w w. j  ava  2s  . com
 *
 * @param item  the (timeperiod, value) pair (<code>null</code> not
 *              permitted).
 * @param notify  notify listeners?
 */
public void add(TimeSeriesDataItem item, boolean notify) {
    ParamChecks.nullNotPermitted(item, "item");
    item = (TimeSeriesDataItem) item.clone();
    Class c = item.getPeriod().getClass();
    if (this.timePeriodClass == null) {
        this.timePeriodClass = c;
    } else if (!this.timePeriodClass.equals(c)) {
        StringBuilder b = new StringBuilder();
        b.append("You are trying to add data where the time period class ");
        b.append("is ");
        b.append(item.getPeriod().getClass().getName());
        b.append(", but the TimeSeries is expecting an instance of ");
        b.append(this.timePeriodClass.getName());
        b.append(".");
        throw new SeriesException(b.toString());
    }

    // make the change (if it's not a duplicate time period)...
    boolean added = false;
    int count = getItemCount();
    if (count == 0) {
        this.data.add(item);
        added = true;
    } else {
        RegularTimePeriod last = getTimePeriod(getItemCount() - 1);
        if (item.getPeriod().compareTo(last) > 0) {
            this.data.add(item);
            added = true;
        } else {
            int index = Collections.binarySearch(this.data, item);
            if (index < 0) {
                this.data.add(-index - 1, item);
                added = true;
            } else {
                StringBuilder b = new StringBuilder();
                b.append("You are attempting to add an observation for ");
                b.append("the time period ");
                b.append(item.getPeriod().toString());
                b.append(" but the series already contains an observation");
                b.append(" for that time period. Duplicates are not ");
                b.append("permitted.  Try using the addOrUpdate() method.");
                throw new SeriesException(b.toString());
            }
        }
    }
    if (added) {
        updateBoundsForAddedItem(item);
        // check if this addition will exceed the maximum item count...
        if (getItemCount() > this.maximumItemCount) {
            TimeSeriesDataItem d = (TimeSeriesDataItem) this.data.remove(0);
            updateBoundsForRemovedItem(d);
        }

        removeAgedItems(false); // remove old items if necessary, but
                                // don't notify anyone, because that
                                // happens next anyway...
        if (notify) {
            fireSeriesChanged();
        }
    }

}