/**
  * 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();
     }
   }
 }
 /**
  * Updates the cached values for the minimum and maximum data values.
  *
  * @param item the item added (<code>null</code> not permitted).
  * @since 1.0.13
  */
 private void updateBoundsForAddedItem(XYDataItem item) {
   double x = item.getXValue();
   this.minX = minIgnoreNaN(this.minX, x);
   this.maxX = maxIgnoreNaN(this.maxX, x);
   if (item.getY() != null) {
     double y = item.getYValue();
     this.minY = minIgnoreNaN(this.minY, y);
     this.maxY = maxIgnoreNaN(this.maxY, y);
   }
 }
  /**
   * 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();
  }