| Method from org.jfree.data.time.TimeSeries Detail: |
public void add(TimeSeriesDataItem item) {
add(item, true);
}
|
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();
}
}
}
|
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 SeriesChangeEvent
to all registered listeners. |
public void add(RegularTimePeriod period,
Number value) {
// defer argument checking...
add(period, value, true);
}
|
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 SeriesChangeEvent
to all registered listeners. |
public void add(RegularTimePeriod period,
Number value,
boolean notify) {
// defer argument checking...
TimeSeriesDataItem item = new TimeSeriesDataItem(period, value);
add(item, notify);
}
|
public TimeSeries addAndOrUpdate(TimeSeries series) {
TimeSeries overwritten = new TimeSeries("Overwritten values from: "
+ getKey(), series.getTimePeriodClass());
for (int i = 0; i < series.getItemCount(); i++) {
TimeSeriesDataItem item = series.getDataItem(i);
TimeSeriesDataItem oldItem = addOrUpdate(item.getPeriod(),
item.getValue());
if (oldItem != null) {
overwritten.add(oldItem);
}
}
return overwritten;
}
Adds or updates data from one series to another. Returns another series
containing the values that were overwritten. |
public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period,
double value) {
return addOrUpdate(period, new Double(value));
}
|
public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period,
Number value) {
if (period == null) {
throw new IllegalArgumentException("Null 'period' argument.");
}
TimeSeriesDataItem overwritten = null;
TimeSeriesDataItem key = new TimeSeriesDataItem(period, value);
int index = Collections.binarySearch(this.data, key);
if (index >= 0) {
TimeSeriesDataItem existing
= (TimeSeriesDataItem) this.data.get(index);
overwritten = (TimeSeriesDataItem) existing.clone();
existing.setValue(value);
removeAgedItems(false); // remove old items if necessary, but
// don't notify anyone, because that
// happens next anyway...
fireSeriesChanged();
}
else {
this.data.add(-index - 1, new TimeSeriesDataItem(period, value));
// 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...
fireSeriesChanged();
}
return overwritten;
}
|
public void clear() {
if (this.data.size() > 0) {
this.data.clear();
fireSeriesChanged();
}
}
Removes all data items from the series and sends a
SeriesChangeEvent to all registered listeners. |
public Object clone() throws CloneNotSupportedException {
TimeSeries clone = (TimeSeries) super.clone();
clone.data = (List) ObjectUtilities.deepClone(this.data);
return clone;
}
Returns a clone of the time series.
Notes:
- no need to clone the domain and range descriptions, since String
object is immutable;
- we pass over to the more general method clone(start, end).
|
public TimeSeries createCopy(int start,
int end) throws CloneNotSupportedException {
if (start < 0) {
throw new IllegalArgumentException("Requires start >= 0.");
}
if (end < start) {
throw new IllegalArgumentException("Requires start < = end.");
}
TimeSeries copy = (TimeSeries) super.clone();
copy.data = new java.util.ArrayList();
if (this.data.size() > 0) {
for (int index = start; index < = end; index++) {
TimeSeriesDataItem item
= (TimeSeriesDataItem) this.data.get(index);
TimeSeriesDataItem clone = (TimeSeriesDataItem) item.clone();
try {
copy.add(clone);
}
catch (SeriesException e) {
e.printStackTrace();
}
}
}
return copy;
}
Creates a new timeseries by copying a subset of the data in this time
series. |
public TimeSeries createCopy(RegularTimePeriod start,
RegularTimePeriod end) throws CloneNotSupportedException {
if (start == null) {
throw new IllegalArgumentException("Null 'start' argument.");
}
if (end == null) {
throw new IllegalArgumentException("Null 'end' argument.");
}
if (start.compareTo(end) > 0) {
throw new IllegalArgumentException(
"Requires start on or before end.");
}
boolean emptyRange = false;
int startIndex = getIndex(start);
if (startIndex < 0) {
startIndex = -(startIndex + 1);
if (startIndex == this.data.size()) {
emptyRange = true; // start is after last data item
}
}
int endIndex = getIndex(end);
if (endIndex < 0) { // end period is not in original series
endIndex = -(endIndex + 1); // this is first item AFTER end period
endIndex = endIndex - 1; // so this is last item BEFORE end
}
if ((endIndex < 0) || (endIndex < startIndex)) {
emptyRange = true;
}
if (emptyRange) {
TimeSeries copy = (TimeSeries) super.clone();
copy.data = new java.util.ArrayList();
return copy;
}
else {
return createCopy(startIndex, endIndex);
}
}
Creates a new timeseries by copying a subset of the data in this time
series. |
public void delete(RegularTimePeriod period) {
int index = getIndex(period);
if (index >= 0) {
this.data.remove(index);
fireSeriesChanged();
}
}
Deletes the data item for the given time period and sends a
SeriesChangeEvent to all registered listeners. If there is no
item with the specified time period, this method does nothing. |
public void delete(int start,
int end) {
if (end < start) {
throw new IllegalArgumentException("Requires start < = end.");
}
for (int i = 0; i < = (end - start); i++) {
this.data.remove(start);
}
fireSeriesChanged();
}
Deletes data from start until end index (end inclusive). |
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (!(object instanceof TimeSeries) || !super.equals(object)) {
return false;
}
TimeSeries s = (TimeSeries) object;
if (!ObjectUtilities.equal(getDomainDescription(),
s.getDomainDescription())) {
return false;
}
if (!ObjectUtilities.equal(getRangeDescription(),
s.getRangeDescription())) {
return false;
}
if (!getClass().equals(s.getClass())) {
return false;
}
if (getMaximumItemAge() != s.getMaximumItemAge()) {
return false;
}
if (getMaximumItemCount() != s.getMaximumItemCount()) {
return false;
}
int count = getItemCount();
if (count != s.getItemCount()) {
return false;
}
for (int i = 0; i < count; i++) {
if (!getDataItem(i).equals(s.getDataItem(i))) {
return false;
}
}
return true;
}
Tests the series for equality with an arbitrary object. |
public TimeSeriesDataItem getDataItem(int index) {
return (TimeSeriesDataItem) this.data.get(index);
}
Returns a data item for the series. |
public TimeSeriesDataItem getDataItem(RegularTimePeriod period) {
int index = getIndex(period);
if (index >= 0) {
return (TimeSeriesDataItem) this.data.get(index);
}
else {
return null;
}
}
Returns the data item for a specific period. |
public String getDomainDescription() {
return this.domain;
}
Returns the domain description. |
public int getIndex(RegularTimePeriod period) {
if (period == null) {
throw new IllegalArgumentException("Null 'period' argument.");
}
TimeSeriesDataItem dummy = new TimeSeriesDataItem(
period, Integer.MIN_VALUE);
return Collections.binarySearch(this.data, dummy);
}
Returns the index for the item (if any) that corresponds to a time
period. |
public int getItemCount() {
return this.data.size();
}
Returns the number of items in the series. |
public List getItems() {
return Collections.unmodifiableList(this.data);
}
Returns the list of data items for the series (the list contains
TimeSeriesDataItem objects and is unmodifiable). |
public long getMaximumItemAge() {
return this.maximumItemAge;
}
Returns the maximum item age (in time periods) for the series. |
public int getMaximumItemCount() {
return this.maximumItemCount;
}
Returns the maximum number of items that will be retained in the series.
The default value is Integer.MAX_VALUE. |
public RegularTimePeriod getNextTimePeriod() {
RegularTimePeriod last = getTimePeriod(getItemCount() - 1);
return last.next();
}
Returns a time period that would be the next in sequence on the end of
the time series. |
public String getRangeDescription() {
return this.range;
}
Returns the range description. |
public RegularTimePeriod getTimePeriod(int index) {
return getDataItem(index).getPeriod();
}
Returns the time period at the specified index. |
public Class getTimePeriodClass() {
return this.timePeriodClass;
}
Returns the time period class for this series.
Only one time period class can be used within a single series (enforced).
If you add a data item with a Year for the time period, then all
subsequent data items must also have a Year for the time period. |
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 all the time periods in the time series. |
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 a collection of time periods in the specified series, but not in
this series, and therefore unique to the specified series. |
public Number getValue(int index) {
return getDataItem(index).getValue();
}
Returns the value at the specified index. |
public Number getValue(RegularTimePeriod period) {
int index = getIndex(period);
if (index >= 0) {
return getValue(index);
}
else {
return null;
}
}
Returns the value for a time period. If there is no data item with the
specified period, this method will return null. |
public int hashCode() {
int result = super.hashCode();
result = 29 * result + (this.domain != null ? this.domain.hashCode()
: 0);
result = 29 * result + (this.range != null ? this.range.hashCode() : 0);
result = 29 * result + (this.timePeriodClass != null
? this.timePeriodClass.hashCode() : 0);
// 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) {
TimeSeriesDataItem item = getDataItem(0);
result = 29 * result + item.hashCode();
}
if (count > 1) {
TimeSeriesDataItem item = getDataItem(count - 1);
result = 29 * result + item.hashCode();
}
if (count > 2) {
TimeSeriesDataItem item = getDataItem(count / 2);
result = 29 * result + item.hashCode();
}
result = 29 * result + this.maximumItemCount;
result = 29 * result + (int) this.maximumItemAge;
return result;
}
Returns a hash code value for the object. |
public void removeAgedItems(boolean notify) {
// check if there are any values earlier than specified by the history
// count...
if (getItemCount() > 1) {
long latest = getTimePeriod(getItemCount() - 1).getSerialIndex();
boolean removed = false;
while ((latest - getTimePeriod(0).getSerialIndex())
> this.maximumItemAge) {
this.data.remove(0);
removed = true;
}
if (removed && notify) {
fireSeriesChanged();
}
}
}
Age items in the series. Ensure that the timespan from the youngest to
the oldest record in the series does not exceed maximumItemAge time
periods. Oldest items will be removed if required. |
public void removeAgedItems(long latest,
boolean notify) {
// find the serial index of the period specified by 'latest'
long index = Long.MAX_VALUE;
try {
Method m = RegularTimePeriod.class.getDeclaredMethod(
"createInstance", new Class[] {Class.class, Date.class,
TimeZone.class});
RegularTimePeriod newest = (RegularTimePeriod) m.invoke(
this.timePeriodClass, new Object[] {this.timePeriodClass,
new Date(latest), TimeZone.getDefault()});
index = newest.getSerialIndex();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
// check if there are any values earlier than specified by the history
// count...
boolean removed = false;
while (getItemCount() > 0 && (index
- getTimePeriod(0).getSerialIndex()) > this.maximumItemAge) {
this.data.remove(0);
removed = true;
}
if (removed && notify) {
fireSeriesChanged();
}
}
Age items in the series. Ensure that the timespan from the supplied
time to the oldest record in the series does not exceed history count.
oldest items will be removed if required. |
public void setDomainDescription(String description) {
String old = this.domain;
this.domain = description;
firePropertyChange("Domain", old, description);
}
Sets the domain description and sends a PropertyChangeEvent
(with the property name Domain) to all registered
property change listeners. |
public void setMaximumItemAge(long periods) {
if (periods < 0) {
throw new IllegalArgumentException("Negative 'periods' argument.");
}
this.maximumItemAge = periods;
removeAgedItems(true); // remove old items and notify if necessary
}
Sets the number of time units in the 'history' for the series. This
provides one mechanism for automatically dropping old data from the
time series. For example, if a series contains daily data, you might set
the history count to 30. Then, when you add a new data item, all data
items more than 30 days older than the latest value are automatically
dropped from the series. |
public void setMaximumItemCount(int maximum) {
if (maximum < 0) {
throw new IllegalArgumentException("Negative 'maximum' argument.");
}
this.maximumItemCount = maximum;
int count = this.data.size();
if (count > maximum) {
delete(0, count - maximum - 1);
}
}
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. |
public void setRangeDescription(String description) {
String old = this.range;
this.range = description;
firePropertyChange("Range", old, description);
}
Sets the range description and sends a PropertyChangeEvent
(with the property name Range) to all registered listeners. |
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 for a time period. Throws a
SeriesException if the period does not exist. |
public void update(int index,
Number value) {
TimeSeriesDataItem item = getDataItem(index);
item.setValue(value);
fireSeriesChanged();
}
Updates (changes) the value of a data item. |