Class SynchronizedQueue<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>, Collection<T>, Queue<T>

    public class SynchronizedQueue<T>
    extends java.lang.Object
    implements Queue<T>
    Synchronized implementation of the Queue interface.
    • 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.
      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()
      NOTE Callers must manually synchronize on the SynchronizedQueue instance to ensure thread safety during iteration.
      T peek()
      Returns the item at the head of the queue without removing it from the queue.
      void setComparator​(java.util.Comparator<T> comparator)
      Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.
      void setMaxLength​(int maxLength)
      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

      • SynchronizedQueue

        public SynchronizedQueue​(Queue<T> queue)
    • 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 maxLength)
        Description copied from interface: Queue
        Set the maximum allowed queue length (0 means unlimited).
        Specified by:
        setMaxLength in interface Queue<T>
        Parameters:
        maxLength - The maximum allowed length.
      • 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)
        Description copied from interface: Collection
        Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.

        Calling this method more than once with the same comparator will re-sort the collection.

        Specified by:
        setComparator in interface Collection<T>
        Parameters:
        comparator - The comparator used to order elements in the collection, or null if the collection is unsorted.
      • iterator

        public java.util.Iterator<T> iterator()
        NOTE Callers must manually synchronize on the SynchronizedQueue instance to ensure thread safety during iteration.
        Specified by:
        iterator in interface java.lang.Iterable<T>