Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.codahale.metrics;
 
 import static java.lang.Math.min;

A Reservoir implementation backed by a sliding window that stores the last N measurements.
 
 public class SlidingWindowReservoir implements Reservoir {
    private final long[] measurements;
    private long count;

    
Creates a new SlidingWindowReservoir which stores the last size measurements.

Parameters:
size the number of measurements to store
    public SlidingWindowReservoir(int size) {
        this. = new long[size];
        this. = 0;
    }
    @Override
    public synchronized int size() {
        return (intmin(.);
    }
    @Override
    public synchronized void update(long value) {
        [((int++ % .)] = value;
    }
    @Override
    public Snapshot getSnapshot() {
        final long[] values = new long[size()];
        for (int i = 0; i < values.lengthi++) {
            synchronized (this) {
                values[i] = [i];
            }
        }
        return new Snapshot(values);
    }
New to GrepCode? Check out our FAQ X