public class MovingAverage extends Object
Note that adding elements out-of-order is supported, but is tricky and will lead to poor results unless the values are tightly packed. For example, adding values that pre-date the covered time period has no effect, and adding values far into the future compared to the rest of the values will lead to premature devaluation of the entire average until the intermediate values are filled-in. In addition, updates anywhere besides the end of the list run in linear time - i.e. O(n).
CONCURRENCY WARNING: this class is not safe under concurrency; synchronize all accesses as-needed for concurrent use. Created by art on 5/27/15.| Modifier and Type | Class and Description |
|---|---|
protected class |
MovingAverage.SampleData |
| Constructor and Description |
|---|
MovingAverage(long sampleTimePeriod,
long maxSlots) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(long timestamp,
long value)
Add the given value with the specified timestamp to the average.
|
protected long |
calcNumSlot()
Calculate the number of effective slots used by the history, including "sparse" slots - i.e.
|
protected BigDecimal |
decayOldValues(long endSlot)
Decay old slots given the new ending slot number.
|
double |
getAverage()
Return the moving average of the values collected.
|
double |
getFullPeriodAverage()
Return the moving average of the values collected while assuming 0 values for any slots not yet collected.
|
void |
put(long timestamp,
long value)
Put the given value at the timeslot for the given timestamp, replacing the existing value, if any.
|
protected void |
update(long timestamp,
long value,
boolean addInd)
Update the average history and accumulator with the given value at the given timestamp, adding or replacing the
value as specified.
|
public void put(long timestamp,
long value)
timestamp - value - public void add(long timestamp,
long value)
timestamp - value - protected void update(long timestamp,
long value,
boolean addInd)
timestamp - timestamp assigned to the value.value - value to store in the history for the moving average.addInd - true => add the value to any existing value; false => replace the current value with the new one.public double getAverage()
public double getFullPeriodAverage()
protected BigDecimal decayOldValues(long endSlot)
endSlot - the new ending slot number.protected long calcNumSlot()
Copyright © 2015. All rights reserved.