Github user vesense commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1674#discussion_r77605544
  
    --- Diff: 
storm-core/src/jvm/org/apache/storm/scheduler/blacklist/CircularBuffer.java ---
    @@ -0,0 +1,182 @@
    +/**
    + * 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
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * 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.storm.scheduler.blacklist;
    +
    +import java.io.Serializable;
    +import java.util.*;
    +
    +public final class CircularBuffer<T extends Serializable> extends 
AbstractCollection<T> implements Serializable {
    +
    +    // This is the largest capacity allowed by this implementation
    +    private static final int MAX_CAPACITY = 1 << 30;
    +    private static final int DEFAULT_CAPACITY = 1 << 8;
    +
    +    private int size          = 0;
    +    private int producerIndex = 0;
    +    private int consumerIndex = 0;
    +
    +    // capacity must be a power of 2 at all times
    +    private int capacity;
    +    //private int maxCapacity;
    +
    +    // we mask with capacity -1.  This variable caches that values
    +    //private int bitmask;
    +
    +    private Serializable[] q;
    +
    +    public CircularBuffer() {
    +        this(DEFAULT_CAPACITY);
    +    }
    +
    +    // Construct a buffer which has at least the specified capacity.  If
    +    // the value specified is a power of two then the buffer will be
    +    // exactly the specified size.  Otherwise the buffer will be the
    +    // first power of two which is greater than the specified value.
    +    public CircularBuffer(int c) {
    +
    +        if (c > MAX_CAPACITY) {
    +            throw new IllegalArgumentException("Capacity greater than " +
    +                    "allowed");
    +        }
    +
    +        //for (capacity = 1; capacity < c; capacity <<= 1) ;
    +        capacity=c;
    +        //bitmask = capacity - 1;
    +        q = new Serializable[capacity];
    +    }
    +
    +    // Constructor used by clone()
    +    private CircularBuffer(CircularBuffer oldBuffer) {
    +        size = oldBuffer.size;
    +        producerIndex = oldBuffer.producerIndex;
    +        consumerIndex = oldBuffer.consumerIndex;
    +        capacity = oldBuffer.capacity;
    +        //bitmask = oldBuffer.bitmask;
    +        q = new Serializable[oldBuffer.q.length];
    +        System.arraycopy(oldBuffer.q, 0, q, 0, q.length);
    +    }
    +
    +    private boolean isFull(){
    +        return size==capacity;
    +    }
    +
    +    public boolean add(Serializable obj) {
    +        if (isFull()) {
    +            remove();
    +        }
    +
    +        size++;
    +        q[producerIndex] = obj;
    +
    +        producerIndex = (producerIndex + 1) % capacity;
    +
    +        return true;
    +    }
    +
    +    public T remove() {
    +        Serializable obj;
    +
    +        if (size == 0) return null;
    +
    +        size--;
    +        obj = q[consumerIndex];
    +        q[consumerIndex] = null; // allow gc to collect
    +
    +        consumerIndex = (consumerIndex + 1) % capacity;
    +
    +        return (T)obj;
    +    }
    +
    +    public boolean isEmpty() { return size == 0; }
    +
    +    public int size() { return size; }
    +
    +    public int capacity() { return capacity; }
    +
    +    public Serializable peek() {
    +        if (size == 0) return null;
    +        return q[consumerIndex];
    +    }
    +
    +    public void clear() {
    +        Arrays.fill(q, null);
    +        size = 0;
    +        producerIndex = 0;
    +        consumerIndex = 0;
    +    }
    +
    +    public Object clone() {
    +        return new CircularBuffer(this);
    +    }
    +
    +    public String toString() {
    +        StringBuffer s = new StringBuffer(super.toString() + "' size: '" + 
size() + "'");
    +
    +        return s.toString();
    +    }
    +
    +    public Iterator iterator() {
    +        return new Iterator<T>() {
    +            private final int ci = consumerIndex;
    +            private final int pi = producerIndex;
    +            private int s = size;
    +            private int i = ci;
    +
    +            public boolean hasNext() {
    +                checkForModification();
    +                return s > 0;
    +            }
    +
    +            public T next() {
    +                checkForModification();
    +                if (s == 0) throw new NoSuchElementException();
    +
    +                s--;
    +                T r = (T)q[i];
    +                i = (i + 1) % capacity;
    +
    +                return r;
    +            }
    +
    +            public void remove() {
    +                throw new UnsupportedOperationException();
    +            }
    +
    +            private void checkForModification() {
    +                if (ci != consumerIndex) throw new 
ConcurrentModificationException();
    +                if (pi != producerIndex) throw new 
ConcurrentModificationException();
    +            }
    +        };
    +    }
    +
    +    public static void main(String []args){
    --- End diff --
    
    move this to a unit test?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to