/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cassandra.utils;

import java.util.ArrayDeque;
import java.util.Iterator;

/**
 * not threadsafe.  caller is responsible for any locking necessary.
 */
public class BoundedStatsDeque extends AbstractStatsDeque
{
    private final int size;
    protected final ArrayDeque<Double> deque;

    public BoundedStatsDeque(int size)
    {
        this.size = size;
        deque = new ArrayDeque<Double>(size);
    }

    public Iterator<Double> iterator()
    {
        return deque.iterator();
    }

    public int size()
    {
        return deque.size();
    }

    public void clear()
    {
        deque.clear();
    }

    public void add(double o)
    {
        if (size == deque.size())
        {
            deque.remove();
        }
        deque.add(o);
    }
}

@@ -839,26 +839,24 @@
 va.util.
-ArrayDeque
+Iterator
 ;%0Aimport
@@ -870,91 +870,113 @@
 til.
-Iterator;%0A%0A/**%0A * not threadsafe.  caller is responsible for any locking necessary.
+NoSuchElementException;%0Aimport java.util.concurrent.LinkedBlockingDeque;%0A%0A/**%0A * bounded threadsafe deque
 %0A */
@@ -1046,55 +1046,36 @@
   pr
-ivate
+otected
  final 
+L
 in
-t size;%0A    protected final Array
+kedBlocking
 Dequ
@@ -1149,51 +1149,34 @@
     
-this.size = size;%0A        deque = new Array
+deque = new LinkedBlocking
 Dequ
@@ -1435,17 +1435,17 @@
 (double 
-o
+i
 )%0A    %7B%0A
@@ -1460,27 +1460,22 @@
 if (
-size == deque.size(
+!deque.offer(i
 ))%0A 
@@ -1499,55 +1499,229 @@
     
-deque.remove();%0A        %7D%0A        deque.add(o);
+try%0A            %7B%0A                deque.remove();%0A            %7D%0A            catch (NoSuchElementException e)%0A            %7B%0A                // oops, clear() beat us to it%0A            %7D%0A            deque.offer(i);%0A        %7D
 %0A   

Make BoundedStatsDeque threadsafe. Patch by brandonwilliams, reviewed by jbellis for CASSANDRA-4019