|
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.Context
@Participant(value="Context") public class Context
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)?
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 | ||
---|---|---|
|
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. |
|
|
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. |
|
|
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 . |
|
|
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 |
---|
private final Set<Expression<?>> touched
Never null, but can be empty.
reset()
,
touch(Expression)
,
Expression.asSymbol(Context)
private final Map<String,KeyValue<VariableExpression<?>,Object>> 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 |
---|
public Context()
public Context(Context context)
The set of touched expressions are not copied.
context
- The context to copy; cannot be null.
NullPointerException
- if context
is null.Method Detail |
---|
public <E> E assign(VariableExpression<E> expression, E value) throws ExpressionException
value
to the
variable
expression supplied
as expression
.
E
- The type of value.expression
- The expression to assign value
to;
cannot be null.value
- The value to associate with expression
;
cannot be null.
expression
, or
null.
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.public Context clear()
touched
expressions.
public boolean exist(String name)
variable
expression having the name supplied as name
has been registered
or has an associated value
in this context, false if not.
name
- The variable name; cannot be null.
NullPointerException
- If name
is null.exist(VariableExpression)
,
lookup(String)
public boolean exist(VariableExpression<?> expression)
variable
expression supplied as expression
has been
registered
or has
an associated value
in this context, false if not.
expression
- The expression to test; cannot be null.
expression
has been registered or has
an associated value in this context, false if not.
NullPointerException
- If expression
is null.exist(String)
,
lookup(VariableExpression)
public <E> VariableExpression<E> getVariable(Class<E> type, String name) throws ExpressionException
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.
E
- The type of value returned by the expression.type
- The type of value of the expression; cannot be null.name
- The name the expression; cannot be null.
NullPointerException
- If either argument is null.
ExpressionException
- If no such expression exist.public Set<VariableExpression<?>> getVariables()
variables
and
constants
stored in
this context, in no particular order. Modifying the returned set will not affect this context.
iterator()
public <E> Set<VariableExpression<E>> getVariables(Class<E> type)
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.
E
- The type of values returned by the expressions.type
- The type of values to be returned by the
expressions; cannot be null.
NullPointerException
- If type
is null.public boolean hasValue(String name)
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!
name
- The variable name; cannot be null.
expression
has an associated value
in this context, false if not.
NullPointerException
- If name
is null.hasValue(VariableExpression)
,
lookup(String)
public boolean hasValue(VariableExpression<?> expression)
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!
expression
- The expression to test; cannot be null.
expression
has an associated value
in this context, false if not.
NullPointerException
- If expression
is null.hasValue(String)
,
lookup(VariableExpression)
public Iterator<VariableExpression<?>> iterator()
variables
and
constants
stored in
this context, in no particular order. Modifying the returned iterator will not affect this context.
iterator
in interface Iterable<VariableExpression<?>>
getVariables()
public Object lookup(String name) throws ExpressionException
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.
name
- The variable name; cannot be null.
NullPointerException
- If name
is null.
ExpressionException
- If no such value exist.lookup(VariableExpression)
,
hasValue(String)
public <E> E lookup(VariableExpression<E> expression) throws ExpressionException
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.
E
- The type of value the evaluation of
expression
produces.expression
- The expression to find the value of;
cannot be null.
ExpressionException
- If no such value exist, or if
E
does not match the type.lookup(String)
,
hasValue(VariableExpression)
public Class<?> lookupType(String name) throws ExpressionException
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.
name
- The variable name; cannot be null.
NullPointerException
- If name
is null.
ExpressionException
- If no such value exist.exist(String)
public void register(VariableExpression<?> expression) throws ExpressionException
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.
expression
- The expression to register; cannot be null.
NullPointerException
- If expression
is null.
ExpressionException
- If expression
has an associated
value.exist(String)
,
exist(VariableExpression)
,
lookupType(String)
,
getVariable(Class, String)
public Context reset()
The actual calling context should always call this method
before generating symbolic
representations because cyclic expression references may occur.
touch(Expression)
public String toString()
toString
in class Object
public boolean touch(Expression<?> expression)
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.
expression
- The expression to touch; cannot be null.
expression
was touched, true if touched before.Expression.asSymbol(Context)
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |