net.sf.jga.fn.adaptor
Class ConditionalGenerator<R>

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

public class ConditionalGenerator<R>
extends Generator<R>

Generator 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 ConditionalGenerator.Visitor
          Interface for classes that may interpret a ConditionalGenerator functor.
 
Constructor Summary
ConditionalGenerator(Generator<java.lang.Boolean> test, Generator<R> trueFn, Generator<R> falseFn)
          Builds a ConditionalGenerator 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(ConditionalGenerator) method, if it implements the nested Visitor interface.
 R fn()
          Given argument x, evaluates test(x); if true, returns trueFn(x), otherwise, returns falseFn(x).
 Generator<java.lang.Boolean> getCondition()
          Returns the test functors
 Generator<R> getFalseFunctor()
          Returns the functor that is executed when the condition is false
 Generator<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.Generator
bind0th, bindNth, eval
 
Methods inherited from class net.sf.jga.fn.Functor
composeNth, composeNth, generateNth
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConditionalGenerator

public ConditionalGenerator(Generator<java.lang.Boolean> test,
                            Generator<R> trueFn,
                            Generator<R> falseFn)
Builds a ConditionalGenerator 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 Generator<java.lang.Boolean> getCondition()
Returns the test functors

Returns:
the test functors

getTrueFunctor

public Generator<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 Generator<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()
Given argument x, evaluates test(x); if true, returns trueFn(x), otherwise, returns falseFn(x).

Specified by:
fn in class Generator<R>
Returns:
test() ? trueFn() : falseFn()

accept

public void accept(Visitor v)
Calls the Visitor's visit(ConditionalGenerator) 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.