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

dk.rode.thesis.interpreter
Class SetExpression<E extends Comparable<? super E>>

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.SequenceExpression<E,E>
          extended by dk.rode.thesis.interpreter.SetExpression<E>
Type Parameters:
E - The type of value the evaluation of this expression produces. The value corresponds to the (super-) type of values delivered by the manipulated sequence.
All Implemented Interfaces:
Expression<E>, TerminalExpression<E>, Copyable<Expression<E>>, StrictCopyable<Expression<E>>

@Participant(value="TerminalExpression")
public class SetExpression<E extends Comparable<? super E>>
extends SequenceExpression<E,E>

A set expression will fast-forward the value of a given bounded sequence to match a specific value, if possible.

The value to fast-forward to can be a constant value, or supplied as an expression of type Expression<Comparable>. The evaluation will fail if no such value can be found at evaluation time!

Implementation notes:
The value is set by invoking Sequence.next() until a matching value is found, or until all sequence values have been traversed and a match cannot be found, in which case an exception is thrown. The worst-case running time is thus proportional to the number of values in the sequence, n, i.e. O(n).

Author:
Gunni Rode / rode.dk
See Also:
CurrentExpression, NextExpression, ResetExpression, ReverseExpression

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  Expression<E> value
          The comparable value to set for the sequence.
 
Fields inherited from class dk.rode.thesis.interpreter.SequenceExpression
includeHashCode, sequence
 
Constructor Summary
SetExpression(Sequence<? extends E> sequence, Class<E> type, E value)
          Constructor.
SetExpression(Sequence<? extends E> sequence, Class<E> type, E value, boolean includeHashCode)
          Constructor.
SetExpression(Sequence<? extends E> sequence, Expression<E> value)
          Constructor.
SetExpression(Sequence<? extends E> sequence, Expression<E> value, boolean includeHashCode)
          Constructor.
SetExpression(SetExpression<E> expression)
          Copy constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns set[x], where x is the symbolic representation of the expression that will determine the value, or set#123[x], where 123 is the identity hash code of the manipulated sequence.
 SetExpression<E> copy()
          Returns a deep copy of this object.
 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.SequenceExpression
equals, hashCode
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains
 
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

value

private final Expression<E extends Comparable<? super E>> value
The comparable value to set for the sequence.

Never null.

Constructor Detail

SetExpression

public SetExpression(Sequence<? extends E> sequence,
                     Class<E> type,
                     E value)
Constructor.

The value to fast-forward to is supplied as the constant value.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence manipulated by this expression; cannot be null.
type - The type of value; cannot be null.
value - The value to fast-forward to; cannot be null.
Throws:
NullPointerException - If either argument is null.
IllegalArgumentException - If sequence is not bounded.

SetExpression

public SetExpression(Sequence<? extends E> sequence,
                     Class<E> type,
                     E value,
                     boolean includeHashCode)
Constructor.

The value to fast-forward to is supplied as the constant value.

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence manipulated by this expression; cannot be null.
type - The type of value; cannot be null.
value - The value to fast-forward to; cannot be null.
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence, type, or value are null.
IllegalArgumentException - If sequence is not bounded.

SetExpression

public SetExpression(Sequence<? extends E> sequence,
                     Expression<E> value)
Constructor.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence manipulated by this expression; cannot be null.
value - The expression that will determine the value to fast-forward to for sequence; cannot be null.
Throws:
NullPointerException - If either argument is null.
IllegalArgumentException - If sequence is not bounded.

SetExpression

public SetExpression(Sequence<? extends E> sequence,
                     Expression<E> value,
                     boolean includeHashCode)
Constructor.

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence manipulated by this expression; cannot be null.
value - The expression that will determine the value to fast-forward to for sequence; cannot be null.
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence or value are null.
IllegalArgumentException - If sequence is not bounded.

SetExpression

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

The actual sequence manipulated by expression is not copied, but used as is.

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

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns set[x], where x is the symbolic representation of the expression that will determine the value, or set#123[x], where 123 is the identity hash code of the manipulated sequence.

Specified by:
asSymbol in interface Expression<E extends Comparable<? super E>>
Overrides:
asSymbol in class SequenceExpression<E extends Comparable<? super E>,E extends Comparable<? super 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 SetExpression<E> copy()
Description copied from interface: Copyable
Returns a deep copy of this object.

Returns:
The copy; 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!

Parameters:
context - The context to use; cannot be null.
Returns:
The result of the evaluation; never null.
Throws:
ExpressionException - If the value to fast-forward to cannot be matched to a sequence value.

name

public final String name()
Description copied from interface: Expression
The stand-alone symbol name for this expression.

Returns:
set

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 extends Comparable<? super E>>
Overrides:
operands in class SequenceExpression<E extends Comparable<? super E>,E extends Comparable<? super 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 extends Comparable<? super E>>
Overrides:
toString in class SequenceExpression<E extends Comparable<? super E>,E extends Comparable<? super 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.