package com.codahale.metrics;

import static java.lang.Math.min;

/**
 * A {@link Reservoir} implementation backed by a sliding window that stores the last {@code N}
 * measurements.
 */
public class SlidingWindowReservoir implements Reservoir {
    private final long[] measurements;
    private boolean fullWindow;
    private long count;

    /**
     * Creates a new {@link SlidingWindowReservoir} which stores the last {@code size} measurements.
     *
     * @param size the number of measurements to store
     */
    public SlidingWindowReservoir(int size) {
        this.measurements = new long[size];
        this.count = 0;
    }

    @Override
    public synchronized int size() {
    	if(fullWindow){
    		return measurements.length;
    	}else{
    		return (int) min(count, measurements.length);
    	}
    }

    @Override
    public synchronized void update(long value) {
        measurements[(int) count++] = value;
    	if(count >= measurements.length){
    		count = 0;
    		fullWindow = true;
    	}
    }

    @Override
    public Snapshot getSnapshot() {
        final long[] values = new long[size()];
        for (int i = 0; i < values.length; i++) {
            synchronized (this) {
                values[i] = measurements[i];
            }
        }
        return new Snapshot(values);
    }
}

@@ -282,40 +282,8 @@
 ts;%0A
-    private boolean fullWindow;%0A
     
@@ -662,77 +662,12 @@
     
-%09if(fullWindow)%7B%0A    %09%09return measurements.length;%0A    %09%7Delse%7B%0A    %09%09
+    
 retu
@@ -708,23 +708,16 @@
 ength);%0A
-    %09%7D%0A
     %7D%0A%0A 
@@ -810,113 +810,49 @@
 nt) 
+(
 count++
-%5D = value;%0A    %09if(count %3E= measurements.length)%7B%0A    %09%09count = 0;%0A    %09%09fullWindow = true;%0A    %09%7D
+ %25 measurements.length)%5D = value;
 %0A   

Update fix per ceetav's comments.