| | * @since 4.0 |
| | */ |
| | -public class CircularFifoQueue<E> extends AbstractCollection<E> |
| | +public final class CircularFifoQueue<E> extends AbstractCollection<E> |
| | implements Queue<E> { |
| | /** |
 |
| | * Constructor that creates a queue with the specified size. |
| | * |
| | - * @param size the size of the queue (cannot be changed) |
| | - * @throws IllegalArgumentException if the size is < 1 |
| | + * @param size Immutable queue size, must be greater than zero. |
| | */ |
| | @SuppressWarnings( "unchecked" ) |
| | public CircularFifoQueue( final int size ) { |
| | - if( size <= 0 ) { |
| | - throw new IllegalArgumentException( "The size must be greater than 0" ); |
| | - } |
| | + assert size > 0; |
| | |
| | elements = (E[]) new Object[ size ]; |
 |
| | public E get( final int index ) { |
| | final int sz = size(); |
| | + |
| | if( index < 0 || index >= sz ) { |
| | throw new NoSuchElementException( |
| | String.format( |
| | - "The specified index %1$d is outside the available range [0, %2$d)", |
| | - index, |
| | - sz ) ); |
| | + "Index %1$d is outside the available range [0, %2$d)", |
| | + index, sz ) ); |
| | } |
| | |
 |
| | @Override |
| | public E poll() { |
| | - if( isEmpty() ) { |
| | - return null; |
| | - } |
| | - return remove(); |
| | + return isEmpty() ? null : remove(); |
| | } |
| | |
| | @Override |
| | public E element() { |
| | if( isEmpty() ) { |
| | - throw new NoSuchElementException( "queue is empty" ); |
| | + throw new NoSuchElementException( "empty queue" ); |
| | } |
| | |
 |
| | public E remove() { |
| | if( isEmpty() ) { |
| | - throw new NoSuchElementException( "queue is empty" ); |
| | + throw new NoSuchElementException( "empty queue" ); |
| | } |
| | |
 |
| | return element; |
| | } |
| | - |
| | - //----------------------------------------------------------------------- |
| | |
| | /** |
| | * Increments the internal index. |
| | * |
| | * @param index the index to increment |
| | * @return the updated index |
| | */ |
| | private int increment( int index ) { |
| | - index++; |
| | - if( index >= maxElements ) { |
| | + if( ++index >= maxElements ) { |
| | index = 0; |
| | } |
 |
| | */ |
| | private int decrement( int index ) { |
| | - index--; |
| | - if( index < 0 ) { |
| | + if( --index < 0 ) { |
| | index = maxElements - 1; |
| | } |