Package io.pravega.common.util
Class SimpleDeque<T>
- java.lang.Object
-
- io.pravega.common.util.SimpleDeque<T>
-
- Type Parameters:
T- Type of item in theSimpleDeque.
@NotThreadSafe public class SimpleDeque<T> extends java.lang.ObjectSimplifiedDequeimplementation that provides an efficient method to remove multiple items at once from the head. Note that this class does not implementDequeand as such not all methods in that interface are present here. The only reason it exists is because there is no way inArrayDequeto efficiently remove multiple items at once (seepollFirst(int).
-
-
Constructor Summary
Constructors Constructor Description SimpleDeque()Creates a new instance of theSimpleDequeclass.SimpleDeque(int initialCapacity)Creates a new instance of theSimpleDequeclass.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddLast(T item)Adds a new item at the tail of theSimpleDeque.voidclear()Clears the entireSimpleDeque.booleanisEmpty()Gets a value indicating whether theSimpleDequeis empty or not.TpeekFirst()Returns (but does not remove) the item at the head of theSimpleDeque.TpollFirst()Removes one item from the head of theSimpleDeque.java.util.Queue<T>pollFirst(int maxCount)Removes one or more items from the head of theSimpleDeque.intsize()Gets a value indicating the number of items in theSimpleDeque.java.lang.StringtoString()
-
-
-
Constructor Detail
-
SimpleDeque
public SimpleDeque()
Creates a new instance of theSimpleDequeclass.
-
SimpleDeque
public SimpleDeque(int initialCapacity)
Creates a new instance of theSimpleDequeclass.- Parameters:
initialCapacity- The initial capacity.
-
-
Method Detail
-
addLast
public void addLast(@NonNull T item)Adds a new item at the tail of theSimpleDeque. SeeDeque.addLast(E).- Parameters:
item- The item to add. Must be non-null (since we use null internally as a sentinel)
-
pollFirst
public T pollFirst()
Removes one item from the head of theSimpleDeque. SeeDeque.pollFirst().- Returns:
- The item at the head of the
SimpleDeque, or null of empty.
-
pollFirst
public java.util.Queue<T> pollFirst(int maxCount)
Removes one or more items from the head of theSimpleDeque. This method is equivalent to:
This method is optimized for a bulk copy and is preferred to using the code exemplified above.SimpleDeque source; Queue target; int count = maxCount; while(count > 0 && !source.isEmpty()) { target.add(source.removeFirst()); count--; }- Parameters:
maxCount- The maximum number of items to remove. If this number is larger thansize(), thensize()items will be removed.- Returns:
- A
Queuecontaining the removed items, in the same order as they were in theSimpleDeque.
-
peekFirst
public T peekFirst()
Returns (but does not remove) the item at the head of theSimpleDeque. This method does not alter the internal state of theSimpleDeque. SeeDeque.peekFirst().- Returns:
- The item at the head of the
SimpleDeque, or null if empty.
-
size
public int size()
Gets a value indicating the number of items in theSimpleDeque.- Returns:
- The size of the
SimpleDeque.
-
isEmpty
public boolean isEmpty()
Gets a value indicating whether theSimpleDequeis empty or not. Consider using this instead of{@link #size()} == 0since this method does not need to compute anything.- Returns:
- True if empty, false otherwise.
-
clear
public void clear()
Clears the entireSimpleDeque.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-