Package generic.util
Class DequePush<E>
- java.lang.Object
-
- generic.util.DequePush<E>
-
- Type Parameters:
E- the type of element pushed to the stack
- All Implemented Interfaces:
java.lang.AutoCloseable
public class DequePush<E> extends java.lang.Object implements java.lang.AutoCloseableA context utility allowing stack management via a try-with-resources block
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static <E> DequePush<E>push(java.util.Deque<E> stack, E elem)Push an element to the given stack
-
-
-
Field Detail
-
stack
protected java.util.Deque<E> stack
-
-
Method Detail
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
push
public static <E> DequePush<E> push(java.util.Deque<E> stack, E elem)
Push an element to the given stack- Parameters:
stack- the stackelem- the element- Returns:
- a context used to pop the element
This is an idiomatic convenience, as in a try-with-resources block:
This idiom can be very useful if there is complex logic between the push and pop. It's easy to forget to pop; however, this convenience comes at the cost of a heap allocation.Deque<String> stack = new LinkedList<>(); try(DequePush<?> p = DequePush.push(stack, "Hello, World!\n")) { System.out.println(stack.peek()); }
-
-