package com.codahale.metrics;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;

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 AtomicLong index;
    private final AtomicLong count;
    private final AtomicLongArray measurements;

    /**
     * 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.index = new AtomicLong();
        this.count = new AtomicLong();
        this.measurements = new AtomicLongArray(size);
    }

    @Override
    public int size() {
        return (int) min(count.get(), measurements.length());
    }

    @Override
    public void update(long value) {
        // first, get and increment the index
        final int n = (int) (index.getAndIncrement() % measurements.length());
        // second, set the measurement
        measurements.set(n, value);
        // third, increment the count of written measurements
        count.incrementAndGet();
    }

    @Override
    public Snapshot getSnapshot() {
        // use size, not index, to prevent phantom reads
        final long[] values = new long[size()];
        for (int i = 0; i < values.length; i++) {
            values[i] = measurements.get(i);
        }
        return new Snapshot(values);
    }
}

@@ -28,108 +28,8 @@
 s;%0A%0A
-import java.util.concurrent.atomic.AtomicLong;%0Aimport java.util.concurrent.atomic.AtomicLongArray;%0A%0A
 impo
@@ -143,19 +143,16 @@
 ores the
-%0A *
  last %7B@
@@ -158,16 +158,19 @@
 @code N%7D
+%0A *
  measure
@@ -261,60 +261,27 @@
 nal 
-AtomicLong index;%0A    private final AtomicLong cou
+long%5B%5D measureme
 nt
+s
 ;%0A  
@@ -294,42 +294,18 @@
 ate 
-final AtomicLongArray measureme
+long cou
 nt
-s
 ;%0A%0A 
@@ -384,23 +384,16 @@
 the last
-%0A     *
  %7B@code 
@@ -545,71 +545,37 @@
 his.
-index = new AtomicLong();%0A        this.count = new AtomicLong()
+measurements = new long%5Bsize%5D
 ;%0A  
@@ -589,48 +589,17 @@
 his.
-measureme
+cou
 nt
-s
  = 
-new AtomicLongArray(size)
+0
 ;%0A  
@@ -628,16 +628,29 @@
  public 
+synchronized 
 int size
@@ -688,14 +688,8 @@
 ount
-.get()
 , me
@@ -701,26 +701,24 @@
 ments.length
-()
 );%0A    %7D%0A%0A  
@@ -740,16 +740,29 @@
  public 
+synchronized 
 void upd
@@ -791,293 +791,67 @@
     
-// first, get and increment the index%0A        final int n = (int) (index.getAndIncrement() %25 measurements.length());%0A        // second, set the measurement%0A        measurements.set(n, value);%0A        // third, increment the count of written measurements%0A        count.incrementAndGet()
+measurements%5B((int) count++ %25 measurements.length)%5D = value
 ;%0A  
@@ -909,65 +909,8 @@
 ) %7B%0A
-        // use size, not index, to prevent phantom reads%0A
     
@@ -999,24 +999,62 @@
 gth; i++) %7B%0A
+            synchronized (this) %7B%0A    
             
@@ -1081,16 +1081,26 @@
 ents
-.get(i);
+%5Bi%5D;%0A            %7D
 %0A   

Revert "Switch to a two-barrier SlidingWindowReservoir."