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

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

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

public class AssignmentExpression<E>
extends AbstractExpression<E>
implements NonTerminalExpression<E>

An assignment expression assigns the result of the evaluation of an expression to a variable.

Implementation notes:
The assignment expression cannot inherit BinaryExpression because of the Expression<? extends E> bound on the first operand for any sub-class of it. Hence, it cannot be guaranteed to store a variable of type E, which is required when assigning the value to the context on evaluation.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  Expression<? extends E> expression
          The second operand.
private  VariableExpression<E> variable
          The variable expression to be assigned the value of expression.
 
Constructor Summary
AssignmentExpression(AssignmentExpression<E> expression)
          Copy constructor.
AssignmentExpression(VariableExpression<E> variable, Expression<? extends E> expression)
          Constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns x = y, where x is the symbolic representation of the first expression operand and y of the the second.
 AssignmentExpression<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.
 
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

expression

private final Expression<? extends E> expression
The second operand.

Never null.


variable

private final VariableExpression<E> variable
The variable expression to be assigned the value of expression.

Never null.

Constructor Detail

AssignmentExpression

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

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

AssignmentExpression

public AssignmentExpression(VariableExpression<E> variable,
                            Expression<? extends E> expression)
Constructor.

Parameters:
variable - The variable to be assigned the result of the evaluation of expression; cannot be null, or be a constant.
expression - The expression to deliver the variable value when evaluated; cannot be null.
Throws:
NullPointerException - If either argument is null.
IllegalArgumentException - If variable is a constant.
Method Detail

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns x = y, where x is the symbolic representation of the first expression operand and y of the the second.

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 AssignmentExpression<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 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 the evaluation fails.

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:
assignment

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 class: AbstractExpression
Returns the string representation of this expression.

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

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.