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:
Iterable<V>
,ValueStack<V>
- Direct Known Subclasses:
DebuggingValueStack
public class DefaultValueStack<V> extends 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 class
DefaultValueStack.Element
-
Field Summary
Fields Modifier and Type Field Description protected DefaultValueStack.Element
head
protected V
tempValue
-
Constructor Summary
Constructors Constructor Description DefaultValueStack()
Initializes an empty value stack.DefaultValueStack(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 void
clear()
Clears all values.void
dup()
Duplicates the top value.boolean
isEmpty()
Determines whether the stack is empty.Iterator<V>
iterator()
V
peek()
Returns the value at the top of the stack without removing it.V
peek(int down)
Returns the value the given number of elements below the top of the stack without removing it.void
poke(int down, V value)
Replaces the element the given number of elements below the current top of the stack.void
poke(V value)
Replaces the current top value with the given value.V
pop()
Removes the value at the top of the stack and returns it.V
pop(int down)
Removes the value the given number of elements below the top of the stack.void
push(int down, V value)
Inserts the given value a given number of elements below the current top of the stack.void
push(V value)
Pushes the given value onto the stack.void
pushAll(Iterable<V> values)
Pushes all given elements onto the stack (in the order as given).void
pushAll(V firstValue, V... moreValues)
Pushes all given elements onto the stack (in the order as given).void
restoreSnapshot(Object snapshot)
Restores the stack state as previously returned byValueStack.takeSnapshot()
.int
size()
Returns the number of elements currently on the stack.void
swap()
Swaps the top two stack values.void
swap3()
Reverses the order of the top 3 stack values.void
swap4()
Reverses the order of the top 4 stack values.void
swap5()
Reverses the order of the top 5 stack values.void
swap6()
Reverses the order of the top 5 stack values.Object
takeSnapshot()
Returns an object representing the current state of the stack.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
head
protected DefaultValueStack.Element head
-
tempValue
protected V tempValue
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Description copied from interface:ValueStack
Determines whether the stack is empty.- Specified by:
isEmpty
in interfaceValueStack<V>
- Returns:
- true if empty
-
size
public int size()
Description copied from interface:ValueStack
Returns the number of elements currently on the stack.- Specified by:
size
in interfaceValueStack<V>
- Returns:
- the number of elements
-
clear
public void clear()
Description copied from interface:ValueStack
Clears all values.- Specified by:
clear
in interfaceValueStack<V>
-
takeSnapshot
public Object takeSnapshot()
Description copied from interface:ValueStack
Returns 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:
takeSnapshot
in interfaceValueStack<V>
- Returns:
- an object representing the current state of the stack
-
restoreSnapshot
public void restoreSnapshot(Object snapshot)
Description copied from interface:ValueStack
Restores 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:
restoreSnapshot
in interfaceValueStack<V>
- Parameters:
snapshot
- a snapshot object previously returned byValueStack.takeSnapshot()
-
push
public void push(V value)
Description copied from interface:ValueStack
Pushes the given value onto the stack. Equivalent to push(0, value).- Specified by:
push
in interfaceValueStack<V>
- Parameters:
value
- the value
-
push
public void push(int down, V value)
Description copied from interface:ValueStack
Inserts the given value a given number of elements below the current top of the stack.- Specified by:
push
in 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:ValueStack
Pushes all given elements onto the stack (in the order as given).- Specified by:
pushAll
in interfaceValueStack<V>
- Parameters:
firstValue
- the first valuemoreValues
- the other values
-
pushAll
public void pushAll(Iterable<V> values)
Description copied from interface:ValueStack
Pushes all given elements onto the stack (in the order as given).- Specified by:
pushAll
in interfaceValueStack<V>
- Parameters:
values
- the values
-
pop
public V pop()
Description copied from interface:ValueStack
Removes the value at the top of the stack and returns it.- Specified by:
pop
in interfaceValueStack<V>
- Returns:
- the current top value
-
pop
public V pop(int down)
Description copied from interface:ValueStack
Removes the value the given number of elements below the top of the stack.- Specified by:
pop
in 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:ValueStack
Returns the value at the top of the stack without removing it.- Specified by:
peek
in interfaceValueStack<V>
- Returns:
- the current top value
-
peek
public V peek(int down)
Description copied from interface:ValueStack
Returns the value the given number of elements below the top of the stack without removing it.- Specified by:
peek
in 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:ValueStack
Replaces the current top value with the given value. Equivalent to poke(0, value).- Specified by:
poke
in interfaceValueStack<V>
- Parameters:
value
- the value
-
poke
public void poke(int down, V value)
Description copied from interface:ValueStack
Replaces the element the given number of elements below the current top of the stack.- Specified by:
poke
in 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:ValueStack
Duplicates the top value. Equivalent to push(peek()).- Specified by:
dup
in interfaceValueStack<V>
-
swap
public void swap()
Description copied from interface:ValueStack
Swaps the top two stack values.- Specified by:
swap
in interfaceValueStack<V>
-
swap3
public void swap3()
Description copied from interface:ValueStack
Reverses the order of the top 3 stack values.- Specified by:
swap3
in interfaceValueStack<V>
-
swap4
public void swap4()
Description copied from interface:ValueStack
Reverses the order of the top 4 stack values.- Specified by:
swap4
in interfaceValueStack<V>
-
swap5
public void swap5()
Description copied from interface:ValueStack
Reverses the order of the top 5 stack values.- Specified by:
swap5
in interfaceValueStack<V>
-
swap6
public void swap6()
Description copied from interface:ValueStack
Reverses the order of the top 5 stack values.- Specified by:
swap6
in interfaceValueStack<V>
-
-