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

dk.rode.thesis.interpreter
Class ConditionalExpression<E>

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.BinaryExpression<E,E>
          extended by dk.rode.thesis.interpreter.ConditionalExpression<E>
Type Parameters:
E - The type of value the evaluation of this expression produces.
All Implemented Interfaces:
Expression<E>, NonTerminalExpression<E>, Copyable<Expression<E>>, StrictCopyable<Expression<E>>

@Participant(value="NonTerminalExpression")
public class ConditionalExpression<E>
extends BinaryExpression<E,E>
implements NonTerminalExpression<E>

A conditional expression represents an if-then-else expression.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  Expression<Boolean> condition
          The condition in form of an expression that returns a Boolean value when evaluated.
 
Fields inherited from class dk.rode.thesis.interpreter.BinaryExpression
first, second
 
Constructor Summary
ConditionalExpression(ConditionalExpression<E> expression)
          Copy constructor.
ConditionalExpression(Expression<Boolean> condition, Expression<? extends E> first, Expression<? extends E> second)
          Constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns {@code (c ?
 boolean contains(Expression<?> expression)
          Returns true if this expression is or contains the expression supplied as expression, false if not.
 ConditionalExpression<E> copy()
          Copies this expression.
 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.BinaryExpression
asSymbol, asSymbol
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
equals, hashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

condition

private final Expression<Boolean> condition
The condition in form of an expression that returns a Boolean value when evaluated.

Never null.

Constructor Detail

ConditionalExpression

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

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

ConditionalExpression

public ConditionalExpression(Expression<Boolean> condition,
                             Expression<? extends E> first,
                             Expression<? extends E> second)
Constructor.

Parameters:
condition - The condition expression; cannot be null.
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Throws:
NullPointerException - If either expression is null.
Method Detail

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns (c ? x : y), where c is the symbolic representation of the condition, x of the first expression operand, and y of the the second.

Specified by:
asSymbol in interface Expression<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)

contains

public boolean contains(Expression<?> expression)
Description copied from interface: Expression
Returns true if this expression is or contains the expression supplied as expression, false if not.

The test is performed using identity (==).

Unlike Expression.asSymbol(Context), querying for containment cannot handle cyclic expression references!

Specified by:
contains in interface Expression<E>
Overrides:
contains in class AbstractExpression<E>
Parameters:
expression - The expression to test; cannot be null.
Returns:
True if expression is this expression or contained within this expression, false if not.
See Also:
Expression.asSymbol(Context), Context.touch(Expression)

copy

public ConditionalExpression<E> copy()
Description copied from interface: Expression
Copies this expression.

Unlike Expression.asSymbol(Context), copying cannot handle cyclic expression references!

Specified by:
copy in interface Expression<E>
Specified by:
copy in interface Copyable<Expression<E>>
Returns:
A copy of this expression; 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!

Specified by:
evaluate in interface Expression<E>
Parameters:
context - The context to use; cannot be null.
Returns:
The result of the evaluation; never null.
Throws:
ExpressionException - If the evaluation fails.

name

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

Specified by:
name in interface Expression<E>
Returns:
conditional

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>
Overrides:
operands in class BinaryExpression<E,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>
Overrides:
toString in class BinaryExpression<E,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.