Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 
 
 /* ------------------------------------------------------------ */
Queue backed by circular array.

This partial Queue implementation (also with remove() for stack operation) is backed by a growable circular array.

Parameters:
<E>
 
 public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
 {
     public static final int DEFAULT_CAPACITY = 64;
     public static final int DEFAULT_GROWTH = 32;
 
     protected final Object _lock;
     protected final int _growCapacity;
     protected Object[] _elements;
     protected int _nextE;
     protected int _nextSlot;
     protected int _size;
 
     /* ------------------------------------------------------------ */
     public ArrayQueue()
     {
         this(, -1);
     }
 
     /* ------------------------------------------------------------ */
     public ArrayQueue(int capacity)
     {
         this(capacity, -1);
     }
 
     /* ------------------------------------------------------------ */
     public ArrayQueue(int initCapacityint growBy)
     {
         this(initCapacitygrowBynull);
     }
 
     /* ------------------------------------------------------------ */
     public ArrayQueue(int initCapacityint growByObject lock)
     {
          = lock == null ? this : lock;
          = growBy;
          = new Object[initCapacity];
     }
 
     /* ------------------------------------------------------------ */
     public int getCapacity()
     {
         synchronized ()
         {
             return .;
         }
     }
 
     /* ------------------------------------------------------------ */
     @Override
     public boolean add(E e)
     {
         if (!offer(e))
             throw new IllegalStateException("Full");
         return true;
     }
 
     /* ------------------------------------------------------------ */
     public boolean offer(E e)
     {
         synchronized ()
         {
             return enqueue(e);
         }
     }
 
     /* ------------------------------------------------------------ */
    private boolean enqueue(E e)
    {
        if ( == . && !grow())
            return false;
        ++;
        [++] = e;
        if ( == .)
             = 0;
        return true;
    }
    /* ------------------------------------------------------------ */
    
Add without synchronization or bounds checking

Parameters:
e the element to add
See also:
add(java.lang.Object)
    public void addUnsafe(E e)
    {
        if (!enqueue(e))
            throw new IllegalStateException("Full");
    }
    /* ------------------------------------------------------------ */
    public E element()
    {
        synchronized ()
        {
            if (isEmpty())
                throw new NoSuchElementException();
            return at();
        }
    }
    @SuppressWarnings("unchecked")
    private E at(int index)
    {
        return (E)[index];
    }
    /* ------------------------------------------------------------ */
    public E peek()
    {
        synchronized ()
        {
            if (isEmpty())
                return null;
            return at();
        }
    }
    /* ------------------------------------------------------------ */
    public E poll()
    {
        synchronized ()
        {
            if ( == 0)
                return null;
            return dequeue();
        }
    }
    /* ------------------------------------------------------------ */
    private E dequeue()
    {
        E e = at();
        [] = null;
        --;
        if (++ == .)
             = 0;
        return e;
    }
    /* ------------------------------------------------------------ */
    public E remove()
    {
        synchronized ()
        {
            if ( == 0)
                throw new NoSuchElementException();
            return dequeue();
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public void clear()
    {
        synchronized ()
        {
             = 0;
             = 0;
             = 0;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public boolean isEmpty()
    {
        synchronized ()
        {
            return  == 0;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public int size()
    {
        synchronized ()
        {
            return ;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public E get(int index)
    {
        synchronized ()
        {
            if (index < 0 || index >= )
                throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" +  + ")");
            return getUnsafe(index);
        }
    }
    /* ------------------------------------------------------------ */
    
Get without synchronization or bounds checking.

Parameters:
index index of the element to return
Returns:
the element at the specified index
See also:
get(int)
    public E getUnsafe(int index)
    {
        int i = ( + index) % .;
        return at(i);
    }
    /* ------------------------------------------------------------ */
    @Override
    public E remove(int index)
    {
        synchronized ()
        {
            if (index < 0 || index >= )
                throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" +  + ")");
            int i = ( + index) % .;
            E old = at(i);
            if (i < )
            {
                // 0                         _elements.length
                //       _nextE........._nextSlot
                System.arraycopy(i + 1, i - i);
                --;
                --;
            }
            else
            {
                // 0                         _elements.length
                // ......_nextSlot   _nextE..........
                System.arraycopy(i + 1, i. - i - 1);
                if ( > 0)
                {
                    [. - 1] = [0];
                    System.arraycopy(, 1, , 0,  - 1);
                    --;
                }
                else
                     = . - 1;
                --;
            }
            return old;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public E set(int index, E element)
    {
        synchronized ()
        {
            if (index < 0 || index >= )
                throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" +  + ")");
            int i =  + index;
            if (i >= .)
                i -= .;
            E old = at(i);
            [i] = element;
            return old;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public void add(int index, E element)
    {
        synchronized ()
        {
            if (index < 0 || index > )
                throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" +  + ")");
            if ( == . && !grow())
                throw new IllegalStateException("Full");
            if (index == )
            {
                add(element);
            }
            else
            {
                int i =  + index;
                if (i >= .)
                    i -= .;
                ++;
                ++;
                if ( == .)
                     = 0;
                if (i < )
                {
                    // 0                         _elements.length
                    //       _nextE.....i..._nextSlot
                    // 0                         _elements.length
                    // ..i..._nextSlot   _nextE..........
                    System.arraycopy(ii + 1,  - i);
                    [i] = element;
                }
                else
                {
                    // 0                         _elements.length
                    // ......_nextSlot   _nextE.....i....
                    if ( > 0)
                    {
                        System.arraycopy(, 0, , 1, );
                        [0] = [. - 1];
                    }
                    System.arraycopy(ii + 1, . - i - 1);
                    [i] = element;
                }
            }
        }
    }
    /* ------------------------------------------------------------ */
    protected boolean grow()
    {
        synchronized ()
        {
            if ( <= 0)
                return false;
            Object[] elements = new Object[. + ];
            int split = . - ;
            if (split > 0)
                System.arraycopy(elements, 0, split);
            if ( != 0)
                System.arraycopy(, 0, elementssplit);
             = elements;
             = 0;
             = ;
            return true;
        }
    }
New to GrepCode? Check out our FAQ X