net.sf.jga.fn.adaptor
Class ConditionalUnary<T,R>

java.lang.Object
  extended by net.sf.jga.fn.Functor<R>
      extended by net.sf.jga.fn.UnaryFunctor<T,R>
          extended by net.sf.jga.fn.adaptor.ConditionalUnary<T,R>
All Implemented Interfaces:
java.io.Serializable, Visitable

public class ConditionalUnary<T,R>
extends UnaryFunctor<T,R>

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 whichever functor is executed. This functor implements the traditional ?: operator.

Copyright © 2003-2005 David A. Hall

Author:
David A. Hall
See Also:
Serialized Form

Nested Class Summary
static interface ConditionalUnary.Visitor
          Interface for classes that may interpret a ConditionalUnary functor.
 
Constructor Summary
ConditionalUnary(UnaryFunctor<T,java.lang.Boolean> test, UnaryFunctor<T,R> trueFn, UnaryFunctor<T,R> falseFn)
          Builds a ConditionalUnary functor, given the condition to test, and the two functors that may be executed.
 
Method Summary
 void accept(Visitor v)
          Calls the Visitor's visit(ConditionalUnary) method, if it implements the nested Visitor interface.
 R fn(T x)
          Given argument x, evaluates test(x); if true, returns trueFn(x), otherwise, returns falseFn(x).
 UnaryFunctor<T,java.lang.Boolean> getCondition()
          Returns the test functors
 UnaryFunctor<T,R> getFalseFunctor()
          Returns the functor that is executed when the condition is false
 UnaryFunctor<T,R> getTrueFunctor()
          Returns the functor that is executed when the condition is true
 java.lang.String toString()
           
 
Methods inherited from class net.sf.jga.fn.UnaryFunctor
bind, bind0th, bindNth, compose, compose, compose, composeNth, composeNth, eval, generate, generateNth
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConditionalUnary

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

Throws:
java.lang.IllegalArgumentException - if any of the functors is missing
Method Detail

getCondition

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

Returns:
the test functors

getTrueFunctor

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

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

getFalseFunctor

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

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

fn

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

Specified by:
fn in class UnaryFunctor<T,R>
Returns:
test(x) ? trueFn(x) : falseFn(x)

accept

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

Specified by:
accept in interface Visitable
Overrides:
accept in class Functor<R>

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2002-2006 David A. Hall. All Rights Reserved.