Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenquotes.git

Minor queue simplifications

AuthorDave Jarvis <email>
Date2021-06-02 22:11:48 GMT-0700
Commitf26ac0b0d3f28bcd8ed613086655d8a3d9a679b5
Parent45f5d44
lib/src/main/java/com/keenwrite/quotes/CircularFifoQueue.java
* @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 &lt; 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;
}
Delta11 lines added, 21 lines removed, 10-line decrease