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

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

java.lang.Object
  extended by dk.rode.thesis.builder.AbstractExpressionBuilder<E>
      extended by dk.rode.thesis.builder.StandardExpressionBuilder<E>
          extended by dk.rode.thesis.builder.CountingExpressionBuilder<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:
CountingComparableExpressionBuilder

@Participant(value="ConcreteBuilder")
public class CountingExpressionBuilder<E>
extends StandardExpressionBuilder<E>

A counting expression builder performs the same functionality as a standard expression builder, but also registers the number of times each expression type is created based on expression names.

The count per expression name is stored in the context used, prefixed with count., for example count.next for NextExpression. The counts can also be directly fetched using the getExpressionCount() method.

Implementation notes:
Due to inheritance of the StandardExpressionBuilder, covariant return types of the precise types of created expressions cannot be made without casting to explicit types, for example to NextExpression. This is not always possible if the implementing types are hidden.

The counts per expression type could easily have been stored in a map instead, but by using the context, we at the same time utilise the functionality exposed by it.

Counting functionality inspired by the Gamma et al. example in [Gamma95, p.104].

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

Field Summary
static String PREFIX
          The prefix used for all variables storing a count for a given expression type.
 
Fields inherited from class dk.rode.thesis.builder.AbstractExpressionBuilder
context, root, sequence
 
Constructor Summary
CountingExpressionBuilder(Context context, Sequence<? extends E> sequence)
          Constructor.
CountingExpressionBuilder(CountingExpressionBuilder<E> builder)
          Copy constructor.
CountingExpressionBuilder(Sequence<? extends E> sequence)
          Constructor.
 
Method Summary
 Expression<Boolean> buildAndExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new short-circuit AND expression.
 Expression<E> buildAssignmentExpression(VariableExpression<E> variable, Expression<? extends E> expression)
          Builds a new ASSIGNMENT expression.
 Expression<E> buildBreakExpression(TypedExpression<E> expression)
          Builds a new BREAK expression.
 Expression<E> buildConditionalExpression(Expression<Boolean> condition, Expression<? extends E> first, Expression<? extends E> second)
          Builds a new CONDITIONAL expression.
<V> Expression<V>
buildConstantExpression(Class<V> type, V value)
          Builds a new CONSTANT expression.
 Expression<E> buildCurrentExpression()
          Builds a new CURRENT expression.
 Expression<Boolean> buildEqualExpression(Expression<?> first, Expression<?> second)
          Builds a new EQUAL expression.
 FlowExpression<E> buildFlowExpression()
          Builds a new uninitialised FLOW expression.
 Expression<E> buildInitialisedFlowExpression(Expression<? extends E>... expressions)
          Builds a new initialised FLOW expression.
 Expression<E> buildNextExpression(Expression<? extends Number> count)
          Builds a new NEXT expression.
 Expression<Boolean> buildNonShortCircuitAndExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new non short-circuit AND expression.
 Expression<Boolean> buildNonShortCircuitOrExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new non short-circuit OR expression.
 Expression<Boolean> buildNotExpression(Expression<Boolean> expression)
          Builds a new NOT expression.
 Expression<Boolean> buildOrExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new short-circuit OR expression.
 Expression<E> buildResetExpression()
          Builds a new RESET expression.
 Expression<Boolean> buildReverseExpression(Expression<Boolean> reverse)
          Builds a new REVERSE expression.
<V> VariableExpression<V>
buildVariableExpression(Class<V> type, String name)
          Builds a new VARIABLE expression.
 CountingExpressionBuilder<E> copy()
          Returns a deep copy of this object.
protected
<V,W extends Expression<V>>
W
count(W expression)
          Updates the number of times this builder has seen an expression with the same name as expression.
 Map<String,Integer> getExpressionCount()
          Returns a map containing as values the number of times a given expression type has been created, excluding the count for the root expression.
 
Methods inherited from class dk.rode.thesis.builder.AbstractExpressionBuilder
buildExpression, equals, getContext, getRootExpression, getSequence, hashCode, initialiseExpressions, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.builder.ExpressionBuilder
buildExpression, getRootExpression, getSequence
 

Field Detail

PREFIX

public static final String PREFIX
The prefix used for all variables storing a count for a given expression type.

The value is count., including the dot (.).

See Also:
Constant Field Values
Constructor Detail

CountingExpressionBuilder

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

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

Note, that in case context has variables registered with names prefixed with PREFIX, this may affect the counting performed by this builder (see the class documentation)!

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:
AbstractExpressionBuilder.getContext()

CountingExpressionBuilder

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

The same sequence as used by builder will be used by this builder, but a new context will be created and used. Hence, all counts are cleared.

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

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

CountingExpressionBuilder

public CountingExpressionBuilder(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:
AbstractExpressionBuilder.getContext()
Method Detail

buildAndExpression

public Expression<Boolean> buildAndExpression(Expression<Boolean> first,
                                              Expression<Boolean> second)
Description copied from interface: ExpressionBuilder
Builds a new short-circuit AND expression.

Specified by:
buildAndExpression in interface ExpressionBuilder<E>
Overrides:
buildAndExpression in class StandardExpressionBuilder<E>
Parameters:
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Returns:
The constructed expression; never null.

buildAssignmentExpression

public Expression<E> buildAssignmentExpression(VariableExpression<E> variable,
                                               Expression<? extends E> expression)
Description copied from interface: ExpressionBuilder
Builds a new ASSIGNMENT expression.

Specified by:
buildAssignmentExpression in interface ExpressionBuilder<E>
Overrides:
buildAssignmentExpression in class StandardExpressionBuilder<E>
Parameters:
variable - The variable to be assigned the result of the evaluation of expression; cannot be null, or be a constant.
expression - The expression to deliver the variable value when evaluated; cannot be null.
Returns:
The constructed expression; never null.

buildBreakExpression

public Expression<E> buildBreakExpression(TypedExpression<E> expression)
Description copied from interface: ExpressionBuilder
Builds a new BREAK expression.

Specified by:
buildBreakExpression in interface ExpressionBuilder<E>
Overrides:
buildBreakExpression in class StandardExpressionBuilder<E>
Parameters:
expression - The target expression, if any; can be null.
Returns:
The constructed expression; never null.

buildConditionalExpression

public Expression<E> buildConditionalExpression(Expression<Boolean> condition,
                                                Expression<? extends E> first,
                                                Expression<? extends E> second)
Description copied from interface: ExpressionBuilder
Builds a new CONDITIONAL expression.

Specified by:
buildConditionalExpression in interface ExpressionBuilder<E>
Overrides:
buildConditionalExpression in class StandardExpressionBuilder<E>
Parameters:
condition - The condition expression; cannot be null.
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Returns:
The constructed expression; never null.

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>
Overrides:
buildConstantExpression in class AbstractExpressionBuilder<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.

buildCurrentExpression

public Expression<E> buildCurrentExpression()
Description copied from interface: ExpressionBuilder
Builds a new CURRENT expression.

Specified by:
buildCurrentExpression in interface ExpressionBuilder<E>
Overrides:
buildCurrentExpression in class StandardExpressionBuilder<E>
Returns:
The constructed expression; never null.

buildEqualExpression

public Expression<Boolean> buildEqualExpression(Expression<?> first,
                                                Expression<?> second)
Description copied from interface: ExpressionBuilder
Builds a new EQUAL expression.

Specified by:
buildEqualExpression in interface ExpressionBuilder<E>
Overrides:
buildEqualExpression in class StandardExpressionBuilder<E>
Parameters:
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Returns:
The constructed expression; never null.

buildFlowExpression

public FlowExpression<E> buildFlowExpression()
Description copied from interface: ExpressionBuilder
Builds a new uninitialised FLOW expression.

Specified by:
buildFlowExpression in interface ExpressionBuilder<E>
Overrides:
buildFlowExpression in class StandardExpressionBuilder<E>
Returns:
The constructed expression; never null.

buildInitialisedFlowExpression

public Expression<E> buildInitialisedFlowExpression(Expression<? extends E>... expressions)
                                             throws ExpressionException
Description copied from interface: ExpressionBuilder
Builds a new initialised FLOW expression.

Specified by:
buildInitialisedFlowExpression in interface ExpressionBuilder<E>
Overrides:
buildInitialisedFlowExpression in class StandardExpressionBuilder<E>
Parameters:
expressions - The expressions, in order, to be associated with the returned expression; cannot be null.
Returns:
The constructed expression; never null.
Throws:
ExpressionException - If the building fails.

buildNextExpression

public Expression<E> buildNextExpression(Expression<? extends Number> count)
Description copied from interface: ExpressionBuilder
Builds a new NEXT expression.

Specified by:
buildNextExpression in interface ExpressionBuilder<E>
Overrides:
buildNextExpression in class StandardExpressionBuilder<E>
Parameters:
count - The expression that will determine the number of times next() will be invoked on the sequence when the constructed expression is evaluated.
Returns:
The constructed expression; never null.

buildNonShortCircuitAndExpression

public Expression<Boolean> buildNonShortCircuitAndExpression(Expression<Boolean> first,
                                                             Expression<Boolean> second)
Description copied from interface: ExpressionBuilder
Builds a new non short-circuit AND expression.

Specified by:
buildNonShortCircuitAndExpression in interface ExpressionBuilder<E>
Overrides:
buildNonShortCircuitAndExpression in class StandardExpressionBuilder<E>
Parameters:
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Returns:
The constructed expression; never null.

buildNonShortCircuitOrExpression

public Expression<Boolean> buildNonShortCircuitOrExpression(Expression<Boolean> first,
                                                            Expression<Boolean> second)
Description copied from interface: ExpressionBuilder
Builds a new non short-circuit OR expression.

Specified by:
buildNonShortCircuitOrExpression in interface ExpressionBuilder<E>
Overrides:
buildNonShortCircuitOrExpression in class StandardExpressionBuilder<E>
Parameters:
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Returns:
The constructed expression; never null.

buildNotExpression

public Expression<Boolean> buildNotExpression(Expression<Boolean> expression)
Description copied from interface: ExpressionBuilder
Builds a new NOT expression.

Specified by:
buildNotExpression in interface ExpressionBuilder<E>
Overrides:
buildNotExpression in class StandardExpressionBuilder<E>
Parameters:
expression - The expression operand; cannot be null.
Returns:
The constructed expression; never null.

buildOrExpression

public Expression<Boolean> buildOrExpression(Expression<Boolean> first,
                                             Expression<Boolean> second)
Description copied from interface: ExpressionBuilder
Builds a new short-circuit OR expression.

Specified by:
buildOrExpression in interface ExpressionBuilder<E>
Overrides:
buildOrExpression in class StandardExpressionBuilder<E>
Parameters:
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Returns:
The constructed expression; never null.

buildResetExpression

public Expression<E> buildResetExpression()
Description copied from interface: ExpressionBuilder
Builds a new RESET expression.

Specified by:
buildResetExpression in interface ExpressionBuilder<E>
Overrides:
buildResetExpression in class StandardExpressionBuilder<E>
Returns:
The constructed expression; never null.

buildReverseExpression

public Expression<Boolean> buildReverseExpression(Expression<Boolean> reverse)
Description copied from interface: ExpressionBuilder
Builds a new REVERSE expression.

Specified by:
buildReverseExpression in interface ExpressionBuilder<E>
Overrides:
buildReverseExpression in class StandardExpressionBuilder<E>
Parameters:
reverse - The expression that will determine if the sequence is tried reversed when the constructed expression is evaluated.
Returns:
The constructed expression; never null.

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>
Overrides:
buildVariableExpression in class AbstractExpressionBuilder<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.

copy

public CountingExpressionBuilder<E> copy()
Description copied from interface: Copyable
Returns a deep copy of this object.

Specified by:
copy in interface Copyable<ExpressionBuilder<E>>
Overrides:
copy in class StandardExpressionBuilder<E>
Returns:
The copy; never null.

count

protected <V,W extends Expression<V>> W count(W expression)
Updates the number of times this builder has seen an expression with the same name as expression.

The count per expression name is stored in the context used, prefixed with count..

Type Parameters:
V - The type of value the evaluation of expression produces.
W - The actual type of expression.
Parameters:
expression - The expression to increase the count for; cannot be null.
Returns:
expression; never null.
Throws:
NullPointerException - If expression is null.

getExpressionCount

public Map<String,Integer> getExpressionCount()
                                       throws ExpressionException
Returns a map containing as values the number of times a given expression type has been created, excluding the count for the root expression.

The keys correspond to an expression name.

Returns:
A map containing the count; never null, but can be empty.
Throws:
ExpressionException - If the generation of the map fails.

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.