net.sf.collections15.functors.closure
Class IfClosure<E>

java.lang.Object
  extended by net.sf.collections15.functors.closure.IfClosure<E>
All Implemented Interfaces:
java.io.Serializable, Closure<E>
Direct Known Subclasses:
IfElseClosure

public class IfClosure<E>
extends java.lang.Object
implements Closure<E>, java.io.Serializable

Closure decorator that conditionally executes a decorated Closure based on a predicate, like an if statement.

Since:
Collections15 1.0
Author:
Stephen Colebourne, Chris Lambrou (port to Java 5.0)
See Also:
Serialized Form

Field Summary
protected  Predicate<? super E> predicate
          The condition to determine whether or not the decorated Closure executes.
protected  Closure<? super E> trueClosure
          The decorated Closure to execute if the condition evaluates to true.
 
Constructor Summary
protected IfClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure)
          Creates a Closure, that executes an existing Closure only if a specified condition is met.
 
Method Summary
static
<T> Closure<T>
decorate(Predicate<? super T> predicate, Closure<? super T> trueClosure)
          Decorates a Closure, returning a Closure that executes only if a specified condition is met.
 void execute(E input)
          Conditionally executes the Closure according to the result of the Predicate.
 Predicate<? super E> getPredicate()
          Gets the Predicate.
 Closure<? super E> getTrueClosure()
          Gets the Closure executed when the Predicate evaluate to true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

predicate

protected final Predicate<? super E> predicate
The condition to determine whether or not the decorated Closure executes.


trueClosure

protected final Closure<? super E> trueClosure
The decorated Closure to execute if the condition evaluates to true.

Constructor Detail

IfClosure

protected IfClosure(Predicate<? super E> predicate,
                    Closure<? super E> trueClosure)
Creates a Closure, that executes an existing Closure only if a specified condition is met.

Parameters:
predicate - The condition that must evaluate to true if the Closure is to execute.
trueClosure - The conditionally executed Closure.
Throws:
java.lang.IllegalArgumentException - Thrown if any argument is null.
Method Detail

decorate

public static <T> Closure<T> decorate(Predicate<? super T> predicate,
                                      Closure<? super T> trueClosure)
Decorates a Closure, returning a Closure that executes only if a specified condition is met.

Parameters:
predicate - The condition that must evaluate to true if the Closure is to execute.
trueClosure - The conditionally executed Closure.
Returns:
A Closure that executes only if the Predicate evaluates to true.
Throws:
java.lang.IllegalArgumentException - Thrown if any argument is null.

execute

public void execute(E input)
Conditionally executes the Closure according to the result of the Predicate.

Specified by:
execute in interface Closure<E>
Parameters:
input - The input object to act upon.

getPredicate

public Predicate<? super E> getPredicate()
Gets the Predicate.

Returns:
The Predicate.
Since:
Collections15 1.0

getTrueClosure

public Closure<? super E> getTrueClosure()
Gets the Closure executed when the Predicate evaluate to true.

Returns:
The true Closure.
Since:
Commons Collections15 1.0


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.