T
- The type of the elements in the inheritable thread local stack.@ThreadSafe public final class InheritableThreadLocalStack<T> extends Object
Stack
, except that
each call is forwarded to a private InheritableThreadLocal
object.
Whenever a child thread gets started, it inherits the top level element
of the parent's thread local stack unless the parent's thread local stack
is empty.
However, it's not possible to pop()
this inherited top level element
off the stack - any attempt to do so will result in a
NoSuchElementException
.
Disclaimer: Although this classes internally uses an
InheritableThreadLocal
, it does not leak memory in multi class
loader environments when used appropriately.
Constructor and Description |
---|
InheritableThreadLocalStack() |
Modifier and Type | Method and Description |
---|---|
boolean |
isEmpty()
Returns
true if this stack is empty. |
T |
peek()
Returns the top element on this stack or
null if it's empty. |
T |
peekOrElse(T elze)
Returns the nullable top element on this stack unless it's empty,
in which case
elze gets returned. |
T |
pop()
Removes and returns the nullable top element on this stack.
|
void |
popIf(T expected)
Removes and returns the nullable top element on this stack
if it's identical to the given element.
|
T |
push(T element)
Pushes the given element onto this stack.
|
public boolean isEmpty()
true
if this stack is empty.true
if this stack is empty.@Nullable public T peek()
null
if it's empty.null
if it's empty.@Nullable public T peekOrElse(@CheckForNull T elze)
elze
gets returned.elze
- the nullable default element.elze
gets returned.@Nullable public T pop()
NoSuchElementException
- if this stack is empty.public void popIf(@CheckForNull T expected)
expected
- The expected top element on this stack.IllegalStateException
- If the given element is not the top
element on this stack.@Nullable public T push(@CheckForNull T element)
element
- the nullable element to push onto this stack.element
- for fluent programming.Copyright © 2005–2018 Schlichtherle IT Services. All rights reserved.