Package org.parboiled.support
Class DefaultValueStack<V>
- java.lang.Object
-
- org.parboiled.support.DefaultValueStack<V>
-
- Type Parameters:
V- the type of the value objects
- All Implemented Interfaces:
java.lang.Iterable<V>,ValueStack<V>
- Direct Known Subclasses:
DebuggingValueStack
public class DefaultValueStack<V> extends java.lang.Object implements ValueStack<V>
An implementation of a stack of value objects providing an efficient snapshot capability and a number of convenience methods. The current state of the stack can be saved and restored in small constant time with the methodstakeSnapshot()andrestoreSnapshot(Object)()}. The implementation also serves as an Iterable over the current stack values (the values are being provided with the last value (on top of the stack) first).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classDefaultValueStack.Element
-
Field Summary
Fields Modifier and Type Field Description protected DefaultValueStack.Elementheadprotected VtempValue
-
Constructor Summary
Constructors Constructor Description DefaultValueStack()Initializes an empty value stack.DefaultValueStack(java.lang.Iterable<V> values)Initializes a value stack containing the given values with the last value being at the top of the stack.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears all values.voiddup()Duplicates the top value.booleanisEmpty()Determines whether the stack is empty.java.util.Iterator<V>iterator()Vpeek()Returns the value at the top of the stack without removing it.Vpeek(int down)Returns the value the given number of elements below the top of the stack without removing it.voidpoke(int down, V value)Replaces the element the given number of elements below the current top of the stack.voidpoke(V value)Replaces the current top value with the given value.Vpop()Removes the value at the top of the stack and returns it.Vpop(int down)Removes the value the given number of elements below the top of the stack.voidpush(int down, V value)Inserts the given value a given number of elements below the current top of the stack.voidpush(V value)Pushes the given value onto the stack.voidpushAll(java.lang.Iterable<V> values)Pushes all given elements onto the stack (in the order as given).voidpushAll(V firstValue, V... moreValues)Pushes all given elements onto the stack (in the order as given).voidrestoreSnapshot(java.lang.Object snapshot)Restores the stack state as previously returned byValueStack.takeSnapshot().intsize()Returns the number of elements currently on the stack.voidswap()Swaps the top two stack values.voidswap3()Reverses the order of the top 3 stack values.voidswap4()Reverses the order of the top 4 stack values.voidswap5()Reverses the order of the top 5 stack values.voidswap6()Reverses the order of the top 5 stack values.java.lang.ObjecttakeSnapshot()Returns an object representing the current state of the stack.
-
-
-
Field Detail
-
head
protected DefaultValueStack.Element head
-
tempValue
protected V tempValue
-
-
Constructor Detail
-
DefaultValueStack
public DefaultValueStack()
Initializes an empty value stack.
-
DefaultValueStack
public DefaultValueStack(java.lang.Iterable<V> values)
Initializes a value stack containing the given values with the last value being at the top of the stack.- Parameters:
values- the initial stack values
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Description copied from interface:ValueStackDetermines whether the stack is empty.- Specified by:
isEmptyin interfaceValueStack<V>- Returns:
- true if empty
-
size
public int size()
Description copied from interface:ValueStackReturns the number of elements currently on the stack.- Specified by:
sizein interfaceValueStack<V>- Returns:
- the number of elements
-
clear
public void clear()
Description copied from interface:ValueStackClears all values.- Specified by:
clearin interfaceValueStack<V>
-
takeSnapshot
public java.lang.Object takeSnapshot()
Description copied from interface:ValueStackReturns an object representing the current state of the stack. This cost of running this operation is negligible and independent from the size of the stack.- Specified by:
takeSnapshotin interfaceValueStack<V>- Returns:
- an object representing the current state of the stack
-
restoreSnapshot
public void restoreSnapshot(java.lang.Object snapshot)
Description copied from interface:ValueStackRestores the stack state as previously returned byValueStack.takeSnapshot(). This cost of running this operation is negligible and independent from the size of the stack.- Specified by:
restoreSnapshotin interfaceValueStack<V>- Parameters:
snapshot- a snapshot object previously returned byValueStack.takeSnapshot()
-
push
public void push(V value)
Description copied from interface:ValueStackPushes the given value onto the stack. Equivalent to push(0, value).- Specified by:
pushin interfaceValueStack<V>- Parameters:
value- the value
-
push
public void push(int down, V value)Description copied from interface:ValueStackInserts the given value a given number of elements below the current top of the stack.- Specified by:
pushin interfaceValueStack<V>- Parameters:
down- the number of elements to skip before inserting the value (0 being equivalent to push(value))value- the value
-
pushAll
public void pushAll(V firstValue, V... moreValues)
Description copied from interface:ValueStackPushes all given elements onto the stack (in the order as given).- Specified by:
pushAllin interfaceValueStack<V>- Parameters:
firstValue- the first valuemoreValues- the other values
-
pushAll
public void pushAll(java.lang.Iterable<V> values)
Description copied from interface:ValueStackPushes all given elements onto the stack (in the order as given).- Specified by:
pushAllin interfaceValueStack<V>- Parameters:
values- the values
-
pop
public V pop()
Description copied from interface:ValueStackRemoves the value at the top of the stack and returns it.- Specified by:
popin interfaceValueStack<V>- Returns:
- the current top value
-
pop
public V pop(int down)
Description copied from interface:ValueStackRemoves the value the given number of elements below the top of the stack.- Specified by:
popin interfaceValueStack<V>- Parameters:
down- the number of elements to skip before removing the value (0 being equivalent to pop())- Returns:
- the value
-
peek
public V peek()
Description copied from interface:ValueStackReturns the value at the top of the stack without removing it.- Specified by:
peekin interfaceValueStack<V>- Returns:
- the current top value
-
peek
public V peek(int down)
Description copied from interface:ValueStackReturns the value the given number of elements below the top of the stack without removing it.- Specified by:
peekin interfaceValueStack<V>- Parameters:
down- the number of elements to skip (0 being equivalent to peek())- Returns:
- the value
-
poke
public void poke(V value)
Description copied from interface:ValueStackReplaces the current top value with the given value. Equivalent to poke(0, value).- Specified by:
pokein interfaceValueStack<V>- Parameters:
value- the value
-
poke
public void poke(int down, V value)Description copied from interface:ValueStackReplaces the element the given number of elements below the current top of the stack.- Specified by:
pokein interfaceValueStack<V>- Parameters:
down- the number of elements to skip before replacing the value (0 being equivalent to poke(value))value- the value to replace with
-
dup
public void dup()
Description copied from interface:ValueStackDuplicates the top value. Equivalent to push(peek()).- Specified by:
dupin interfaceValueStack<V>
-
swap
public void swap()
Description copied from interface:ValueStackSwaps the top two stack values.- Specified by:
swapin interfaceValueStack<V>
-
swap3
public void swap3()
Description copied from interface:ValueStackReverses the order of the top 3 stack values.- Specified by:
swap3in interfaceValueStack<V>
-
swap4
public void swap4()
Description copied from interface:ValueStackReverses the order of the top 4 stack values.- Specified by:
swap4in interfaceValueStack<V>
-
swap5
public void swap5()
Description copied from interface:ValueStackReverses the order of the top 5 stack values.- Specified by:
swap5in interfaceValueStack<V>
-
swap6
public void swap6()
Description copied from interface:ValueStackReverses the order of the top 5 stack values.- Specified by:
swap6in interfaceValueStack<V>
-
-