net.sf.jga.fn.adaptor

Class Conditional<T,R>

public class Conditional<T,R> extends AbstractUnaryFunctor<T,R> implements Serializable

UnaryFunctor that tests a condition, executes one of two given functors, and returns the result. The argument used to evaluate the condition will also be passed to the functor when it is executed. This functor implements the traditional ?: operator.

Copyright © 2003 David A. Hall

Author: David A. Hall

Nested Class Summary
interfaceConditional.Visitor
Interface for classes that may interpret a Conditional functor.
Constructor Summary
Conditional(UnaryFunctor<T,Boolean> test, UnaryFunctor<T,R> trueFn, UnaryFunctor<T,R> falseFn)
Builds a Conditional functor, given the condition to test, and the two functors that may be executed.
Method Summary
voidaccept(Visitor v)
Calls the Visitor's visit(Conditional) method, if it implements the nested Visitor interface.
Rfn(T x)
Given argument x, evaluates test(x); if true, returns trueFn(x), otherwise, returns falseFn(x).
UnaryFunctor<T,Boolean>getCondition()
Returns the test functors
UnaryFunctor<T,R>getFalseFn()
Returns the functor that is executed when the condition is false
UnaryFunctor<T,R>getTrueFn()
Returns the functor that is executed when the condition is true
static <T> Conditional<T,T>replaceAll(UnaryFunctor<T,Boolean> test, T value)
Builds a replacement functor: returns the given value if the arg passes the test, returns the arg if it does not pass the test.
StringtoString()

Constructor Detail

Conditional

public Conditional(UnaryFunctor<T,Boolean> test, UnaryFunctor<T,R> trueFn, UnaryFunctor<T,R> falseFn)
Builds a Conditional functor, given the condition to test, and the two functors that may be executed.

Throws: IllegalArgumentException if any of the functors is missing

Method Detail

accept

public void accept(Visitor v)
Calls the Visitor's visit(Conditional) method, if it implements the nested Visitor interface.

fn

public R fn(T x)
Given argument x, evaluates test(x); if true, returns trueFn(x), otherwise, returns falseFn(x).

Returns: test(x) ? trueFn(x) : falseFn(x)

getCondition

public UnaryFunctor<T,Boolean> getCondition()
Returns the test functors

Returns: the test functors

getFalseFn

public UnaryFunctor<T,R> getFalseFn()
Returns the functor that is executed when the condition is false

Returns: the functor that is executed when the condition is false

getTrueFn

public UnaryFunctor<T,R> getTrueFn()
Returns the functor that is executed when the condition is true

Returns: the functor that is executed when the condition is true

replaceAll

public static <T> Conditional<T,T> replaceAll(UnaryFunctor<T,Boolean> test, T value)
Builds a replacement functor: returns the given value if the arg passes the test, returns the arg if it does not pass the test.

Returns: a UnaryFunctor of the form: test(x) ? x : value;

toString

public String toString()