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

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

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

public class CompareExpression<E extends Comparable<? super E>>
extends BinaryExpression<E,Boolean>
implements NonTerminalExpression<Boolean>

A compare expression can compare two expressions evaluating to the same Comparable type as smaller than, smaller than or equal, equal, not equal, greater than, or equal or greater than.

Author:
Gunni Rode / rode.dk
See Also:
EqualExpression, AndExpression, OrExpression, NotExpression

Nested Class Summary
static class CompareExpression.Comparison
          The comparison type to be performed by a compare expression.
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
 CompareExpression.Comparison comparison
          The type of comparison performed by this expression.
 
Fields inherited from class dk.rode.thesis.interpreter.BinaryExpression
first, second
 
Constructor Summary
CompareExpression(CompareExpression.Comparison comparison, Expression<? extends E> first, Class<E> type, E value)
          Constructor.
CompareExpression(CompareExpression.Comparison comparison, Expression<? extends E> first, Class<E> type, String name)
          Constructor.
CompareExpression(CompareExpression.Comparison comparison, Expression<? extends E> first, Expression<? extends E> second)
          Constructor.
CompareExpression(CompareExpression<E> expression)
          Copy constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns x operator y, where x is the symbolic representation of the first expression operand, y of the the second, and operator is the operator used for the comparison.
 CompareExpression<E> copy()
          Copies this expression.
 Boolean evaluate(Context context)
          Evaluates this expression and returns the result.
 String name()
          The stand-alone symbol name for this expression.
 String toString()
          Returns the string representation of this expression.
 
Methods inherited from class dk.rode.thesis.interpreter.BinaryExpression
asSymbol, asSymbol, operands
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains, equals, hashCode
 
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, operands
 

Field Detail

comparison

public final CompareExpression.Comparison comparison
The type of comparison performed by this expression.

Never null.

Constructor Detail

CompareExpression

public CompareExpression(CompareExpression.Comparison comparison,
                         Expression<? extends E> first,
                         Class<E> type,
                         E value)
Constructor.

The second expression used is a constant expression created based on type and value.

Parameters:
comparison - The comparison type; cannot be null.
first - The first expression operand; cannot be null.
type - The type of the constant expression to use as the second expression operand; cannot be null.
value - The value of the constant expression to use as the second expression operand; cannot be null.
Throws:
NullPointerException - If either argument is null.

CompareExpression

public CompareExpression(CompareExpression.Comparison comparison,
                         Expression<? extends E> first,
                         Class<E> type,
                         String name)
Constructor.

The second expression used is a variable expression created based on type and name.

Parameters:
comparison - The comparison type; cannot be null.
first - The first expression operand; cannot be null.
type - The type of the variable expression to use as the second expression operand; cannot be null.
name - The name of the variable expression to use as the second expression operand; cannot be null.
Throws:
NullPointerException - If either argument is null.

CompareExpression

public CompareExpression(CompareExpression.Comparison comparison,
                         Expression<? extends E> first,
                         Expression<? extends E> second)
Constructor.

Parameters:
comparison - The comparison type; cannot be null.
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Throws:
NullPointerException - If either argument is null.

CompareExpression

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

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 x operator y, where x is the symbolic representation of the first expression operand, y of the the second, and operator is the operator used for the comparison.

Specified by:
asSymbol in interface Expression<Boolean>
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 CompareExpression<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<Boolean>
Specified by:
copy in interface Copyable<Expression<Boolean>>
Returns:
A copy of this expression; never null.

evaluate

public Boolean 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<Boolean>
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<Boolean>
Returns:
compare

toString

public String toString()
Description copied from class: AbstractExpression
Returns the string representation of this expression.

Specified by:
toString in interface Expression<Boolean>
Overrides:
toString in class BinaryExpression<E extends Comparable<? super E>,Boolean>
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.