org.parboiled.support
Class DefaultValueStack<V>

java.lang.Object
  extended by 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 methods takeSnapshot() and restoreSnapshot(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
protected static class DefaultValueStack.Element
           
 
Field Summary
protected  DefaultValueStack.Element head
           
protected  V tempValue
           
 
Constructor Summary
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
 void clear()
          Clears all values.
 void dup()
          Duplicates the top value.
 boolean isEmpty()
          Determines whether the stack is empty.
 java.util.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(java.lang.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(java.lang.Object snapshot)
          Restores the stack state as previously returned by ValueStack.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.
 java.lang.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
 

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: ValueStack
Determines whether the stack is empty.

Specified by:
isEmpty in interface ValueStack<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 interface ValueStack<V>
Returns:
the number of elements

clear

public void clear()
Description copied from interface: ValueStack
Clears all values.

Specified by:
clear in interface ValueStack<V>

takeSnapshot

public java.lang.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 interface ValueStack<V>
Returns:
an object representing the current state of the stack

restoreSnapshot

public void restoreSnapshot(java.lang.Object snapshot)
Description copied from interface: ValueStack
Restores the stack state as previously returned by ValueStack.takeSnapshot(). This cost of running this operation is negligible and independent from the size of the stack.

Specified by:
restoreSnapshot in interface ValueStack<V>
Parameters:
snapshot - a snapshot object previously returned by ValueStack.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 interface ValueStack<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 interface ValueStack<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 interface ValueStack<V>
Parameters:
firstValue - the first value
moreValues - the other values

pushAll

public void pushAll(java.lang.Iterable<V> values)
Description copied from interface: ValueStack
Pushes all given elements onto the stack (in the order as given).

Specified by:
pushAll in interface ValueStack<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 interface ValueStack<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 interface ValueStack<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 interface ValueStack<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 interface ValueStack<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 interface ValueStack<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 interface ValueStack<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 interface ValueStack<V>

swap

public void swap()
Description copied from interface: ValueStack
Swaps the top two stack values.

Specified by:
swap in interface ValueStack<V>

swap3

public void swap3()
Description copied from interface: ValueStack
Reverses the order of the top 3 stack values.

Specified by:
swap3 in interface ValueStack<V>

swap4

public void swap4()
Description copied from interface: ValueStack
Reverses the order of the top 4 stack values.

Specified by:
swap4 in interface ValueStack<V>

swap5

public void swap5()
Description copied from interface: ValueStack
Reverses the order of the top 5 stack values.

Specified by:
swap5 in interface ValueStack<V>

swap6

public void swap6()
Description copied from interface: ValueStack
Reverses the order of the top 5 stack values.

Specified by:
swap6 in interface ValueStack<V>

iterator

public java.util.Iterator<V> iterator()
Specified by:
iterator in interface java.lang.Iterable<V>