|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.interpreter.AbstractExpression<E> dk.rode.thesis.interpreter.FlowExpression<E> dk.rode.thesis.builder.TypedFlowExpression<E>
E
- The type of value the evaluation of this expression
produces.public class TypedFlowExpression<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.
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 |
---|
private final FlowExpression<E> expression
Never null, and never empty.
private final Class<E> type
expression
produces. Never null.
Constructor Detail |
---|
public TypedFlowExpression(Class<E> type, FlowExpression<E> expression)
type
- A class literal representing the type of value the
evaluation of expression
produces.expression
- The flow expression to use; cannot be null.
NullPointerException
- If either argument is null.public TypedFlowExpression(TypedFlowExpression<E> expression)
expression
- The expression to copy; cannot be null.
NullPointerException
- If expression
is null.Method Detail |
---|
public TypedFlowExpression<E> add(Expression<? extends E> expression) throws ExpressionException
FlowExpression
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.
add
in class FlowExpression<E>
expression
- The expression to add; cannot be null.
ExpressionException
- If this flow expression has already
been initialised.public String asSymbol(Context context) throws ExpressionException
FlowExpression
x, y, z, ..
, where x
, y
,
z
, .., are the symbol representations of the
contained expressions.
asSymbol
in interface Expression<E>
asSymbol
in class FlowExpression<E>
context
- The context to use; never null.
ExpressionException
- If the symbol cannot be generated.Expression.contains(Expression)
public TypedFlowExpression<E> copy()
Expression
Unlike Expression.asSymbol(Context)
, copying
cannot handle cyclic expression references!
copy
in interface Expression<E>
copy
in interface InitialisableExpression<E>
copy
in interface TypedExpression<E>
copy
in interface Copyable<Expression<E>>
copy
in class FlowExpression<E>
public boolean equals(Object object)
object
is a type flow expression using
the same type and the same expression, false if not.
equals
in class AbstractExpression<E>
object
- The object to test; can be null.
public E evaluate(Context context) throws ExpressionException
Expression
There is no guarantee that the evaluation of this expression will terminate!
evaluate
in interface Expression<E>
evaluate
in class FlowExpression<E>
context
- The context to use; cannot be null.
ExpressionException
- If this expression has not been
initialised, or if the evaluation fails.public int hashCode()
hashCode
in class AbstractExpression<E>
public TypedFlowExpression<E> initialise() throws ExpressionException
InitialisableExpression
Contained initialisable expressions are not initialised!
initialise
in interface InitialisableExpression<E>
initialise
in class FlowExpression<E>
ExpressionException
- If this expression has already
been initialised.public boolean isInitialised()
InitialisableExpression
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.
isInitialised
in interface InitialisableExpression<E>
isInitialised
in class FlowExpression<E>
public List<Expression<?>> operands()
Expression
Modifying the returned list will not affect this expression.
operands
in interface Expression<E>
operands
in class FlowExpression<E>
public Class<E> type()
TypedExpression
type
in interface TypedExpression<E>
BreakExpression
!
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |