Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.interpreter
Class NextExpression<E>

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.SequenceExpression<E,E>
          extended by dk.rode.thesis.interpreter.NextExpression<E>
Type Parameters:
E - The type of value the evaluation of this expression produces. The value corresponds to the (super-) type of values delivered by the manipulated sequence.
All Implemented Interfaces:
Expression<E>, TerminalExpression<E>, Copyable<Expression<E>>, StrictCopyable<Expression<E>>

@Participant(value="TerminalExpression")
public class NextExpression<E>
extends SequenceExpression<E,E>

A next expression will invoke next() a number of times on a given sequence when evaluated.

The number of times to invoke next() can be constant, but it can also be represented by any expression of the type Expression<? extends Number>, where the actual count is the absolute integer value of the result of the expression evaluation (if zero, a count of one is used).

Author:
Gunni Rode / rode.dk
See Also:
CurrentExpression, SetExpression, ResetExpression, ReverseExpression

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  Expression<? extends Number> count
          The number of times Sequence.next() is invoked for the manipulated sequence.
 
Fields inherited from class dk.rode.thesis.interpreter.SequenceExpression
includeHashCode, sequence
 
Constructor Summary
NextExpression(NextExpression<E> expression)
          Copy constructor.
NextExpression(Sequence<? extends E> sequence)
          Constructor, which will call Sequence.next() a single time on sequence when this expression is evaluated.
NextExpression(Sequence<? extends E> sequence, boolean includeHashCode)
          Constructor, which will call Sequence.next() a single time on sequence when this expression is evaluated.
NextExpression(Sequence<? extends E> sequence, Expression<? extends Number> count)
          Constructor, which will call Sequence.next() the number of times corresponding to the absolute value of the result of evaluating count when this expression is evaluated.
NextExpression(Sequence<? extends E> sequence, Expression<? extends Number> count, boolean includeHashCode)
          Constructor, which will call Sequence.next() the number of times corresponding to the absolute value of the result of evaluating count when this expression is evaluated.
NextExpression(Sequence<? extends E> sequence, int count)
          Constructor, which will call Sequence.next() count time(s) on sequence when this expression is evaluated.
NextExpression(Sequence<? extends E> sequence, int count, boolean includeHashCode)
          Constructor, which will call Sequence.next() count time(s) on sequence when this expression is evaluated.
 
Method Summary
 String asSymbol(Context context)
          Returns next[x], where x is the symbolic representation of the expression that will determine the number of times to invoke next(), or next#123[x], where 123 is the identity hash code of the manipulated sequence.
 NextExpression<E> copy()
          Returns a deep copy of this object.
 E evaluate(Context context)
          Evaluates this expression and returns the result.
 String name()
          The stand-alone symbol name for this expression.
 List<Expression<?>> operands()
          Returns the expression operands used by this expression, in order.
 String toString()
          Returns the string representation of this expression.
 
Methods inherited from class dk.rode.thesis.interpreter.SequenceExpression
equals, hashCode
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.interpreter.Expression
contains
 

Field Detail

count

private final Expression<? extends Number> count
The number of times Sequence.next() is invoked for the manipulated sequence.

The absolute integer value is used. If zero, a count of one is used.

Constructor Detail

NextExpression

public NextExpression(NextExpression<E> expression)
Copy constructor.

The actual sequence manipulated by expression is not copied, but used as is.

Parameters:
expression - The expression to copy; cannot be null.
Throws:
NullPointerException - If expression is null.

NextExpression

public NextExpression(Sequence<? extends E> sequence)
Constructor, which will call Sequence.next() a single time on sequence when this expression is evaluated.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
Throws:
NullPointerException - If sequence is null.

NextExpression

public NextExpression(Sequence<? extends E> sequence,
                      boolean includeHashCode)
Constructor, which will call Sequence.next() a single time on sequence when this expression is evaluated.

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence is null.

NextExpression

public NextExpression(Sequence<? extends E> sequence,
                      Expression<? extends Number> count)
Constructor, which will call Sequence.next() the number of times corresponding to the absolute value of the result of evaluating count when this expression is evaluated.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
count - The expression that will determine the number of times next() will be invoked on sequence when this expression is evaluated.
Throws:
NullPointerException - If either argument is null.

NextExpression

public NextExpression(Sequence<? extends E> sequence,
                      Expression<? extends Number> count,
                      boolean includeHashCode)
Constructor, which will call Sequence.next() the number of times corresponding to the absolute value of the result of evaluating count when this expression is evaluated.

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
count - The expression that will determine the number of times next() will be invoked on sequence when this expression is evaluated.
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence or count are null.

NextExpression

public NextExpression(Sequence<? extends E> sequence,
                      int count)
Constructor, which will call Sequence.next() count time(s) on sequence when this expression is evaluated.

The absolute value of count is used.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
count - The number of times to call next() on sequence when this expression is evaluated.
Throws:
NullPointerException - If sequence is null.

NextExpression

public NextExpression(Sequence<? extends E> sequence,
                      int count,
                      boolean includeHashCode)
Constructor, which will call Sequence.next() count time(s) on sequence when this expression is evaluated.

The absolute value of count is used.

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
count - The number of times to call next() on sequence when this expression is evaluated.
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence is null.
Method Detail

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns next[x], where x is the symbolic representation of the expression that will determine the number of times to invoke next(), or next#123[x], where 123 is the identity hash code of the manipulated sequence.

Specified by:
asSymbol in interface Expression<E>
Overrides:
asSymbol in class SequenceExpression<E,E>
Parameters:
context - The context to use; never null.
Returns:
The symbolic representation; never null.
Throws:
ExpressionException - If the symbol cannot be generated.
See Also:
Expression.contains(Expression)

copy

public NextExpression<E> copy()
Description copied from interface: Copyable
Returns a deep copy of this object.

Returns:
The copy; never null.

evaluate

public E evaluate(Context context)
           throws ExpressionException
Description copied from interface: Expression
Evaluates this expression and returns the result.

There is no guarantee that the evaluation of this expression will terminate!

Parameters:
context - The context to use; cannot be null.
Returns:
The result of the last invocation of Sequence.next(); never null.
Throws:
ExpressionException - If the evaluation fails.

name

public final String name()
Description copied from interface: Expression
The stand-alone symbol name for this expression.

Returns:
next

operands

public List<Expression<?>> operands()
Description copied from interface: Expression
Returns the expression operands used by this expression, in order.

Modifying the returned list will not affect this expression.

Specified by:
operands in interface Expression<E>
Overrides:
operands in class SequenceExpression<E,E>
Returns:
The operands; never null, but can be empty.

toString

public String toString()
Description copied from class: AbstractExpression
Returns the string representation of this expression.

Specified by:
toString in interface Expression<E>
Overrides:
toString in class SequenceExpression<E,E>
Returns:
The string representation; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.