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

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

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

@Participant(value="ConcreteBuilder")
public class TypedExpressionBuilder<E>
extends AbstractExpressionBuilder<E>
implements ExpressionBuilder<E>

A typed expression builder builds typed expressions.

An actual builder must be supplied at construction to perform the construction of expressions, but this builder will use a local context.

Implementation notes:
This builder uses covariant types for the created expressions where possible. The expression type is narrowed to TypedExpression. Narrowing it even further, i.e. to the exact expression type, is not always possible if the implementation types are hidden: how can the builder cast to the exact type if only the interfaces are known? As TypedExpression has a concrete decorator class, TypedExpressionDecorator, the narrowing to TypedExpression can be performed because an implementation is always known.

This presents a problem with the buildFlowExpression() method because it already has a narrowed return type, namely FlowExpression, and because it is created via composition; this builder has no control over the creation process. The FlowExpression type must not be lost, or the functionality offered by it cannot be used by the client. This is not a problem for the buildInitialisedFlowExpression(Expression...) method, because it returns an Expression type like most other builder methods.

Inheritance, proxies, and/or decoration/adaptation must be used. Dynamic proxies can only be used of an interface is used. Decoration and adaptation can only be used if a non-final type is known. Inheritance only if a concrete class is known, regardless if an interface is known. As FlowExpression is concrete, but non final, we have to make a decorator wrapping the created FlowExpression, overriding all methods, and adapting it to the TypedExpression interface. This is tedious at best, but cannot be avoided. The decorator class is TypedFlowExpression.

Dynamic proxies created by a ProxyFactory cannot be used for several reasons. First, FlowExpression is a concrete type, so casting a proxy into the return type required by the buildFlowExpression() method will cause a class cast exception. Secondly, FlowExpression already implements InitialisableExpression that use covariant return types to refine its copy() method. TypedExpression also use covariant return types to to refine its copy() method. This works for FlowExpression because it is a concrete type known to implement both interfaces, but cannot work for dynamic proxies because if incompatible return types. See the JavaDoc for TypedFlowExpression.copy() to get an idea of the different narrowed return types that fits the TypedFlowExpression class.

Instead of inheritance, this builder relies on composition: the actual builder to construct the expressions is supplied at construction time. This can be viewed as an application of the Decorator pattern, and perhaps also as a degenerate version of the Bridge pattern where the abstraction and implementation share the same interface.

Author:
Gunni Rode / rode.dk

Field Summary
protected  ExpressionBuilder<E> builder
          The expression builder used to construct the expressions.
protected  Class<E> type
          A class literal representing the type of value the evaluation of constructed expressions produces.
 
Fields inherited from class dk.rode.thesis.builder.AbstractExpressionBuilder
context, root, sequence
 
Constructor Summary
TypedExpressionBuilder(Class<E> type, ExpressionBuilder<E> builder)
          Constructor.
TypedExpressionBuilder(TypedExpressionBuilder<E> builder)
          Copy constructor.
 
Method Summary
 TypedExpression<Boolean> buildAndExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new short-circuit AND expression.
 TypedExpression<E> buildAssignmentExpression(VariableExpression<E> variable, Expression<? extends E> expression)
          Builds a new ASSIGNMENT expression.
 TypedExpression<E> buildBreakExpression(TypedExpression<E> expression)
          Builds a new BREAK expression.
 TypedExpression<E> buildConditionalExpression(Expression<Boolean> condition, Expression<? extends E> first, Expression<? extends E> second)
          Builds a new CONDITIONAL expression.
 TypedExpression<E> buildCurrentExpression()
          Builds a new CURRENT expression.
 TypedExpression<Boolean> buildEqualExpression(Expression<?> first, Expression<?> second)
          Builds a new EQUAL expression.
 TypedFlowExpression<E> buildFlowExpression()
          Builds a new uninitialised FLOW expression.
 TypedExpression<E> buildInitialisedFlowExpression(Expression<? extends E>... expressions)
          Builds a new initialised FLOW expression.
 TypedExpression<E> buildNextExpression(Expression<? extends Number> count)
          Builds a new NEXT expression.
 TypedExpression<Boolean> buildNonShortCircuitAndExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new non short-circuit AND expression.
 TypedExpression<Boolean> buildNonShortCircuitOrExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new non short-circuit OR expression.
 TypedExpression<Boolean> buildNotExpression(Expression<Boolean> expression)
          Builds a new NOT expression.
 TypedExpression<Boolean> buildOrExpression(Expression<Boolean> first, Expression<Boolean> second)
          Builds a new short-circuit OR expression.
 TypedExpression<E> buildResetExpression()
          Builds a new RESET expression.
 TypedExpression<Boolean> buildReverseExpression(Expression<Boolean> reverse)
          Builds a new REVERSE expression.
 TypedExpressionBuilder<E> copy()
          Returns a deep copy of this object.
protected static
<T> TypedExpression<T>
getTypedExpression(Expression<T> expression, Class<T> type)
          Returns a typed expression of the typed supplied as type based on expression.
 Class<E> type()
          Returns a class literal representing the type of value the evaluation of constructed expressions produces.
 
Methods inherited from class dk.rode.thesis.builder.AbstractExpressionBuilder
buildConstantExpression, buildExpression, buildVariableExpression, 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
buildConstantExpression, buildExpression, buildVariableExpression, getRootExpression, getSequence
 

Field Detail

builder

protected final ExpressionBuilder<E> builder
The expression builder used to construct the expressions.

Never null.


type

protected final Class<E> type
A class literal representing the type of value the evaluation of constructed expressions produces. Supplied at construction time, never null.

Constructor Detail

TypedExpressionBuilder

public TypedExpressionBuilder(Class<E> type,
                              ExpressionBuilder<E> builder)
Constructor.

The same sequence as used by builder will be used by this builder, but a new local context will be created and used.

Parameters:
type - The class literal representing the type of value the evaluation of constructed expressions produces; cannot be null.
builder - The actual builder to use; cannot be null.
Throws:
NullPointerException - If either argument is null.

TypedExpressionBuilder

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

The same sequence as used by builder will be used by this builder, but a new local context will be created and used. 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.
Method Detail

buildAndExpression

public TypedExpression<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>
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 TypedExpression<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>
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 TypedExpression<E> buildBreakExpression(TypedExpression<E> expression)
Description copied from interface: ExpressionBuilder
Builds a new BREAK expression.

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

buildConditionalExpression

public TypedExpression<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>
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.

buildCurrentExpression

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

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

buildEqualExpression

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

Specified by:
buildEqualExpression in interface ExpressionBuilder<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 TypedFlowExpression<E> buildFlowExpression()
Description copied from interface: ExpressionBuilder
Builds a new uninitialised FLOW expression.

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

buildInitialisedFlowExpression

public TypedExpression<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>
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 TypedExpression<E> buildNextExpression(Expression<? extends Number> count)
Description copied from interface: ExpressionBuilder
Builds a new NEXT expression.

Specified by:
buildNextExpression in interface ExpressionBuilder<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 TypedExpression<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>
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 TypedExpression<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>
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 TypedExpression<Boolean> buildNotExpression(Expression<Boolean> expression)
Description copied from interface: ExpressionBuilder
Builds a new NOT expression.

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

buildOrExpression

public TypedExpression<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>
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 TypedExpression<E> buildResetExpression()
Description copied from interface: ExpressionBuilder
Builds a new RESET expression.

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

buildReverseExpression

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

Specified by:
buildReverseExpression in interface ExpressionBuilder<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.

copy

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

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

getTypedExpression

protected static final <T> TypedExpression<T> getTypedExpression(Expression<T> expression,
                                                                 Class<T> type)
Returns a typed expression of the typed supplied as type based on expression.

Type Parameters:
T - The type.
Parameters:
expression - The expression to associate type with, if not already; cannot be null.
type - The type; cannot be null.
Returns:
A typed expression; never null.
Throws:
NullPointerException - If either argument is null.

type

public final Class<E> type()
Returns a class literal representing the type of value the evaluation of constructed expressions produces.

Returns:
The type; 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.