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

dk.rode.thesis.interpreter
Class Context

java.lang.Object
  extended by dk.rode.thesis.interpreter.Context
All Implemented Interfaces:
Iterable<VariableExpression<?>>

@Participant(value="Context")
public class Context
extends Object
implements Iterable<VariableExpression<?>>

A context represents an evaluation context where variables (and constants) are stored during interpretation.

A context aids in the generation of symbolic representations of expressions via the touch(Expression) and reset() methods.

A context is not thread-safe!

Implementation notes:
The context could easily be expanded to store any kind of TypedExpression, but the problem is what the associated value should be. A value (constant) or another expression, and what would be the point (replacement of expressions, perhaps)?

Author:
Gunni Rode / rode.dk
See Also:
Interpreter

Field Summary
private  Set<Expression<?>> touched
          A set storing currently touched expressions based on their identity.
private  Map<String,KeyValue<VariableExpression<?>,Object>> values
          The map storing the values.
 
Constructor Summary
Context()
          No-arg constructor.
Context(Context context)
          Copy constructor.
 
Method Summary
<E> E
assign(VariableExpression<E> expression, E value)
          Assigns the value supplied as value to the variable expression supplied as expression.
 Context clear()
          Clears all stored variables or touched expressions.
 boolean exist(String name)
          Returns true if a variable expression having the name supplied as name has been registered or has an associated value in this context, false if not.
 boolean exist(VariableExpression<?> expression)
          Returns true if the variable expression supplied as expression has been registered or has an associated value in this context, false if not.
<E> VariableExpression<E>
getVariable(Class<E> type, String name)
          Returns the variable or constant stored in this context associated with the type and name supplied as type and name, respectively.
 Set<VariableExpression<?>> getVariables()
          Returns a set containing all the contained variables and constants stored in this context, in no particular order.
<E> Set<VariableExpression<E>>
getVariables(Class<E> type)
          Returns a set containing all the contained variables and constants stored in this context, in no particular order, delivering the type of values supplied as type.
 boolean hasValue(String name)
          Returns true if a variable expression having the name supplied as name has an associated value in this context, false if not.
 boolean hasValue(VariableExpression<?> expression)
          Returns true if the variable expression supplied as expression has an associated value in this context, false if not.
 Iterator<VariableExpression<?>> iterator()
          Returns an iterator to iterate over all the contained variables and constants stored in this context, in no particular order.
 Object lookup(String name)
          Returns the value currently associated with the variable expression identified by the variable name supplied as name.
<E> E
lookup(VariableExpression<E> expression)
          Returns the value currently associated with the variable expression supplied as expression.
 Class<?> lookupType(String name)
          Returns the type of the value associated with the variable expression identified by the variable name supplied as name, or throws an ExpressionException in case no such value exist.
 void register(VariableExpression<?> expression)
          Registers the variable expression supplied as expression in this context with no associated value.
 Context reset()
          Resets the internal set of touched expressions.
 String toString()
          Returns the string representation of this context.
 boolean touch(Expression<?> expression)
          Touches the expression supplied as expression based on its identity and return false if it was the first time expression was touched since the last call to reset(), true otherwise.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

touched

private final Set<Expression<?>> touched
A set storing currently touched expressions based on their identity.

Never null, but can be empty.

See Also:
reset(), touch(Expression), Expression.asSymbol(Context)

values

private final Map<String,KeyValue<VariableExpression<?>,Object>> values
The map storing the values.

Format: (key, value) = (String, KeyValue<VariableExpression<X>,X>).

Keys are expression names.

If a variable/constant has only been registered, the associated value is null, and no value can be retrieved.

Never null.

Constructor Detail

Context

public Context()
No-arg constructor.


Context

public Context(Context context)
Copy constructor.

The set of touched expressions are not copied.

Parameters:
context - The context to copy; cannot be null.
Throws:
NullPointerException - if context is null.
Method Detail

assign

public <E> E assign(VariableExpression<E> expression,
                    E value)
         throws ExpressionException
Assigns the value supplied as value to the variable expression supplied as expression.

Type Parameters:
E - The type of value.
Parameters:
expression - The expression to assign value to; cannot be null.
value - The value to associate with expression; cannot be null.
Returns:
The old value associated with expression, or null.
Throws:
NullPointerException - If either argument is null.
ExpressionException - If the old value has an incompatible type compared to the type used by expression, or if expression represents a constant with a different value.

clear

public Context clear()
Clears all stored variables or touched expressions.

Returns:
This context; never null.

exist

public boolean exist(String name)
Returns true if a variable expression having the name supplied as name has been registered or has an associated value in this context, false if not.

Parameters:
name - The variable name; cannot be null.
Returns:
True if such an expression has an associated value in this context, false if not.
Throws:
NullPointerException - If name is null.
See Also:
exist(VariableExpression), lookup(String)

exist

public boolean exist(VariableExpression<?> expression)
Returns true if the variable expression supplied as expression has been registered or has an associated value in this context, false if not.

Parameters:
expression - The expression to test; cannot be null.
Returns:
True if expression has been registered or has an associated value in this context, false if not.
Throws:
NullPointerException - If expression is null.
See Also:
exist(String), lookup(VariableExpression)

getVariable

public <E> VariableExpression<E> getVariable(Class<E> type,
                                             String name)
                                  throws ExpressionException
Returns the variable or constant stored in this context associated with the type and name supplied as type and name, respectively.

A variable need only be registered to be retrieved with this method.

Type Parameters:
E - The type of value returned by the expression.
Parameters:
type - The type of value of the expression; cannot be null.
name - The name the expression; cannot be null.
Returns:
The matching expression; never null.
Throws:
NullPointerException - If either argument is null.
ExpressionException - If no such expression exist.

getVariables

public Set<VariableExpression<?>> getVariables()
Returns a set containing all the contained variables and constants stored in this context, in no particular order.

Modifying the returned set will not affect this context.

Returns:
A set containing the expressions; never null, but can be empty.
See Also:
iterator()

getVariables

public <E> Set<VariableExpression<E>> getVariables(Class<E> type)
Returns a set containing all the contained variables and constants stored in this context, in no particular order, delivering the type of values supplied as type.

Variables that are only registered are also included in the returned list.

Modifying the returned set will not affect this context.

Type Parameters:
E - The type of values returned by the expressions.
Parameters:
type - The type of values to be returned by the expressions; cannot be null.
Returns:
A set containing the expressions; never null, but can be empty.
Throws:
NullPointerException - If type is null.

hasValue

public boolean hasValue(String name)
Returns true if a variable expression having the name supplied as name has an associated value in this context, false if not. Variables that only have been registered does not have an associated value!

Parameters:
name - The variable name; cannot be null.
Returns:
True if expression has an associated value in this context, false if not.
Throws:
NullPointerException - If name is null.
See Also:
hasValue(VariableExpression), lookup(String)

hasValue

public boolean hasValue(VariableExpression<?> expression)
Returns true if the variable expression supplied as expression has an associated value in this context, false if not.

Variables that only have been registered does not have an associated value!

Parameters:
expression - The expression to test; cannot be null.
Returns:
True if expression has an associated value in this context, false if not.
Throws:
NullPointerException - If expression is null.
See Also:
hasValue(String), lookup(VariableExpression)

iterator

public Iterator<VariableExpression<?>> iterator()
Returns an iterator to iterate over all the contained variables and constants stored in this context, in no particular order.

Modifying the returned iterator will not affect this context.

Specified by:
iterator in interface Iterable<VariableExpression<?>>
Returns:
The iterator to iterate over the contained expressions; never null, but can be empty.
See Also:
getVariables()

lookup

public Object lookup(String name)
              throws ExpressionException
Returns the value currently associated with the variable expression identified by the variable name supplied as name.

A value must have been assigned to expression in this context or an ExpressionException will be thrown.

Parameters:
name - The variable name; cannot be null.
Returns:
The associated value; never null.
Throws:
NullPointerException - If name is null.
ExpressionException - If no such value exist.
See Also:
lookup(VariableExpression), hasValue(String)

lookup

public <E> E lookup(VariableExpression<E> expression)
         throws ExpressionException
Returns the value currently associated with the variable expression supplied as expression.

A value that has a type assignable to E must have been assigned to expression in this context or an ExpressionException will be thrown.

Type Parameters:
E - The type of value the evaluation of expression produces.
Parameters:
expression - The expression to find the value of; cannot be null.
Returns:
The associated value, having the proper type; never null.
Throws:
ExpressionException - If no such value exist, or if E does not match the type.
See Also:
lookup(String), hasValue(VariableExpression)

lookupType

public Class<?> lookupType(String name)
                    throws ExpressionException
Returns the type of the value associated with the variable expression identified by the variable name supplied as name, or throws an ExpressionException in case no such value exist.

A variable need only be registered for its type to be looked up.

Parameters:
name - The variable name; cannot be null.
Returns:
The type of the associated value; never null.
Throws:
NullPointerException - If name is null.
ExpressionException - If no such value exist.
See Also:
exist(String)

register

public void register(VariableExpression<?> expression)
              throws ExpressionException
Registers the variable expression supplied as expression in this context with no associated value.

Registered variables can tested for existence and type; can be retrieved using the getVariable(Class, String) method; but an associated value cannot be retrieved!

Once expression has been assigned a value, invoking this method again will cause an exception to be thrown. Registering expression several times is allowed.

Parameters:
expression - The expression to register; cannot be null.
Throws:
NullPointerException - If expression is null.
ExpressionException - If expression has an associated value.
See Also:
exist(String), exist(VariableExpression), lookupType(String), getVariable(Class, String)

reset

public Context reset()
Resets the internal set of touched expressions.

The actual calling context should always call this method before generating symbolic representations because cyclic expression references may occur.

Returns:
The context; never null.
See Also:
touch(Expression)

toString

public String toString()
Returns the string representation of this context.

Overrides:
toString in class Object
Returns:
The string representation; never null.

touch

public boolean touch(Expression<?> expression)
Touches the expression supplied as expression based on its identity and return false if it was the first time expression was touched since the last call to reset(), true otherwise.

Parameters:
expression - The expression to touch; cannot be null.
Returns:
False if this was the first time expression was touched, true if touched before.
See Also:
Expression.asSymbol(Context)

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.