Class IntArrayQueue

All Implemented Interfaces:
Iterable<Integer>, Collection<Integer>, Queue<Integer>

public class IntArrayQueue extends AbstractQueue<Integer>
Queue of ints which stores the elements without boxing. Null is represented by a special nullValue().

The IntArrayQueue.IntIterator is cached by default to avoid allocation unless directed to do so in the constructor.

Note: This class is not threadsafe.

  • Field Details

    • DEFAULT_NULL_VALUE

      public static final int DEFAULT_NULL_VALUE
      Default representation of null for an element.
      See Also:
    • MIN_CAPACITY

      public static final int MIN_CAPACITY
      Minimum capacity for the queue which must also be a power of 2.
      See Also:
    • shouldAvoidAllocation

      private final boolean shouldAvoidAllocation
    • tail

      private int tail
    • nullValue

      private final int nullValue
    • elements

      private int[] elements
    • iterator

      private IntArrayQueue.IntIterator iterator
  • Constructor Details

    • IntArrayQueue

      public IntArrayQueue()
      Construct a new queue defaulting to MIN_CAPACITY capacity, DEFAULT_NULL_VALUE and cached iterators.
    • IntArrayQueue

      public IntArrayQueue(int nullValue)
      Construct a new queue defaulting to MIN_CAPACITY capacity and cached iterators.
      Parameters:
      nullValue - cannot be stored in the queue and used as a sentinel.
    • IntArrayQueue

      public IntArrayQueue(int initialCapacity, int nullValue)
      Construct a new queue default to cached iterators.
      Parameters:
      initialCapacity - for the queue which will be rounded up to the nearest power of 2.
      nullValue - which cannot be stored in the queue and used as a sentinel.
    • IntArrayQueue

      public IntArrayQueue(int initialCapacity, int nullValue, boolean shouldAvoidAllocation)
      Construct a new queue providing all the config options.
      Parameters:
      initialCapacity - for the queue which will be rounded up to the nearest power of 2.
      nullValue - which cannot be stored in the queue and used as a sentinel.
      shouldAvoidAllocation - true to cache the iterator otherwise false to allocate a new iterator each time.
  • Method Details

    • nullValue

      public int nullValue()
      The value representing a null element.
      Returns:
      value representing a null element.
    • capacity

      public int capacity()
      The current capacity for the collection.
      Returns:
      the current capacity for the collection.
    • size

      public int size()
      Specified by:
      size in interface Collection<Integer>
      Specified by:
      size in class AbstractCollection<Integer>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<Integer>
      Overrides:
      isEmpty in class AbstractCollection<Integer>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<Integer>
      Overrides:
      clear in class AbstractQueue<Integer>
    • offer

      public boolean offer(Integer element)
    • offerInt

      public boolean offerInt(int element)
      Offer an element to the tail of the queue without boxing.
      Parameters:
      element - to be offered to the queue.
      Returns:
      will always be true as long as the underlying array can be expanded.
    • add

      public boolean add(Integer element)
      Specified by:
      add in interface Collection<Integer>
      Specified by:
      add in interface Queue<Integer>
      Overrides:
      add in class AbstractQueue<Integer>
    • addInt

      public boolean addInt(int element)
      Offer an element to the tail of the queue without boxing.
      Parameters:
      element - to be offered to the queue.
      Returns:
      will always be true as long as the underlying array can be expanded.
    • peek

      public Integer peek()
    • peekInt

      public int peekInt()
      Peek at the element on the head of the queue without boxing.
      Returns:
      the element at the head of the queue without removing it.
    • poll

      public Integer poll()
    • pollInt

      public int pollInt()
      Poll the element from the head of the queue without boxing.
      Returns:
      the element at the head of the queue removing it. If empty then nullValue.
    • remove

      public Integer remove()
      Specified by:
      remove in interface Queue<Integer>
      Overrides:
      remove in class AbstractQueue<Integer>
    • element

      public Integer element()
      Specified by:
      element in interface Queue<Integer>
      Overrides:
      element in class AbstractQueue<Integer>
    • elementInt

      public int elementInt()
      Peek at the element on the head of the queue without boxing.
      Returns:
      the element at the head of the queue without removing it.
      Throws:
      NoSuchElementException - if the queue is empty.
    • removeInt

      public int removeInt()
      Remove the element at the head of the queue without boxing.
      Returns:
      the element at the head of the queue.
      Throws:
      NoSuchElementException - if the queue is empty.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<Integer>
    • forEach

      public void forEach(Consumer<? super Integer> action)
    • forEachInt

      public void forEachInt(IntConsumer action)
      Iterate over the collection without boxing.
      Parameters:
      action - to be taken for each element.
    • iterator

      public IntArrayQueue.IntIterator iterator()
      Specified by:
      iterator in interface Collection<Integer>
      Specified by:
      iterator in interface Iterable<Integer>
      Specified by:
      iterator in class AbstractCollection<Integer>
    • increaseCapacity

      private void increaseCapacity()