PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/kilim/RingQueue.java

http://github.com/kilim/kilim
Java | 121 lines | 108 code | 12 blank | 1 comment | 16 complexity | 2d5fd01e8578508ca8ae79ed3fe8c550 MD5 | raw file
  1// Copyright 2009 by sriram - offered under the terms of the MIT License
  2
  3package kilim;
  4
  5public class RingQueue<T> {
  6    protected T[] elements;
  7    protected int iprod;   // producer index
  8    protected int icons;   // consumer index;
  9    protected int maxSize;
 10    protected int size;
 11
 12    public RingQueue(int initialSize) {
 13        this(initialSize, Integer.MAX_VALUE);
 14    }
 15    
 16    @SuppressWarnings("unchecked")
 17    public RingQueue(int initialSize, int maxSize) {
 18        elements = (T[]) new Object[initialSize];
 19        size = 0;
 20        this.maxSize = maxSize;
 21    }
 22
 23    public int size() {
 24        return size;
 25    }
 26
 27    public T peek() {
 28        T elem;
 29        T[] elems;
 30        int n = size;
 31        if (n > 0) {
 32            elems = elements;
 33            int ic = icons;
 34            elem = elems[ic];
 35            return elem;
 36        }
 37        
 38        return null;
 39    }
 40    
 41    public T get() {
 42        T elem;
 43        T[] elems;
 44        int n = size;
 45        if (n > 0) {
 46            elems = elements;
 47            int ic = icons;
 48            elem = elems[ic];
 49            elems[ic] = null;
 50            icons = (ic + 1) % elems.length;
 51            size = n - 1;
 52        } else {
 53            elem = null;
 54        }
 55        return elem;
 56    }
 57
 58    @SuppressWarnings("unchecked")
 59    public boolean put(T elem) {
 60        boolean ret = true;
 61        if (elem == null) {
 62            throw new NullPointerException("Null message supplied to put");
 63        }
 64        int ip = iprod;
 65        int ic = icons;
 66        int n = size;
 67        if (n == elements.length) {
 68            assert ic == ip : "numElements == elements.length && ic != ip";
 69            if (n < maxSize) {
 70                T[] newmsgs = (T[]) new Object[Math.min(n * 2, maxSize)];
 71                System.arraycopy(elements, ic, newmsgs, 0, n - ic);
 72                if (ic > 0) {
 73                    System.arraycopy(elements, 0, newmsgs, n - ic, ic);
 74                }
 75                elements = newmsgs;
 76                ip = n;
 77                ic = 0;
 78            } else {
 79                ret = false;
 80            }
 81        }
 82        if (ret) {
 83            size = n + 1;
 84            elements[ip] = elem;
 85            iprod = (ip + 1) % elements.length;
 86            icons = ic;
 87        }
 88        return ret;
 89    }
 90
 91    public boolean contains(T obj) {
 92        int i = icons;
 93        int c = 0;
 94        T[] elems = elements;
 95        while (c < size) {
 96            if (obj == elems[i])
 97                return true;
 98            i = (i + 1) % elems.length;
 99            c++;
100        }
101        return false;
102    }
103
104    public void reset() {
105        icons = iprod = 0;
106    }
107
108    @Override
109    public String toString() {
110        StringBuilder sb = new StringBuilder();
111        int i = icons;
112        int c = 0;
113        T[] elems = elements;
114        while (c < size) {
115            sb.append(elems[i]);
116            i = (i + 1) % elems.length;
117            c++;
118        }
119        return sb.toString();
120    }
121}