📄 timeseries.java
字号:
* to all registerd listeners. * * @param item the (timeperiod, value) pair (<code>null</code> not permitted). */ public void add(final TimeSeriesDataItem item) { if (item == null) { throw new IllegalArgumentException("Null 'item' argument."); } if (!item.getPeriod().getClass().equals(this.timePeriodClass)) { String message = "TimeSeries.add(): you are trying to add data where the time "; message = message + "period class is " + item.getPeriod().getClass().getName() + ", "; message = message + "but the TimeSeries is expecting an instance of " + this.timePeriodClass.getName() + "."; throw new SeriesException(message); } // make the change (if it's not a duplicate time period)... final int index = Collections.binarySearch(this.data, item); if (index < 0) { this.data.add(-index - 1, item); // check if this addition will exceed the maximum item count... if (getItemCount() > this.maximumItemCount) { this.data.remove(0); } // check if there are any values earlier than specified by the history count... ageHistoryCountItems(); fireSeriesChanged(); } else { String message = "TimeSeries.add(): you are attempting to add an observation for "; message += "the time period " + item.getPeriod().toString() + " but the "; message += "series already contains an observation for that time period. "; message += "Duplicates are not permitted. Try using the addOrUpdate() method."; throw new SeriesException(message); } } /** * Adds a new data item to the series and sends a {@link org.jfree.data.SeriesChangeEvent} * to all registerd listeners. * * @param period the time period (<code>null</code> not permitted). * @param value the value. */ public void add(final RegularTimePeriod period, final double value) { // defer argument checking... final TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); add(item); } /** * Adds a new data item to the series and sends a {@link org.jfree.data.SeriesChangeEvent} * to all registerd listeners. * * @param period the time period (<code>null</code> not permitted). * @param value the value (<code>null</code> permitted). */ public void add(final RegularTimePeriod period, final Number value) { // defer argument checking... final TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); add(item); } /** * 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(final RegularTimePeriod period, final Number value) { final TimeSeriesDataItem temp = new TimeSeriesDataItem(period, value); final int index = Collections.binarySearch(this.data, temp); if (index >= 0) { final 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(final int index, final Number value) { final 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. */ public TimeSeries addAndOrUpdate(final TimeSeries series) { final TimeSeries overwritten = new TimeSeries("Overwritten values from: " + getName()); for (int i = 0; i < series.getItemCount(); i++) { final TimeSeriesDataItem pair = series.getDataItem(i); final TimeSeriesDataItem oldPair = addOrUpdate(pair.getPeriod(), pair.getValue()); if (oldPair != null) { try { overwritten.add(oldPair); } catch (SeriesException e) { // should not get here... System.err.println( "TimeSeries.addAndOrUpdate(series): " + "unable to add data to overwritten series." ); } } } return overwritten; } /** * Adds or updates an item in the times series and sends a * {@link org.jfree.data.SeriesChangeEvent} to all registered listenrs. * * @param period the time period to add/update (<code>null</code> not permitted). * @param value the new value. * * @return A copy of the overwritten data item, or <code>null</code> if no item * was overwritten. */ public TimeSeriesDataItem addOrUpdate(final RegularTimePeriod period, final double value) { return this.addOrUpdate(period, new Double(value)); } /** * Adds or updates an item in the times series and sends a * {@link org.jfree.data.SeriesChangeEvent} to all registered listenrs. * * @param period the time period to add/update (<code>null</code> not permitted). * @param value the new value (<code>null</code> permitted). * * @return A copy of the overwritten data item, or <code>null</code> if no item * was overwritten. */ public TimeSeriesDataItem addOrUpdate(final RegularTimePeriod period, final Number value) { if (period == null) { throw new IllegalArgumentException("Null 'period' argument."); } TimeSeriesDataItem overwritten = null; final TimeSeriesDataItem key = new TimeSeriesDataItem(period, value); final int index = Collections.binarySearch(this.data, key); if (index >= 0) { final TimeSeriesDataItem existing = (TimeSeriesDataItem) this.data.get(index); overwritten = (TimeSeriesDataItem) existing.clone(); existing.setValue(value); ageHistoryCountItems(); fireSeriesChanged(); } else { this.data.add(-index - 1, new TimeSeriesDataItem(period, value)); ageHistoryCountItems(); fireSeriesChanged(); } return overwritten; } /** * Age items in the series. Ensure that the timespan from the youngest to the oldest record * in the series does not exceed history count. oldest items will be removed if required. */ public void ageHistoryCountItems() { // check if there are any values earlier than specified by the history count... if ((getItemCount() > 1) && (this.historyCount > 0)) { final long latest = getTimePeriod(getItemCount() - 1).getSerialIndex(); while ((latest - getTimePeriod(0).getSerialIndex()) >= this.historyCount) { this.data.remove(0); } } } /** * 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. * * @param latest the time to be compared against when aging data. */ public void ageHistoryCountItems(final long latest) { // check if there are any values earlier than specified by the history count... if ((getItemCount() > 1) && (this.historyCount > 0)) { while ((latest - getTimePeriod(0).getSerialIndex()) >= this.historyCount) { this.data.remove(0); } } } /** * Removes all data items from the series and sends a {@link org.jfree.data.SeriesChangeEvent} * to all registered listeners. */ public void clear() { if (this.data.size() > 0) { this.data.clear(); fireSeriesChanged(); } } /** * Deletes the data item for the given time period and sends * a {@link org.jfree.data.SeriesChangeEvent} to all registered listeners. * * @param period the period of the item to delete (<code>null</code> not permitted). */ public void delete(final RegularTimePeriod period) { final int index = getIndex(period); this.data.remove(index); fireSeriesChanged(); } /** * Deletes data from start until end index (end inclusive). * * @param start the index of the first period to delete. * @param end the index of the last period to delete. */ public void delete(final int start, final int end) { for (int i = 0; i <= (end - start); i++) { this.data.remove(start); } fireSeriesChanged(); } /** * Returns a clone of the time series. * <P> * Notes: * <ul> * <li>no need to clone the domain and range descriptions, since String object is * immutable;</li> * <li>we pass over to the more general method clone(start, end).</li> * </ul> * * @return A clone of the time series. * * @throws CloneNotSupportedException not thrown by this class, but subclasses may differ. */ public Object clone() throws CloneNotSupportedException { final Object clone = createCopy(0, getItemCount() - 1); return clone; } /** * Creates a new timeseries by copying a subset of the data in this time * series. * * @param start the index of the first time period to copy. * @param end the index of the last time period to copy. * * @return A series containing a copy of this times series from start until end. * * @throws CloneNotSupportedException if there is a cloning problem. */ public TimeSeries createCopy(final int start, final int end) throws CloneNotSupportedException { final TimeSeries copy = (TimeSeries) super.clone(); copy.data = new java.util.ArrayList(); if (this.data.size() > 0) { for (int index = start; index <= end; index++) { final TimeSeriesDataItem item = (TimeSeriesDataItem) this.data.get(index); final TimeSeriesDataItem clone = (TimeSeriesDataItem) item.clone(); try { copy.add(clone); } catch (SeriesException e) { System.err.println("TimeSeries.createCopy(): unable to add cloned data item."); } } } return copy; } /** * Creates a new timeseries by copying a subset of the data in this time series. * * @param start the first time period to copy. * @param end the last time period to copy. * * @return a time series containing a copy of this time series from start until end. * * @throws CloneNotSupportedException if there is a cloning problem. */ public TimeSeries createCopy(final RegularTimePeriod start, final RegularTimePeriod end) throws CloneNotSupportedException { int startIndex = getIndex(start); if (startIndex < 0) { startIndex = -(startIndex + 1); } int endIndex = getIndex(end); if (endIndex < 0) { // end period is not in original series endIndex = -(endIndex + 1); // this gives first item AFTER end period endIndex = endIndex - 1; // so this gives last item BEFORE end period } final TimeSeries result = createCopy(startIndex, endIndex); return result; } /** * Tests the series for equality with an arbitrary object. * * @param object the object to test against (<code>null</code> permitted). * * @return A boolean. */ public boolean equals(final Object object) { if (object == this) { return true; } if (!(object instanceof TimeSeries) || !super.equals(object)) { return false; } final TimeSeries s = (TimeSeries) object; if (!getDomainDescription().equals(s.getDomainDescription())) { return false; } if (!getRangeDescription().equals(s.getRangeDescription())) { return false; } if (!getClass().equals(s.getClass())) { return false; } if (getHistoryCount() != s.getHistoryCount()) { return false; } if (getMaximumItemCount() != s.getMaximumItemCount()) { return false; } final 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; } /** * Returns a hash code value for the object. * * @return The hashcode */ public int hashCode() { int result; 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); result = 29 * result + this.data.hashCode(); result = 29 * result + this.maximumItemCount; result = 29 * result + this.historyCount; return result; } //// DEPRECATED CODE ////////////////////////////////////////////////////////////////////////// /** * Returns one data pair for the series. * * @param index the item index (zero-based). * * @return one data pair for the series. * @deprecated Use getDataItem(int). */ public TimeSeriesDataItem getDataPair(final int index) { return getDataItem(index); } /** * Returns the data pair for a specific period. * * @param period the period of interest. * * @return the data pair matching the specified period (or null if there is no match). * @deprecated Use getDataItem(RegularTimePeriod). * */ public TimeSeriesDataItem getDataPair(final RegularTimePeriod period) { return getDataItem(period); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -