Class ArrayQueue<T>

  • Type Parameters:
    T - The type of object stored in this queue.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<T>, Collection<T>, Queue<T>

    public class ArrayQueue<T>
    extends java.lang.Object
    implements Queue<T>, java.io.Serializable
    Implementation of the Queue interface that is backed by an ArrayList.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayQueue()
      Construct an empty queue, with all defaults.
      ArrayQueue​(int capacity)
      Construct an empty queue with a given initial capacity.
      ArrayQueue​(int capacity, int maxLen)
      Construct an empty queue with a given initial capacity and maxmimum length.
      ArrayQueue​(int capacity, int maxLen, java.util.Comparator<T> comparator)
      Construct an empty queue with a given initial capacity, maximum length, and comparator for ordering the queue.
      ArrayQueue​(java.util.Comparator<T> comparator)
      Construct an empty queue with the given comparator used to order the elements in it.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all elements from the collection.
      T dequeue()
      Removes the item from the head of the queue and returns it.
      void enqueue​(T item)
      Enqueues an item.
      void ensureCapacity​(int capacity)
      Ensure that the queue has sufficient internal capacity to satisfy the given number of elements.
      java.util.Comparator<T> getComparator()
      Returns the collection's sort order.
      int getLength()  
      int getMaxLength()  
      ListenerList<QueueListener<T>> getQueueListeners()  
      boolean isEmpty()
      Tests the emptiness of the queue.
      java.util.Iterator<T> iterator()  
      T peek()
      Returns the item at the head of the queue without removing it from the queue.
      void setComparator​(java.util.Comparator<T> comparator)
      Set/remove the comparator for this queue.
      void setMaxLength​(int maxLen)
      Set the maximum allowed queue length (0 means unlimited).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • ArrayQueue

        public ArrayQueue()
        Construct an empty queue, with all defaults.
      • ArrayQueue

        public ArrayQueue​(java.util.Comparator<T> comparator)
        Construct an empty queue with the given comparator used to order the elements in it.
        Parameters:
        comparator - A comparator used to sort the elements in the queue as they are added.
      • ArrayQueue

        public ArrayQueue​(int capacity)
        Construct an empty queue with a given initial capacity.
        Parameters:
        capacity - The initial capacity for the queue.
      • ArrayQueue

        public ArrayQueue​(int capacity,
                          int maxLen)
        Construct an empty queue with a given initial capacity and maxmimum length.
        Parameters:
        capacity - The initial capacity for the queue.
        maxLen - The maximum permitted queue length.
      • ArrayQueue

        public ArrayQueue​(int capacity,
                          int maxLen,
                          java.util.Comparator<T> comparator)
        Construct an empty queue with a given initial capacity, maximum length, and comparator for ordering the queue.
        Parameters:
        capacity - The initial capacity for the queue.
        maxLen - The maximum permitted queue length.
        comparator - The comparator to use for ordering the elements.
    • Method Detail

      • enqueue

        public void enqueue​(T item)
        Description copied from interface: Queue
        Enqueues an item. If the queue is unsorted, the item is added at the tail of the queue (index 0). Otherwise, it is inserted at the appropriate index according to the priority/sort order.

        If there is a maximum queue length defined and the queue is already at the maximum length this new item will not be queued.

        Specified by:
        enqueue in interface Queue<T>
        Parameters:
        item - The item to add to the queue.
      • dequeue

        public T dequeue()
        Description copied from interface: Queue
        Removes the item from the head of the queue and returns it. Calling this method should have the same effect as: remove(getLength() - 1, 1);
        Specified by:
        dequeue in interface Queue<T>
        Returns:
        The (removed) object at the head of the queue.
      • peek

        public T peek()
        Description copied from interface: Queue
        Returns the item at the head of the queue without removing it from the queue. Returns null if the queue contains no items. Will also return null if the head item in the queue is null. isEmpty() can be used to distinguish between these two cases.
        Specified by:
        peek in interface Queue<T>
        Returns:
        The object at the head of the queue (not removed from the queue).
      • clear

        public void clear()
        Description copied from interface: Collection
        Removes all elements from the collection.
        Specified by:
        clear in interface Collection<T>
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: Queue
        Tests the emptiness of the queue.
        Specified by:
        isEmpty in interface Collection<T>
        Specified by:
        isEmpty in interface Queue<T>
        Returns:
        true if the queue contains no items; false, otherwise.
      • getLength

        public int getLength()
        Specified by:
        getLength in interface Queue<T>
        Returns:
        The length of the queue.
      • getMaxLength

        public int getMaxLength()
        Specified by:
        getMaxLength in interface Queue<T>
        Returns:
        The maximum queue length allowed (0 means unlimited).
      • setMaxLength

        public void setMaxLength​(int maxLen)
        Description copied from interface: Queue
        Set the maximum allowed queue length (0 means unlimited).
        Specified by:
        setMaxLength in interface Queue<T>
        Parameters:
        maxLen - The maximum allowed length.
      • ensureCapacity

        public void ensureCapacity​(int capacity)
        Ensure that the queue has sufficient internal capacity to satisfy the given number of elements.
        Parameters:
        capacity - The capacity to ensure (to make sure no further allocations are done to accommodate this number of elements).
      • getComparator

        public java.util.Comparator<T> getComparator()
        Description copied from interface: Collection
        Returns the collection's sort order.
        Specified by:
        getComparator in interface Collection<T>
        Returns:
        The comparator used to order elements in the collection, or null if the sort order is undefined.
        See Also:
        Collection.setComparator(Comparator)
      • setComparator

        public void setComparator​(java.util.Comparator<T> comparator)
        Set/remove the comparator for this queue.

        If a new comparator is set the queue will be reordered if it contains any elements. Removing the comparator will not reorder any elements.

        Calls the QueueListener.comparatorChanged(org.apache.pivot.collections.Queue<T>, java.util.Comparator<T>) method for each registered listener after setting the new comparator.

        Specified by:
        setComparator in interface Collection<T>
        Parameters:
        comparator - The new comparator used to order the elements in the queue. Can be null to remove the existing comparator.
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>