Package io.microsphere.collection
Class AbstractDeque<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- io.microsphere.collection.AbstractDeque<E>
-
- Type Parameters:
E
- The type of elements held in this deque
- All Implemented Interfaces:
java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Deque<E>
,java.util.Queue<E>
- Direct Known Subclasses:
EmptyDeque
,SingletonDeque
public abstract class AbstractDeque<E> extends java.util.AbstractQueue<E> implements java.util.Deque<E>
AbstractDeque
implementation that provides default implementations for some of the operations in theDeque
interface.This class extends
AbstractQueue
, which itself extendsAbstractCollection
, and hence inherits behaviors such as size, isEmpty, etc. Subclasses need to implement the abstract methods defined by the deque contract, such as insertion, removal, and access methods.The default implementations in this class make assumptions about the behavior of other methods, so subclasses should ensure consistent behavior when overriding any of these methods.
Example Usage
public class SimpleDeque<E> extends AbstractDeque<E> { private final List<E> list = new ArrayList<>(); public void addFirst(E e) { list.add(0, e); } public void addLast(E e) { list.add(e); } public E peekFirst() { return list.isEmpty() ? null : list.get(0); } public E peekLast() { return list.isEmpty() ? null : list.get(list.size() - 1); } public E pollFirst() { return list.isEmpty() ? null : list.remove(0); } public E pollLast() { return list.isEmpty() ? null : list.remove(list.size() - 1); } public boolean removeFirstOccurrence(Object o) { return list.remove(o); } public boolean removeLastOccurrence(Object o) { int index = list.lastIndexOf(o); if (index != -1) { list.remove(index); return true; } return false; } }
- Since:
- 1.0.0
- Author:
- Mercy
-
-
Constructor Summary
Constructors Constructor Description AbstractDeque()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFirst(E e)
void
addLast(E e)
boolean
offer(E e)
E
peek()
E
poll()
E
pop()
void
push(E e)
E
removeFirst()
boolean
removeFirstOccurrence(java.lang.Object o)
E
removeLast()
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
-
-