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

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

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

public class TypedExpressionDecorator<E>
extends Object
implements TypedExpression<E>, InitialisableExpression<E>

A type expression decorator allows any expression to be explicitly associated with the type of the values the evaluation of it will produce.

In case the decorated expression is initialisable, it will be initialised by the initialise() method defined in this decorator. If the decorated expression is not initialisable, the initialise() and isInitialised() methods does nothing.

Implementation notes:
This is a fine example of the Decorator pattern at work.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
protected  Expression<E> expression
          The decorated expression.
protected  boolean includeTypeInfo
          True if the evaluation type of expression will be included in the symbolic representation of it, false if not.
protected  Class<E> type
          The type of values the evaluation of expression produces.
 
Constructor Summary
TypedExpressionDecorator(Expression<E> expression, Class<E> type)
          Constructor, which decorates the expression supplied as expression having the type supplied as type.
TypedExpressionDecorator(Expression<E> expression, Class<E> type, boolean includeTypeInfo)
          Constructor, which decorates the expression supplied as expression having the type supplied as type.
TypedExpressionDecorator(TypedExpression<E> expression)
          Constructor, which decorates the typed expression supplied as expression.
TypedExpressionDecorator(TypedExpressionDecorator<E> expression)
          Copy constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns x, where x is the symbolic representation of the decorated expression, or if type information is included, Type{x}, where Type is the simple name of the type.
 boolean contains(Expression<?> expression)
          Returns true if this expression is or contains the expression supplied as expression, false if not.
 TypedExpressionDecorator<E> copy()
          Copies this typed expression.
 boolean equals(Object object)
          Returns true if object is a typed expression decorator decorating the same expressions as this decorator, tested using equals(Object), false if not.
 E evaluate(Context context)
          Evaluates this expression and returns the result.
 int hashCode()
          Returns the hash code of this expression.
 TypedExpressionDecorator<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.
 String toString()
          Returns the string representation of this expression.
 Class<E> type()
          Return the type of value the evaluation of this expression produces.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

expression

protected final Expression<E> expression
The decorated expression.

Never null.


includeTypeInfo

protected final boolean includeTypeInfo
True if the evaluation type of expression will be included in the symbolic representation of it, false if not.

Default false.


type

protected final Class<E> type
The type of values the evaluation of expression produces.

Never null.

Constructor Detail

TypedExpressionDecorator

public TypedExpressionDecorator(Expression<E> expression,
                                Class<E> type)
Constructor, which decorates the expression supplied as expression having the type supplied as type.

The symbolic representation of expression will include type information regarding type.

Parameters:
expression - The expression to decorate; cannot be null.
type - The actual type; cannot be null.
Throws:
NullPointerException - If either argument is null.

TypedExpressionDecorator

public TypedExpressionDecorator(Expression<E> expression,
                                Class<E> type,
                                boolean includeTypeInfo)
Constructor, which decorates the expression supplied as expression having the type supplied as type.

The symbolic representation of expression will include type information regarding type if includeTypeInfo is true, not if false.

Parameters:
expression - The expression to decorate; cannot be null.
type - The type of values the evaluation of expression produces.
includeTypeInfo - True to include type information in the symbolic representation of expression, false if not.
Throws:
NullPointerException - If expression or type is null.

TypedExpressionDecorator

public TypedExpressionDecorator(TypedExpression<E> expression)
Constructor, which decorates the typed expression supplied as expression.

The symbolic representation of expression will include type information regarding the type of expression.

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

TypedExpressionDecorator

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

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

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns x, where x is the symbolic representation of the decorated expression, or if type information is included, Type{x}, where Type is the simple name of the type.

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)

contains

public boolean contains(Expression<?> expression)
Description copied from interface: Expression
Returns true if this expression is or contains the expression supplied as expression, false if not.

The test is performed using identity (==).

Unlike Expression.asSymbol(Context), querying for containment cannot handle cyclic expression references!

Specified by:
contains in interface Expression<E>
Parameters:
expression - The expression to test; cannot be null.
Returns:
True if expression is this expression or contained within this expression, false if not.
See Also:
Expression.asSymbol(Context), Context.touch(Expression)

copy

public TypedExpressionDecorator<E> copy()
Description copied from interface: TypedExpression
Copies this typed 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>>
Returns:
A copy of this typed expression; never null.

equals

public boolean equals(Object object)
Returns true if object is a typed expression decorator decorating the same expressions as this decorator, tested using equals(Object), false if not.

Overrides:
equals in class Object
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>
Parameters:
context - The context to use; cannot be null.
Returns:
The result of the evaluation; never null.
Throws:
ExpressionException - If the evaluation fails.

hashCode

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

Overrides:
hashCode in class Object
Returns:
The hash code.

initialise

public TypedExpressionDecorator<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 String name()
Description copied from interface: Expression
The stand-alone symbol name for this expression.

Specified by:
name in interface Expression<E>
Returns:
The symbol name; never null.

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 operands; never null, but can be empty.

toString

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

Unlike Expression.asSymbol(Context), the string generation cannot handle cyclic expression references!

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

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.