Package org.sonar.java.collections
Interface PStack<E>
- Type Parameters:
E
- the type of elements maintained by this stack
public interface PStack<E>
Persistent (functional) Stack.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Test given predicate on elements and return true if any of elements matches the predicatevoid
Performs the given action for each element in this stack until all elements have been processed or the action throws an exception.boolean
isEmpty()
peek()
peek(int i)
pop()
int
size()
Naive implementation has O(n) time complexity, where n is number of elements.toString()
-
Method Details
-
push
- Returns:
- new stack with added element
-
peek
E peek()- Returns:
- element at the top of this stack
- Throws:
IllegalStateException
- if this stack is empty.
-
peek
- Parameters:
i
- - index of element to be returned, 0 means top of the stack- Returns:
- i-th element from top of the stack
- Throws:
IllegalStateException
- if stack has less than i elements
-
pop
- Returns:
- new stack with removed element
- Throws:
IllegalStateException
- if this stack is empty.
-
isEmpty
boolean isEmpty()- Returns:
- true if this stack contains no elements
-
forEach
Performs the given action for each element in this stack until all elements have been processed or the action throws an exception. -
anyMatch
Test given predicate on elements and return true if any of elements matches the predicate- Parameters:
predicate
- predicate to be tested- Returns:
- true if any of the stack elements satisfies the predicate
-
size
int size()Naive implementation has O(n) time complexity, where n is number of elements. More clever implementation could take advantage of PStack's immutability- Returns:
- number of elements in the stack
-
toString
String toString()
-