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

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

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

@Participant(value="NonTerminalExpression")
public class VariableExpression<E>
extends Object
implements NonTerminalExpression<E>, TypedExpression<E>

A variable expression represents an expression that can be assigned a given value, which will be stored in a given context supplied at evaluation time.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
protected  boolean includeTypeInfo
          True if the type of this variable will be included in the symbolic representation of it, false if not.
 String name
          The unique name identifying this expression.
private  Class<E> type
          The type identifying the value returned by this expression.
 
Constructor Summary
VariableExpression(Class<E> type, String name)
          Constructor.
VariableExpression(Class<E> type, String name, boolean includeTypeInfo)
          Constructor.
VariableExpression(VariableExpression<E> expression)
          Copy constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns x, where x is the name of this variable, 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.
 VariableExpression<E> copy()
          Copies this expression.
 boolean equals(Object object)
          Returns true if object is an instance of this expression type and have the same name and type, false if not.
 E evaluate(Context context)
          Evaluates this expression and returns the result.
 int hashCode()
          Returns the hash code of this expression.
 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

includeTypeInfo

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

Default false.


name

public final String name
The unique name identifying this expression.

Names are case-sensitive.

Never null, nor empty.

This name is not the same as name()!

See Also:
Context.lookup(String)

type

private final Class<E> type
The type identifying the value returned by this expression.

Never null.

Constructor Detail

VariableExpression

public VariableExpression(Class<E> type,
                          String name)
Constructor.

The symbolic representation of this variable will not include type information regarding type.

Parameters:
type - The type of the value represented by this expression; cannot be null.
name - The name of this expression; cannot be null or empty.
Throws:
NullPointerException - If either argument is null.
IllegalArgumentException - If name is illegal.
See Also:
Context.lookup(String)

VariableExpression

public VariableExpression(Class<E> type,
                          String name,
                          boolean includeTypeInfo)
Constructor.

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

Parameters:
type - The type of the value represented by this expression; cannot be null.
name - The name of this expression; cannot be null or empty.
includeTypeInfo - True to include type information in the symbolic representation, false if not.
Throws:
NullPointerException - If either type or name are null.
IllegalArgumentException - If name is illegal.
See Also:
Context.lookup(String)

VariableExpression

public VariableExpression(VariableExpression<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)
Returns x, where x is the name of this variable, 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.
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 VariableExpression<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 TypedExpression<E>
Specified by:
copy in interface Copyable<Expression<E>>
Returns:
A copy of this expression; never null.

equals

public boolean equals(Object object)
Returns true if object is an instance of this expression type and have the same name and type, 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; never null.

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

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.