comparableobjectseries.java
来自「jfreechart不错的教程例子谢谢!」· Java 代码 · 共 451 行 · 第 1/2 页
JAVA
451 行
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();
}
}
/**
* 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(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;
}
}
/**
* 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.
*/
protected void update(Comparable x, Object y) {
int index = indexOf(x);
if (index < 0) {
throw new SeriesException("No observation for x = " + x);
}
else {
ComparableObjectItem item = getDataItem(index);
item.setObject(y);
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).
*/
protected void updateByIndex(int index, Object y) {
ComparableObjectItem item = getDataItem(index);
item.setObject(y);
fireSeriesChanged();
}
/**
* Return the data item with the specified index.
*
* @param index the index.
*
* @return The data item with the specified index.
*/
protected ComparableObjectItem getDataItem(int index) {
return (ComparableObjectItem) this.data.get(index);
}
/**
* 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).
*/
protected void delete(int start, int end) {
for (int i = start; i <= end; i++) {
this.data.remove(start);
}
fireSeriesChanged();
}
/**
* Removes all data items from the series and, unless the series is
* already empty, sends a {@link SeriesChangeEvent} to all registered
* listeners.
*/
public void clear() {
if (this.data.size() > 0) {
this.data.clear();
fireSeriesChanged();
}
}
/**
* Removes the item at the specified index and sends a
* {@link SeriesChangeEvent} to all registered listeners.
*
* @param index the index.
*
* @return The item removed.
*/
protected ComparableObjectItem remove(int index) {
ComparableObjectItem result = (ComparableObjectItem) this.data.remove(
index);
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 ComparableObjectItem remove(Comparable x) {
return remove(indexOf(x));
}
/**
* 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(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ComparableObjectSeries)) {
return false;
}
if (!super.equals(obj)) {
return false;
}
ComparableObjectSeries that = (ComparableObjectSeries) obj;
if (this.maximumItemCount != that.maximumItemCount) {
return false;
}
if (this.autoSort != that.autoSort) {
return false;
}
if (this.allowDuplicateXValues != that.allowDuplicateXValues) {
return false;
}
if (!ObjectUtilities.equal(this.data, that.data)) {
return false;
}
return true;
}
/**
* Returns a hash code.
*
* @return A hash code.
*/
public int hashCode() {
int result = super.hashCode();
// it is too slow to look at every data item, so let's just look at
// the first, middle and last items...
int count = getItemCount();
if (count > 0) {
ComparableObjectItem item = getDataItem(0);
result = 29 * result + item.hashCode();
}
if (count > 1) {
ComparableObjectItem item = getDataItem(count - 1);
result = 29 * result + item.hashCode();
}
if (count > 2) {
ComparableObjectItem item = getDataItem(count / 2);
result = 29 * result + item.hashCode();
}
result = 29 * result + this.maximumItemCount;
result = 29 * result + (this.autoSort ? 1 : 0);
result = 29 * result + (this.allowDuplicateXValues ? 1 : 0);
return result;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?