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

dk.rode.thesis.interpreter
Class AndExpression

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.BinaryExpression<Boolean,Boolean>
          extended by dk.rode.thesis.interpreter.AndExpression
All Implemented Interfaces:
Expression<Boolean>, NonTerminalExpression<Boolean>, Copyable<Expression<Boolean>>, StrictCopyable<Expression<Boolean>>

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

A logical and expression for two expressions evaluating to a Boolean value.

The logical AND operation may or may not be short-circuit, which is determined at construction time.

The evaluation of a logical short-circuit AND expression is not commutative.

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
 boolean shortCircuit
          True if a short-circuit logical AND operation is used, i.e.
 
Fields inherited from class dk.rode.thesis.interpreter.BinaryExpression
first, second
 
Constructor Summary
AndExpression(AndExpression expression)
          Copy constructor.
AndExpression(boolean shortCircuit, Expression<Boolean> first, Expression<Boolean> second)
          Constructor.
AndExpression(Expression<Boolean> first, Expression<Boolean> second)
          Constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns (x && y) if short-circuit, or (x & y), where x is the symbolic representation of the first expression operand and y of the second.
 AndExpression 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.
 
Methods inherited from class dk.rode.thesis.interpreter.BinaryExpression
asSymbol, asSymbol, operands, toString
 
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, toString
 

Field Detail

shortCircuit

public final boolean shortCircuit
True if a short-circuit logical AND operation is used, i.e. &&, false if not, i.e. &.

Constructor Detail

AndExpression

public AndExpression(AndExpression expression)
Copy constructor.

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

AndExpression

public AndExpression(boolean shortCircuit,
                     Expression<Boolean> first,
                     Expression<Boolean> second)
Constructor.

The logical AND operation performed is short-circuit, i.e. &&, if shortCircuit is true, otherwise not, i.e &.

Parameters:
shortCircuit - True for short-circuit (&&).
first - The first expression operand; cannot be null.
second - The second expression operand; cannot be null.
Throws:
NullPointerException - If either expression is null.

AndExpression

public AndExpression(Expression<Boolean> first,
                     Expression<Boolean> second)
Constructor.

The logical AND operation performed is short-circuit, i.e. &&.

Parameters:
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 (x && y) if short-circuit, or (x & y), where x is the symbolic representation of the first expression operand and y of the second.

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 AndExpression 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:
and

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.