Example usage for org.jfree.data ComparableObjectItem getComparable

List of usage examples for org.jfree.data ComparableObjectItem getComparable

Introduction

In this page you can find the example usage for org.jfree.data ComparableObjectItem getComparable.

Prototype

protected Comparable getComparable() 

Source Link

Document

Returns the x-value.

Usage

From source file:org.jfree.data.ComparableObjectSeries.java

/**
 * 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./*w  w  w  .jav a2s . co  m*/
 *
 * @param x  the x-value (<code>null</code> not permitted).
 *
 * @return The index.
 */
public int indexOf(Comparable x) {
    if (this.autoSort) {
        return Collections.binarySearch(this.data, new ComparableObjectItem(x, null));
    } else {
        for (int i = 0; i < this.data.size(); i++) {
            ComparableObjectItem item = (ComparableObjectItem) this.data.get(i);
            if (item.getComparable().equals(x)) {
                return i;
            }
        }
        return -1;
    }
}

From source file:org.jfree.data.ComparableObjectSeries.java

/**
 * 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.// w  w  w  .  j a v  a2s.  co m
 */
protected void add(ComparableObjectItem item, boolean notify) {

    ParamChecks.nullNotPermitted(item, "item");
    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
                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
            int index = indexOf(item.getComparable());
            if (index >= 0) {
                throw new SeriesException("X-value already exists.");
            }
        }
        this.data.add(item);
    }
    if (getItemCount() > this.maximumItemCount) {
        this.data.remove(0);
    }
    if (notify) {
        fireSeriesChanged();
    }
}