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

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

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.SequenceExpression<E,Boolean>
          extended by dk.rode.thesis.interpreter.ReverseExpression<E>
Type Parameters:
E - The (super-) type of values delivered by the manipulated sequence.
All Implemented Interfaces:
Expression<Boolean>, TerminalExpression<Boolean>, Copyable<Expression<Boolean>>, StrictCopyable<Expression<Boolean>>

@Participant(value="TerminalExpression")
public class ReverseExpression<E>
extends SequenceExpression<E,Boolean>

A reverse expression will invoke reverse() on a given reversible sequence when evaluated, if so specified and only if possible.

In order to reverse the sequence, the sequence must not only be reversible, but an expression can also be supplied to determine whether or not to reverse at evaluation time. Any expression of the type Expression<Boolean> can be used.

Author:
Gunni Rode / rode.dk
See Also:
CurrentExpression, NextExpression, SetExpression, ResetExpression

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  Expression<Boolean> reverse
          If this expression evaluates to true, the sequence will be tried reversed, not if it evaluates to false.
 
Fields inherited from class dk.rode.thesis.interpreter.SequenceExpression
includeHashCode, sequence
 
Constructor Summary
ReverseExpression(ReverseExpression<E> expression)
          Copy constructor.
ReverseExpression(Sequence<? extends E> sequence)
          Constructor.
ReverseExpression(Sequence<? extends E> sequence, boolean reverse)
          Constructor.
ReverseExpression(Sequence<? extends E> sequence, boolean reverse, boolean includeHashCode)
          Constructor.
ReverseExpression(Sequence<? extends E> sequence, Expression<Boolean> reverse)
          Constructor.
ReverseExpression(Sequence<? extends E> sequence, Expression<Boolean> reverse, boolean includeHashCode)
          Constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns reverse[x], where x is the symbolic representation of the expression used to determine if the sequence should be reversed, or reverse#123[x], where 123 is the identity hash code of the manipulated sequence.
 ReverseExpression<E> copy()
          Returns a deep copy of this object.
 Boolean 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.SequenceExpression
equals, hashCode
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains
 
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
 

Field Detail

reverse

private final Expression<Boolean> reverse
If this expression evaluates to true, the sequence will be tried reversed, not if it evaluates to false.

Constructor Detail

ReverseExpression

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

The actual sequence manipulated by expression is not copied, but used as is.

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

ReverseExpression

public ReverseExpression(Sequence<? extends E> sequence)
Constructor.

This expression will try and reverse sequence.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
Throws:
NullPointerException - If sequence is null.

ReverseExpression

public ReverseExpression(Sequence<? extends E> sequence,
                         boolean reverse)
Constructor.

This expression will try and reverse sequence if reverse is true, not if false (no-op).

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
reverse - True to try and reverse sequence, false not to (no-op).
Throws:
NullPointerException - If sequence is null.

ReverseExpression

public ReverseExpression(Sequence<? extends E> sequence,
                         boolean reverse,
                         boolean includeHashCode)
Constructor.

This expression will try and reverse sequence if reverse is true, not if false (no-op).

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
reverse - True to try and reverse sequence, false not to (no-op).
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence is null.

ReverseExpression

public ReverseExpression(Sequence<? extends E> sequence,
                         Expression<Boolean> reverse)
Constructor.

This expression will try and reverse sequence if reverse evaluates to true when this expression is evaluated.

The identity hash code of sequence is not included in the symbolic representation of this expression.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
reverse - The expression that will determine if sequence is tried reversed when this expression is evaluated.
Throws:
NullPointerException - If either argument is null.

ReverseExpression

public ReverseExpression(Sequence<? extends E> sequence,
                         Expression<Boolean> reverse,
                         boolean includeHashCode)
Constructor.

This expression will try and reverse sequence if reverse evaluates to true when this expression is evaluated.

The identity hash code of sequence is included in the symbolic representation of this expression if includeHashCode is true.

Parameters:
sequence - The sequence to manipulate by this expression; cannot be null.
reverse - The expression that will determine if sequence is tried reversed when this expression is evaluated.
includeHashCode - True to include the identity hash code of sequence in the symbolic representation, false not to.
Throws:
NullPointerException - If sequence or reverse are null.
Method Detail

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns reverse[x], where x is the symbolic representation of the expression used to determine if the sequence should be reversed, or reverse#123[x], where 123 is the identity hash code of the manipulated sequence.

Specified by:
asSymbol in interface Expression<Boolean>
Overrides:
asSymbol in class SequenceExpression<E,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 ReverseExpression<E> copy()
Description copied from interface: Copyable
Returns a deep copy of this object.

Returns:
The copy; 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!

Parameters:
context - The context to use; cannot be null.
Returns:
True if the manipulated sequence could be reversed, false if not.
Throws:
ExpressionException - If the evaluation fails.
See Also:
ReversibleSequence.reverse()

name

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

Returns:
reverse

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