Class LIFOQueue<E>

java.lang.Object
de.geolykt.starloader.deobf.LIFOQueue<E>
Type Parameters:
E - The type of elements that should be stored in the queue.
All Implemented Interfaces:
Iterable<E>

public class LIFOQueue<E> extends Object implements Iterable<E>
A Last-in-first-out (LIFO) queue that uses a Deque as a delegate. Technically the Deque can be used directly, however the issue is that there is a rather high chance of logical programmer errors as Queue.add(Object) does not specify the insertion order and ArrayDeque.add(Object) adds the element to the tail of the queue and not to the head as it might be expected. This class resolves that issue.

Alternatively the Stack class could be used as a LIFO queue, however it has the drawbacks of almost every method being synchronised and thus not being very good in concurrent environments as well of implementing Deque and thus also inheriting the above stated issue.

Author:
Geolykt
  • Constructor Details

  • Method Details

    • add

      public void add(E element)
      Calls Deque.addFirst(Object), which adds an element to the head of the queue. If the underlying Dequeue has capacity restrictions, it throws an IllegalStateException. Some Dequeues (such as ArrayDeque) may not accept null elements and will throw a NullPointerException, which would be propagated by this method.
      Parameters:
      element - The element to add
    • clear

      public void clear()
      Clears all elements from the queue.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getDelegate

      public Deque<E> getDelegate()
      Obtains the dequeue delegate for more advanced operations.
      Returns:
      The delegate Deque
    • getDelegateList

      public LinkedList<E> getDelegateList()
      Obtains the LinkedList delegate for more advanced operations.
      Returns:
      The delegate LinkedList.
    • getHead

      public E getHead()
      Obtains the element at the head of the queue, if there is no such element then a NoSuchElementException will be thrown. This method calls Deque.getFirst().
      Returns:
      The element at the head of the queue.
      Throws:
      NoSuchElementException - If there are no elements in the queue.
    • getSize

      public int getSize()
      Obtains the amount of elements left in the queue.
      Returns:
      The amount of elements in the queue
      See Also:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • isEmpty

      public boolean isEmpty()
      Checks whether the queue is empty. This method corresponds to Collection.isEmpty(). This method should generally only return true is getSize() is equal to 0.
      Returns:
      False if there is at least one element left in the queue, otherwise true.
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Iterable<E>
    • remove

      public E remove()
      Calls Deque.remove() and thus throws an exception if there is no element left in the queue. If there is an element left in the queue, the element at the head of the queue is removed from the queue and returned.
      Returns:
      The removed element.
      Throws:
      NoSuchElementException - If there are no elements left in the queue.
    • spliterator

      public Spliterator<E> spliterator()
      Specified by:
      spliterator in interface Iterable<E>
    • toString

      public String toString()
      Overrides:
      toString in class Object