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

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

java.lang.Object
  extended by dk.rode.thesis.interpreter.VariableExpression<E>
      extended by dk.rode.thesis.interpreter.ConstantExpression<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>>

@Participant(value="NonTerminalExpression")
public class ConstantExpression<E>
extends VariableExpression<E>

A constant expression represents an expression that can be assigned a constant value.

Constants can be stored in a context as well, but this is not a requirement.

This class does not declare a copy constructor, and will when copied return the same instance since the value is constant.

Implementation notes:
The auto-generated name of a constant ensures that two constants of the same type, E, having the same actual value will also have the same name. Hence, look-up of different constant instances in a Context will result in a match if same type and value; equals(Object) works in a similar manner. However, this also means that a constant cannot be assigned a value during evaluation, not even once, because the name depends on the value. Trying to create an assignment expression using a constant as the target of the evaluation is an error, and will cause an exception to be thrown at construction time.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
 E value
          The value of this constant.
 
Fields inherited from class dk.rode.thesis.interpreter.VariableExpression
includeTypeInfo, name
 
Constructor Summary
ConstantExpression(Class<E> type, E value)
          Constructor.
ConstantExpression(Class<E> type, E value, boolean includeTypeInfo)
          Constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns x, where x is the value of this constant, or if type information is included, Type{x}, where Type is the simple name of the type.
 ConstantExpression<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.
 
Methods inherited from class dk.rode.thesis.interpreter.VariableExpression
contains, equals, hashCode, operands, toString, type
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

value

public final E value
The value of this constant.

Never null.

Constructor Detail

ConstantExpression

public ConstantExpression(Class<E> type,
                          E value)
Constructor.

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

Parameters:
type - The type of the value this expression represents; cannot be null.
value - The constant value; cannot be null.
Throws:
NullPointerException - If either argument is null.

ConstantExpression

public ConstantExpression(Class<E> type,
                          E value,
                          boolean includeTypeInfo)
Constructor.

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

Parameters:
type - The type of the value this expression represents; cannot be null.
value - The constant value; cannot be null.
includeTypeInfo - True to include type information in the symbolic representation, false if not.
Throws:
NullPointerException - If either argument is null.
Method Detail

asSymbol

public String asSymbol(Context context)
Returns x, where x is the value of this constant, or if type information is included, Type{x}, where Type is the simple name of the type.

Specified by:
asSymbol in interface Expression<E>
Overrides:
asSymbol in class VariableExpression<E>
Parameters:
context - The context to use; never null.
Returns:
The symbolic representation; never null.
See Also:
Expression.contains(Expression)

copy

public ConstantExpression<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>>
Overrides:
copy in class VariableExpression<E>
Returns:
A copy of this expression; never null.

evaluate

public E evaluate(Context context)
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>
Overrides:
evaluate in class VariableExpression<E>
Parameters:
context - The context to use; cannot be null.
Returns:
The result of the evaluation; never null.

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>
Overrides:
name in class VariableExpression<E>
Returns:
constant

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.