|
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.BreakExpression<E>
E
- The type of value the evaluation of this expression
produces.@Participant(value="NonTerminalExpression") public class BreakExpression<E>
A break expression allows the interpretation
of a given expression syntax-tree to break at the point of the
break expression, and possibly continue the interpretation starting
from a specified target expression. Only a TypedExpression
instance can be the target.
The target expression cannot be a break expression unless explicitly
cast to a TypedExpression
, but a break expression can always
be contained in the target expression.
Implementation notes:
A break expression is a crude way to transfer evaluation control,
but quite effective for small languages though it depends on
an interpreter
. It utilises a
break
exception, which is caught and
processed by the interpreter. The break exception stores the
target expression to transfer control to, if any.
However, exception types cannot be generic, so the expression
to transfer control to must be declared using a wild-card in
the BreakException
type, i.e. TypedExpression<?>
.
Before the expression is interpreted, the interpreter will cast
it into the proper type, TypedExpression<E>
. This only
works because we only allow typed
expressions as the targets for break exceptions. Hence, we can test
on the actual type at runtime, but the compiler does not know this when
we perform the cast, and we therefore have to suppress warnings
explicitly.
Nested Class Summary | |
---|---|
static class |
BreakExpression.BreakException
A break exception is thrown when an break expression is evaluated. |
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression |
---|
Expression.SymbolIdiom |
Field Summary | |
---|---|
private TypedExpression<E> |
expression
The target expression to transfer control to, if any. |
Constructor Summary | |
---|---|
BreakExpression()
No-arg constructor. |
|
BreakExpression(BreakExpression<E> expression)
Copy constructor. |
|
BreakExpression(TypedExpression<E> expression)
Constructor. |
Method Summary | |
---|---|
String |
asSymbol(Context context)
Returns either exit or break[x] , where
x is the symbol representation of the target expression
used, if any. |
BreakExpression<E> |
copy()
Copies this expression. |
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. |
Class<E> |
type()
Return the type of value the evaluation of this expression produces. |
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression |
---|
contains, equals, hashCode |
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 |
---|
private final transient TypedExpression<E> expression
Can be null.
Constructor Detail |
---|
public BreakExpression()
When this expression is evaluated by an
interpreter
, the interpreter
will exit will a null value.
public BreakExpression(BreakExpression<E> expression)
Note, that if a break expression is to be the target
expression, it must be cast to a TypedExpression
and set using the BreakExpression(TypedExpression)
constructor. This constructor is a copy constructor that
simply copies expression
.
expression
- The expression to copy; cannot be null.
NullPointerException
- If expression
is null.public BreakExpression(TypedExpression<E> expression)
When this expression is evaluated by an
interpreter
, the interpreter
will transfer control to expression
, if
not null, and return the result of that evaluation.
If null, the interpreter will exit.
expression
- The target expression to use; can be null.Method Detail |
---|
public String asSymbol(Context context) throws ExpressionException
exit
or break[x]
, where
x
is the symbol representation of the target expression
used, if any.
asSymbol
in interface Expression<E>
context
- The context to use; never null.
ExpressionException
- If the symbol cannot be generated.Expression.contains(Expression)
public BreakExpression<E> copy()
Expression
Unlike Expression.asSymbol(Context)
, copying
cannot handle cyclic expression references!
copy
in interface Expression<E>
copy
in interface TypedExpression<E>
copy
in interface Copyable<Expression<E>>
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>
context
- The context to use; cannot be null.
ExpressionException
- If the evaluation fails.public final String name()
Expression
name
in interface Expression<E>
exit
or break
.public List<Expression<?>> operands()
Expression
Modifying the returned list will not affect this expression.
operands
in interface Expression<E>
public String toString()
AbstractExpression
toString
in interface Expression<E>
toString
in class AbstractExpression<E>
public final Class<E> type()
TypedExpression
type
in interface TypedExpression<E>
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |