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

dk.rode.thesis.builder
Class AbstractExpressionBuilder<E>

java.lang.Object
  extended by dk.rode.thesis.builder.AbstractExpressionBuilder<E>
Type Parameters:
E - The type of value the evaluation of constructed expressions produces.
All Implemented Interfaces:
ExpressionBuilder<E>, Copyable<ExpressionBuilder<E>>, StrictCopyable<ExpressionBuilder<E>>
Direct Known Subclasses:
StandardExpressionBuilder, TypedExpressionBuilder

@Participant(value="Builder")
public abstract class AbstractExpressionBuilder<E>
extends Object
implements ExpressionBuilder<E>

An abstract expression builder implements the basic traits of the ExpressionBuilder interface, while ensuring that the constructed terminal expressions will all manipulate the same sequence instance.

Any builder inheriting this class furthermore uses a context to register all unique instances of constructed variables and store all created constants.

Implementation notes:
The initialiseExpressions(Expression) method is a visitation method used to visit all expressions in the expression syntax-tree. See also the Visitor pattern.

Author:
Gunni Rode / rode.dk

Field Summary
protected  Context context
          The context used to register created variables and store created constants.
protected  FlowExpression<E> root
          The root expression returned by getRootExpression().
protected  Sequence<? extends E> sequence
          The sequence manipulated by terminal expressions.
 
Constructor Summary
AbstractExpressionBuilder(AbstractExpressionBuilder<E> builder)
          Copy constructor.
AbstractExpressionBuilder(Context context, Sequence<? extends E> sequence)
          Constructor.
AbstractExpressionBuilder(Sequence<? extends E> sequence)
          Constructor.
 
Method Summary
<V> Expression<V>
buildConstantExpression(Class<V> type, V value)
          Builds a new CONSTANT expression.
 void buildExpression(Expression<? extends E> expression)
          Adds the constructed expression supplied as expression to the root expression constructed by this builder.
<V> VariableExpression<V>
buildVariableExpression(Class<V> type, String name)
          Builds a new VARIABLE expression.
 boolean equals(Object object)
          Returns true if object is an instance of this class and uses the same sequence as this builder, false if not.
 Context getContext()
          Return the context used by this builder.
 Expression<E> getRootExpression()
          Returns the root expression constructed by this builder.
 Sequence<? extends E> getSequence()
          Returns the sequence manipulated by the terminal expressions constructed by this builder.
 int hashCode()
          Returns the hash code of this builder.
protected static
<V> Expression<V>
initialiseExpressions(Expression<V> expression)
          Initialises all initialisable expressions reachable from the expression supplied as expression that has not already been initialised.
 String toString()
          Returns the string representation of this builder.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.builder.ExpressionBuilder
buildAndExpression, buildAssignmentExpression, buildBreakExpression, buildConditionalExpression, buildCurrentExpression, buildEqualExpression, buildFlowExpression, buildInitialisedFlowExpression, buildNextExpression, buildNonShortCircuitAndExpression, buildNonShortCircuitOrExpression, buildNotExpression, buildOrExpression, buildResetExpression, buildReverseExpression
 
Methods inherited from interface dk.rode.thesis.prototype.Copyable
copy
 

Field Detail

context

protected final Context context
The context used to register created variables and store created constants.

Never null.


root

protected final FlowExpression<E> root
The root expression returned by getRootExpression().

Never null.


sequence

protected final Sequence<? extends E> sequence
The sequence manipulated by terminal expressions.

Never null.

Constructor Detail

AbstractExpressionBuilder

public AbstractExpressionBuilder(AbstractExpressionBuilder<E> builder)
Copy constructor.

The same context and sequence as used by builder will be used by this builder.

The root expression from builder is not copied. Hence, this builder is ready to construct new expressions, and return a unique root expression from getRootExpression().

Parameters:
builder - The builder to copy; cannot be null.
Throws:
NullPointerException - If builder is null.

AbstractExpressionBuilder

public AbstractExpressionBuilder(Context context,
                                 Sequence<? extends E> sequence)
Constructor.

The context supplied as context is used to register created variables and store constants.

Parameters:
context - The context to use; cannot be null.
sequence - The sequence to be manipulated by constructed terminal expressions; cannot be null.
Throws:
NullPointerException - If either argument is null.
See Also:
getContext()

AbstractExpressionBuilder

public AbstractExpressionBuilder(Sequence<? extends E> sequence)
Constructor.

A local context is used to register created variables and store constants.

Parameters:
sequence - The sequence to be manipulated by constructed terminal expressions; cannot be null.
Throws:
NullPointerException - If sequence is null.
See Also:
getContext()
Method Detail

buildConstantExpression

public <V> Expression<V> buildConstantExpression(Class<V> type,
                                                 V value)
                                      throws ExpressionException
Description copied from interface: ExpressionBuilder
Builds a new CONSTANT expression.

Specified by:
buildConstantExpression in interface ExpressionBuilder<E>
Type Parameters:
V - The type of value the constructed expression produces.
Parameters:
type - The type of the constant represented by the constructed expression; cannot be null.
value - The value of the constant; cannot be null.
Returns:
The constructed expression; never null.
Throws:
ExpressionException - If the building fails.

buildExpression

public void buildExpression(Expression<? extends E> expression)
                     throws ExpressionException
Description copied from interface: ExpressionBuilder
Adds the constructed expression supplied as expression to the root expression constructed by this builder.

If expression is initialisable but not initialised, it will be initialised when the root expression is returned.

Specified by:
buildExpression in interface ExpressionBuilder<E>
Parameters:
expression - The expression to add; cannot be null.
Throws:
ExpressionException - If the adding fails in case the root expression has already been fetched.

buildVariableExpression

public <V> VariableExpression<V> buildVariableExpression(Class<V> type,
                                                         String name)
                                              throws ExpressionException
Description copied from interface: ExpressionBuilder
Builds a new VARIABLE expression.

Specified by:
buildVariableExpression in interface ExpressionBuilder<E>
Type Parameters:
V - The type of value the constructed expression produces.
Parameters:
type - The type of the value represented by the constructed expression; cannot be null.
name - The name of the constructed expression; cannot be null or empty.
Returns:
The constructed expression; never null.
Throws:
ExpressionException - If the building fails.

equals

public boolean equals(Object object)
Returns true if object is an instance of this class and uses the same sequence as this builder, false if not.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

getContext

public Context getContext()
Return the context used by this builder.

Returns:
The context; never null.

getRootExpression

public Expression<E> getRootExpression()
                                throws ExpressionException
Description copied from interface: ExpressionBuilder
Returns the root expression constructed by this builder.

Expressions are added to the root expression using the ExpressionBuilder.buildExpression(Expression) method. Once the root expression has been returned once, additional expressions cannot be added to it!

All initialisable expressions contained in the root expression will have been initialised, if not already.

Specified by:
getRootExpression in interface ExpressionBuilder<E>
Returns:
The root expression, completely initialised; never null.
Throws:
ExpressionException - If the initialisation fails.

getSequence

public Sequence<? extends E> getSequence()
Description copied from interface: ExpressionBuilder
Returns the sequence manipulated by the terminal expressions constructed by this builder.

Specified by:
getSequence in interface ExpressionBuilder<E>
Returns:
The sequence; never null.

hashCode

public int hashCode()
Returns the hash code of this builder.

Overrides:
hashCode in class Object
Returns:
The hash code.

initialiseExpressions

protected static <V> Expression<V> initialiseExpressions(Expression<V> expression)
                                              throws ExpressionException
Initialises all initialisable expressions reachable from the expression supplied as expression that has not already been initialised.

Type Parameters:
V - The type of value the evaluation of expression produces.
Parameters:
expression - The expression (root); cannot be null.
Returns:
expression; never null.
Throws:
NullPointerException - If expression is null.
ExpressionException - If the initialisation fails.

toString

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

Overrides:
toString in class Object
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.