public void addValue(long value, long nanoTime) { while (timestamps.getN() > 0) { double age = (nanoTime - timestamps.getOldest()) / 1.e9; if (age < windowSizeInSeconds) break; timestamps.removeOldestN(1); measurements.removeOldestN(1); } timestamps.add(nanoTime); measurements.add(value); }
public void removeOldestN(int n) { if (getN() < n) throw new IllegalArgumentException(); for (int i = 0; i < n; ++i) removeValueFormSumAt(i); advance(n); fill -= n; }
/** * @return @{code true} if the next call to {@link #add(long)} will * overwrite the oldest value. */ public boolean willAddOverwrite() { return getN() == getCapacity(); }
/** * Adds a new value to the ring buffer, overwriting the oldest value if the * ring buffer is already full. */ public void add(long value) { int i = indexOf(getN()); if (willAddOverwrite()) { removeValueFormSumAt(0); advance(1); } else { fill += 1; } buffer[i] = value; addToSum(value); }
public void removeNewestN(int n) { if (getN() < n) throw new IllegalArgumentException(); for (int i = 0; i < n; ++i) removeValueFormSumAt(fill - i - 1); fill -= n; }
public boolean hasMeasurements() { return measurements.getN() > 0; } }
private int advance(int n) { return start = (start + n) % getCapacity(); }
public SpeedMeter( double windowSizeInSeconds, int bufferSize) { if (bufferSize <= 0) throw new IllegalArgumentException(); if (bufferSize > MAX_BUFFER_SIZE) throw new IllegalArgumentException(); this.windowSizeInSeconds = windowSizeInSeconds; this.measurements = new AccumulatingRingBufferLong(bufferSize); this.timestamps = new AccumulatingRingBufferLong(bufferSize); }
private void removeValueFormSumAt(int index) { sum = sum.subtract(BigInteger.valueOf(getAt(index))); } }
public double speed() { return speed(timestamps.getNewest()); }
@Override public boolean hasNext() { return i < getN(); }
/** * @param index * 0 is the oldest value. */ private int indexOf(int index) { return (start + index) % getCapacity(); }