/**
  * Updates the cached values for the minimum and maximum data values on the basis that the
  * specified item has just been removed.
  *
  * @param item the item added (<code>null</code> not permitted).
  * @since 1.0.13
  */
 private void updateBoundsForRemovedItem(XYDataItem item) {
   boolean itemContributesToXBounds = false;
   boolean itemContributesToYBounds = false;
   double x = item.getXValue();
   if (!Double.isNaN(x)) {
     if (x <= this.minX || x >= this.maxX) {
       itemContributesToXBounds = true;
     }
   }
   if (item.getY() != null) {
     double y = item.getYValue();
     if (!Double.isNaN(y)) {
       if (y <= this.minY || y >= this.maxY) {
         itemContributesToYBounds = true;
       }
     }
   }
   if (itemContributesToYBounds) {
     findBoundsByIteration();
   } else if (itemContributesToXBounds) {
     if (getAutoSort()) {
       this.minX = getX(0).doubleValue();
       this.maxX = getX(getItemCount() - 1).doubleValue();
     } else {
       findBoundsByIteration();
     }
   }
 }
 /**
  * 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(int maximum) {
   this.maximumItemCount = maximum;
   int remove = this.data.size() - maximum;
   if (remove > 0) {
     this.data.subList(0, remove).clear();
     findBoundsByIteration();
     fireSeriesChanged();
   }
 }
  /**
   * Adds or updates an item in the 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.
   * @since 1.0.14
   */
  public XYDataItem addOrUpdate(XYDataItem item) {
    ParamChecks.nullNotPermitted(item, "item");
    if (this.allowDuplicateXValues) {
      add(item);
      return null;
    }

    // if we get to here, we know that duplicate X values are not permitted
    XYDataItem overwritten = null;
    int index = indexOf(item.getX());
    if (index >= 0) {
      XYDataItem existing = (XYDataItem) this.data.get(index);
      overwritten = (XYDataItem) existing.clone();
      // figure out if we need to iterate through all the y-values
      boolean iterate = false;
      double oldY = existing.getYValue();
      if (!Double.isNaN(oldY)) {
        iterate = oldY <= this.minY || oldY >= this.maxY;
      }
      existing.setY(item.getY());

      if (iterate) {
        findBoundsByIteration();
      } else if (item.getY() != null) {
        double yy = item.getY().doubleValue();
        this.minY = minIgnoreNaN(this.minY, yy);
        this.maxY = maxIgnoreNaN(this.maxY, yy);
      }
    } 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...
      item = (XYDataItem) item.clone();
      if (this.autoSort) {
        this.data.add(-index - 1, item);
      } else {
        this.data.add(item);
      }
      updateBoundsForAddedItem(item);

      // check if this addition will exceed the maximum item count...
      if (getItemCount() > this.maximumItemCount) {
        XYDataItem removed = (XYDataItem) this.data.remove(0);
        updateBoundsForRemovedItem(removed);
      }
    }
    fireSeriesChanged();
    return overwritten;
  }
  /**
   * 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 {@link #updateByIndex(int, Number)} to avoid confusion with the {@link
   *     #update(Number, Number)} method.
   */
  public void update(int index, Number y) {
    XYDataItem item = getRawDataItem(index);

    // figure out if we need to iterate through all the y-values
    boolean iterate = false;
    double oldY = item.getYValue();
    if (!Double.isNaN(oldY)) {
      iterate = oldY <= this.minY || oldY >= this.maxY;
    }
    item.setY(y);

    if (iterate) {
      findBoundsByIteration();
    } else if (y != null) {
      double yy = y.doubleValue();
      this.minY = minIgnoreNaN(this.minY, yy);
      this.maxY = maxIgnoreNaN(this.maxY, yy);
    }
    fireSeriesChanged();
  }
 /**
  * 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(int start, int end) {
   this.data.subList(start, end + 1).clear();
   findBoundsByIteration();
   fireSeriesChanged();
 }