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

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

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.FlowExpression<E>
Type Parameters:
E - The type of value the evaluation of this expression produces.
All Implemented Interfaces:
Expression<E>, InitialisableExpression<E>, NonTerminalExpression<E>, Copyable<Expression<E>>, StrictCopyable<Expression<E>>
Direct Known Subclasses:
TypedFlowExpression

@Participant(value="NonTerminalExpression")
public class FlowExpression<E>
extends AbstractExpression<E>
implements NonTerminalExpression<E>, InitialisableExpression<E>

A flow expression represents one or more expressions to be evaluated in order, one at a time. The result of the evaluation is the result of the last expression evaluated.

Expressions to be evaluated by a flow expression must explicitly be added. Once the last expression has been added, the flow expression must be initialised before evaluation, or the evaluation will fail. Once initialised, additional expressions cannot be added.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  List<Expression<? extends E>> expressions
          The expressions to evaluate, in order.
private  boolean initialised
          True if this expression has been initialised, false if not.
 
Constructor Summary
FlowExpression()
          No-arg constructor.
FlowExpression(FlowExpression<E> expression)
          Copy constructor.
 
Method Summary
 FlowExpression<E> add(Expression<? extends E> expression)
          Adds the expression supplied as expression to this flow expression.
 String asSymbol(Context context)
          Returns x, y, z, .., where x, y, z, .., are the symbol representations of the contained expressions.
 FlowExpression<E> copy()
          Copies this expression.
 E evaluate(Context context)
          Evaluates this expression and returns the result.
 FlowExpression<E> initialise()
          Initialises this expression before evaluation.
 boolean isInitialised()
          Returns true if this expression has been initialised, and hence ready to be evaluated, false if not.
 String name()
          The stand-alone symbol name for this expression.
 List<Expression<?>> operands()
          Returns the expression operands used by this expression, in order.
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains, equals, hashCode, toString
 
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, toString
 

Field Detail

expressions

private final List<Expression<? extends E>> expressions
The expressions to evaluate, in order.

Never null, and never empty.


initialised

private boolean initialised
True if this expression has been initialised, false if not.

Constructor Detail

FlowExpression

public FlowExpression()
No-arg constructor.

Expressions to be evaluated must be added before evaluation or the evaluation of this expression will fail.


FlowExpression

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

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

add

public FlowExpression<E> add(Expression<? extends E> expression)
                      throws ExpressionException
Adds the expression supplied as expression to this flow expression.

The same expression can be added several times.

After the last expression has been added, this flow expression must be initialised before it can be evaluated.

Parameters:
expression - The expression to add; cannot be null.
Returns:
This flow expression; never null.
Throws:
NullPointerException - If expression is null.
ExpressionException - If this flow expression has already been initialised.

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns x, y, z, .., where x, y, z, .., are the symbol representations of the contained expressions.

Specified by:
asSymbol in interface Expression<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 FlowExpression<E> copy()
Description copied from interface: Expression
Copies this expression.

Unlike Expression.asSymbol(Context), copying cannot handle cyclic expression references!

Specified by:
copy in interface Expression<E>
Specified by:
copy in interface InitialisableExpression<E>
Specified by:
copy in interface Copyable<Expression<E>>
Returns:
A copy of this expression; 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!

Specified by:
evaluate in interface Expression<E>
Parameters:
context - The context to use; cannot be null.
Returns:
The result of the evaluation; never null.
Throws:
ExpressionException - If this expression has not been initialised, or if the evaluation fails.

initialise

public FlowExpression<E> initialise()
                             throws ExpressionException
Description copied from interface: InitialisableExpression
Initialises this expression before evaluation.

Contained initialisable expressions are not initialised!

Specified by:
initialise in interface InitialisableExpression<E>
Returns:
This expression; never null.
Throws:
ExpressionException - If this expression has already been initialised.

isInitialised

public boolean isInitialised()
Description copied from interface: InitialisableExpression
Returns true if this expression has been initialised, and hence ready to be evaluated, false if not.

If this expression has not been initialised when evaluated, the evaluation will throw an exception.

Specified by:
isInitialised in interface InitialisableExpression<E>
Returns:
True if initialised, false if not.

name

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

Specified by:
name in interface Expression<E>
Returns:
flow

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>
Returns:
The expressions contained in this expression.

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.