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

dk.rode.thesis.builder
Class TypedFlowExpression<E>

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

public class TypedFlowExpression<E>
extends FlowExpression<E>
implements TypedExpression<E>

A typed flow expression represents one or more expressions to be evaluated in order, one at a time, where the type of value the evaluation of the expressions produces associated with it at runtime. 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.

Implementation notes:
This is an application of the Decorator and Adapter patterns.

As FlowExpression returns a FlowExpression type from several methods, these methods have to be overridden using covariant return types as not to loose the type information. This requires creating a lot of instances of this class, all using the same decorated flow expression.

Author:
Gunni Rode / rode.dk
See Also:
TypedExpressionBuilder

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  FlowExpression<E> expression
          The decorated flow expression.
private  Class<E> type
          A class literal representing the type of value the evaluation of expression produces.
 
Constructor Summary
TypedFlowExpression(Class<E> type, FlowExpression<E> expression)
          Constructor.
TypedFlowExpression(TypedFlowExpression<E> expression)
          Copy constructor.
 
Method Summary
 TypedFlowExpression<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.
 TypedFlowExpression<E> copy()
          Copies this expression.
 boolean equals(Object object)
          Returns true if object is a type flow expression using the same type and the same expression, false if not.
 E evaluate(Context context)
          Evaluates this expression and returns the result.
 int hashCode()
          Returns the hash code of this expression.
 TypedFlowExpression<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.
 List<Expression<?>> operands()
          Returns the expression operands used by this expression, in order.
 Class<E> type()
          Return the type of value the evaluation of this expression produces.
 
Methods inherited from class dk.rode.thesis.interpreter.FlowExpression
name
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains, 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, name, toString
 

Field Detail

expression

private final FlowExpression<E> expression
The decorated flow expression.

Never null, and never empty.


type

private final Class<E> type
A class literal representing the type of value the evaluation of expression produces.

Never null.

Constructor Detail

TypedFlowExpression

public TypedFlowExpression(Class<E> type,
                           FlowExpression<E> expression)
Constructor.

Parameters:
type - A class literal representing the type of value the evaluation of expression produces.
expression - The flow expression to use; cannot be null.
Throws:
NullPointerException - If either argument is null.

TypedFlowExpression

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

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

add

public TypedFlowExpression<E> add(Expression<? extends E> expression)
                           throws ExpressionException
Description copied from class: FlowExpression
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.

Overrides:
add in class FlowExpression<E>
Parameters:
expression - The expression to add; cannot be null.
Returns:
This flow expression; never null.
Throws:
ExpressionException - If this flow expression has already been initialised.

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Description copied from class: FlowExpression
Returns x, y, z, .., where x, y, z, .., are the symbol representations of the contained expressions.

Specified by:
asSymbol in interface Expression<E>
Overrides:
asSymbol in class FlowExpression<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 TypedFlowExpression<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 TypedExpression<E>
Specified by:
copy in interface Copyable<Expression<E>>
Overrides:
copy in class FlowExpression<E>
Returns:
A copy of this expression; never null.

equals

public boolean equals(Object object)
Returns true if object is a type flow expression using the same type and the same expression, false if not.

Overrides:
equals in class AbstractExpression<E>
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

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>
Overrides:
evaluate in class FlowExpression<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.

hashCode

public int hashCode()
Returns the hash code of this expression.

Overrides:
hashCode in class AbstractExpression<E>
Returns:
The hash code.

initialise

public TypedFlowExpression<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>
Overrides:
initialise in class FlowExpression<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>
Overrides:
isInitialised in class FlowExpression<E>
Returns:
True if initialised, false if not.

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

type

public Class<E> type()
Description copied from interface: TypedExpression
Return the type of value the evaluation of this expression produces.

Specified by:
type in interface TypedExpression<E>
Returns:
The type. The only expression type allowed to return null is BreakExpression!

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.