📄 timeseries.java
字号:
*
* @return The data item matching the specified period (or
* <code>null</code> if there is no match).
*
*/
public TimeSeriesDataItem getDataItem(RegularTimePeriod period) {
// check arguments...
if (period == null) {
throw new IllegalArgumentException("Null 'period' argument");
}
// fetch the value...
TimeSeriesDataItem dummy = new TimeSeriesDataItem(
period, Integer.MIN_VALUE
);
int index = Collections.binarySearch(this.data, dummy);
if (index >= 0) {
return (TimeSeriesDataItem) this.data.get(index);
}
else {
return null;
}
}
/**
* Returns the time period at the specified index.
*
* @param index the index of the data item.
*
* @return The time period.
*/
public RegularTimePeriod getTimePeriod(int index) {
return getDataItem(index).getPeriod();
}
/**
* Returns a time period that would be the next in sequence on the end of
* the time series.
*
* @return The next time period.
*/
public RegularTimePeriod getNextTimePeriod() {
RegularTimePeriod last = getTimePeriod(getItemCount() - 1);
return last.next();
}
/**
* Returns a collection of all the time periods in the time series.
*
* @return A collection of all the time periods.
*/
public Collection getTimePeriods() {
Collection result = new java.util.ArrayList();
for (int i = 0; i < getItemCount(); i++) {
result.add(getTimePeriod(i));
}
return result;
}
/**
* Returns a collection of time periods in the specified series, but not in
* this series, and therefore unique to the specified series.
*
* @param series the series to check against this one.
*
* @return The unique time periods.
*/
public Collection getTimePeriodsUniqueToOtherSeries(TimeSeries series) {
Collection result = new java.util.ArrayList();
for (int i = 0; i < series.getItemCount(); i++) {
RegularTimePeriod period = series.getTimePeriod(i);
int index = getIndex(period);
if (index < 0) {
result.add(period);
}
}
return result;
}
/**
* Returns the index for the item (if any) that corresponds to a time
* period.
*
* @param period the time period (<code>null</code> not permitted).
*
* @return The index.
*/
public int getIndex(RegularTimePeriod period) {
// check argument...
if (period == null) {
throw new IllegalArgumentException("Null 'period' argument.");
}
// fetch the value...
TimeSeriesDataItem dummy = new TimeSeriesDataItem(
period, Integer.MIN_VALUE
);
int index = Collections.binarySearch(this.data, dummy);
return index;
}
/**
* Returns the value at the specified index.
*
* @param index index of a value.
*
* @return The value (possibly <code>null</code>).
*/
public Number getValue(int index) {
return getDataItem(index).getValue();
}
/**
* Returns the value for a time period. If there is no data item with the
* specified period, this method will return <code>null</code>.
*
* @param period time period (<code>null</code> not permitted).
*
* @return The value (possibly <code>null</code>).
*/
public Number getValue(RegularTimePeriod period) {
int index = getIndex(period);
if (index >= 0) {
return getValue(index);
}
else {
return null;
}
}
/**
* Adds a data item to the series and sends a
* {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param item the (timeperiod, value) pair (<code>null</code> not
* permitted).
*/
public void add(TimeSeriesDataItem item) {
add(item, true);
}
/**
* Adds a data item to the series and sends a
* {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param item the (timeperiod, value) pair (<code>null</code> not
* permitted).
* @param notify notify listeners?
*/
public void add(TimeSeriesDataItem item, boolean notify) {
if (item == null) {
throw new IllegalArgumentException("Null 'item' argument.");
}
if (!item.getPeriod().getClass().equals(this.timePeriodClass)) {
StringBuffer b = new StringBuffer();
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 {
StringBuffer b = new StringBuffer();
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) {
// check if this addition will exceed the maximum item count...
if (getItemCount() > this.maximumItemCount) {
this.data.remove(0);
}
removeAgedItems(false); // remove old items if necessary, but
// don't notify anyone, because that
// happens next anyway...
if (notify) {
fireSeriesChanged();
}
}
}
/**
* Adds a new data item to the series and sends
* a {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param period the time period (<code>null</code> not permitted).
* @param value the value.
*/
public void add(RegularTimePeriod period, double value) {
// defer argument checking...
add(period, value, true);
}
/**
* Adds a new data item to the series and sends
* a {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param period the time period (<code>null</code> not permitted).
* @param value the value.
* @param notify notify listeners?
*/
public void add(RegularTimePeriod period, double value, boolean notify) {
// defer argument checking...
TimeSeriesDataItem item = new TimeSeriesDataItem(period, value);
add(item, notify);
}
/**
* Adds a new data item to the series and sends
* a {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param period the time period (<code>null</code> not permitted).
* @param value the value (<code>null</code> permitted).
*/
public void add(RegularTimePeriod period, Number value) {
// defer argument checking...
add(period, value, true);
}
/**
* Adds a new data item to the series and sends
* a {@link org.jfree.data.general.SeriesChangeEvent} to all registered
* listeners.
*
* @param period the time period (<code>null</code> not permitted).
* @param value the value (<code>null</code> permitted).
* @param notify notify listeners?
*/
public void add(RegularTimePeriod period, Number value, boolean notify) {
// defer argument checking...
TimeSeriesDataItem item = new TimeSeriesDataItem(period, value);
add(item, notify);
}
/**
* Updates (changes) the value for a time period. Throws a
* {@link SeriesException} if the period does not exist.
*
* @param period the period (<code>null</code> not permitted).
* @param value the value (<code>null</code> permitted).
*/
public void update(RegularTimePeriod period, Number value) {
TimeSeriesDataItem temp = new TimeSeriesDataItem(period, value);
int index = Collections.binarySearch(this.data, temp);
if (index >= 0) {
TimeSeriesDataItem pair = (TimeSeriesDataItem) this.data.get(index);
pair.setValue(value);
fireSeriesChanged();
}
else {
throw new SeriesException(
"TimeSeries.update(TimePeriod, Number): period does not exist."
);
}
}
/**
* Updates (changes) the value of a data item.
*
* @param index the index of the data item.
* @param value the new value (<code>null</code> permitted).
*/
public void update(int index, Number value) {
TimeSeriesDataItem item = getDataItem(index);
item.setValue(value);
fireSeriesChanged();
}
/**
* Adds or updates data from one series to another. Returns another series
* containing the values that were overwritten.
*
* @param series the series to merge with this.
*
* @return A series containing the values that were overwritten.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -