@@ -0,0 +1,2738 @@
+package net.floodlightcontroller.util;%0A%0A%0Aimport java.util.concurrent.atomic.AtomicInteger;%0Aimport java.lang.reflect.Array;%0A%0A%0A%0Apublic class ConcurrentCircularBuffer %3CT%3E %7B%0A    private final AtomicInteger cursor = new AtomicInteger();%0A    private final Object%5B%5D      buffer;%0A    private final Class%3CT%3E      type;%0A%0A    public ConcurrentCircularBuffer (final Class %3CT%3E type, %0A                                     final int bufferSize) %0A%09%7B%0A%09    if (bufferSize %3C 1) %7B%0A%09%09throw new IllegalArgumentException(%0A                %22Buffer size must be a positive value%22%0A%09%09%09%09%09%09   );%0A%09    %7D%0A%0A%09    this.type   = type;%0A%09    this.buffer = new Object %5B bufferSize %5D;%0A%09%7D%0A%0A    public void add (T sample) %7B%0A        buffer%5B cursor.getAndIncrement() %25 buffer.length %5D = sample;%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A%09public T%5B%5D snapshot () %7B%0A        Object%5B%5D snapshots = new Object %5B buffer.length %5D;%0A            %0A        /* Identify the start-position of the buffer. */%0A        int before = cursor.get();%0A%0A        /* Terminate early for an empty buffer. */%0A        if (before == 0) %7B%0A            return (T%5B%5D) Array.newInstance(type, 0);%0A        %7D%0A%0A        System.arraycopy(buffer, 0, snapshots, 0, buffer.length);%0A%0A        int after          = cursor.get();%0A        int size           = buffer.length - (after - before);%0A        int snapshotCursor = before - 1;%0A%0A        /* The entire buffer was replaced during the copy. */%0A        if (size %3C= 0) %7B%0A            return (T%5B%5D) Array.newInstance(type, 0);%0A        %7D%0A%0A        int start = snapshotCursor - (size - 1);%0A        int end   = snapshotCursor;%0A%0A        if (snapshotCursor %3C snapshots.length) %7B%0A            size   = snapshotCursor + 1;%0A            start  = 0;%0A        %7D%0A%0A        /* Copy the sample snapshot to a new array the size of our stable%0A         * snapshot area.%0A         */%0A        T%5B%5D result = (T%5B%5D) Array.newInstance(type, size);%0A%0A        int startOfCopy = start %25 snapshots.length;%0A        int endOfCopy   = end   %25 snapshots.length;%0A%0A        /* If the buffer space wraps the physical end of the array, use two%0A         * copies to construct the new array.%0A         */%0A        if (startOfCopy %3E endOfCopy) %7B%0A            System.arraycopy(snapshots, startOfCopy,%0A                             result, 0, %0A                             snapshots.length - startOfCopy);%0A            System.arraycopy(snapshots, 0,%0A                             result, (snapshots.length - startOfCopy),%0A                             endOfCopy + 1);%0A        %7D%0A        else %7B%0A            /* Otherwise it's a single continuous segment, copy the whole thing%0A             * into the result.%0A             */%0A            System.arraycopy(snapshots, startOfCopy, result, 0, size);%0A        %7D%0A%0A        return (T%5B%5D) result;%0A    %7D%0A%7D

add ConcurrentCircularBuffer