jfun.yan
Class Monad

java.lang.Object
  extended by jfun.yan.Monad

public class Monad
extends java.lang.Object

The facade class to provide monadic combinators for Component.

map(jfun.yan.Creator, jfun.yan.Map) only maps one Creator, this class provides additional map combinator to map 2, 3, 4, 5 Creators to a Component.

For mapping for more than 5 Creator objects, use Component.map(jfun.yan.Map) together with Components.array(jfun.yan.Component[]) or Components.list(jfun.yan.Creator[]) or Components.list(java.util.List).

The bind() combinator is the ultimate resort to get flexibility.

Codehaus.org.

Author:
Ben Yu

Constructor Summary
Monad()
           
 
Method Summary
static
<From,To> Component<To>
bind(Creator<From> c1, Binder<From,To> binder)
          Monadic 'bind' operation.
static
<From,To> Component<To>
bind(Creator<From> c1, ComponentBinder<From,To> binder)
          Monadic 'bind' operation.
static Component fail(java.lang.String msg)
          Create a Component that always fails.
static
<T> Component<T>
followedBy(Component<T> c1, Binder<T,?> binder)
          Create a staged component.
static
<T> Component<T>
followedBy(Component<T> c1, ComponentBinder<T,?> binder)
          Create a staged component.
static
<T> Component<T>
followedBy(Component<T> c1, Creator<?> c2)
          Create a staged component.
static
<T> Component<T>
ifelse(Creator<java.lang.Boolean> cond, Component<T> a, Component<T> b)
          Create a Component object according to the boolean value returned from another Component.
static ComponentBinder instantiator()
          To create a ComponentBinder object that instantiates the Creator object created from the previous step.
static
<A,B,C,D,E,R>
Component<R>
map(Creator<A> c1, Creator<B> c2, Creator<C> c3, Creator<D> c4, Creator<E> c5, Map5<A,B,C,D,E,R> m)
          Creates a Component object which takes instances created from five other Creator object and transforms them to a new Object.
static
<A,B,C,D,R>
Component<R>
map(Creator<A> c1, Creator<B> c2, Creator<C> c3, Creator<D> c4, Map4<A,B,C,D,R> m)
          Creates a Component object which takes instances created from four other Creator object and transforms them to a new Object.
static
<A,B,C,R> Component<R>
map(Creator<A> c1, Creator<B> c2, Creator<C> c3, Map3<A,B,C,R> m)
          Creates a Component object which takes instances created from three other Creator object and transforms them to a new Object.
static
<A,B,R> Component<R>
map(Creator<A> c1, Creator<B> c2, Map2<A,B,R> m)
          Creates a Component object which takes instances created from two other Creator object and transforms them to a new Object.
static
<From,To> Component<To>
map(Creator<From> cc, Map<From,To> m)
          Customizes a Component object so that upon creation, the new Component object transforms the result component instance to another instance.
static
<T> Component<T>
mplus(Creator<T> c1, Creator<T> c2)
          The monadic mplus operation, when the first component fails with resolution error, an alternative component is tried instead.
static Component mzero()
          Create a monadic mzero Component that always fails.
static
<T> Recovery<T>
onException(java.lang.Class<? extends java.lang.Throwable> type, Creator<T> creator)
          Create a Recovery object that will recover from a provided exception type by returning an alternative Creator object.
static ComponentBinder pass()
          Create a ComponentBinder object that always returns the same object passed in.
static
<T> Component<T>
recover(Creator<T> c1, Recovery<T> r)
          Create a new Component object that will recover errors happened from the provided Component.
static
<A,B> Component<B>
seq(Creator<A> c1, Creator<B> c2)
          Monadic 'sequence' operation.
static
<T> Component<T>
sequence(Creator<T>... ccs)
          Monadic 'sequence' operator that sequentially execute an array of components and keep the result of the last one.
static
<From,To> ComponentBinder<From,To>
toComponentBinder(Binder<From,To> b)
          Convert a Binder object to a ComponentBinder object by making the verification do nothing.
static
<x,T> ComponentBinder<x,T>
toVerificationBinder(Creator<T> c)
          Create a ComponentBinder object that uses a Creator object for instantiation and verification regardless of the input.
static Verifiable verifyAs(java.lang.Class t)
          Create a Verifiable object that always succeed with a given result type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Monad

public Monad()
Method Detail

bind

public static <From,To> Component<To> bind(Creator<From> c1,
                                           ComponentBinder<From,To> binder)
Monadic 'bind' operation. It creates a new Component object. Upon instance creation, this Component object will

1. invokes the Creator object to create an instance.
2. feed the instance to the Binder object to get a second Creator object.
3. Invoke the second Creator object to get a second instance.
4. return the second instance as the instance of this Component.

Since the actual component type is not known until creation time, null is returned for getType().

Parameters:
c1 - the Component object.
binder - the Binder object that also takes care of dynamic verification.
Returns:
the new Component object.

bind

public static <From,To> Component<To> bind(Creator<From> c1,
                                           Binder<From,To> binder)
Monadic 'bind' operation. It creates a new Component object. Upon instance creation, this Component object will

1. invokes the Creator object to create an instance.
2. feed the instance to the Binder object to get a second Creator object.
3. Invoke the second Creator object to get a second instance.
4. return the second instance as the instance of this Component.

Since the actual component type is not known until creation time, null is returned for getType().
This version does not dynamically verifies the result type from the first component.

Parameters:
c1 - the Creator object.
binder - the Binder object.
Returns:
the new Component object.

seq

public static <A,B> Component<B> seq(Creator<A> c1,
                                     Creator<B> c2)
Monadic 'sequence' operation. It sequentially execute two components and keep the result of the second component.

Parameters:
c1 - the first component.
c2 - the second component.
Returns:
the new Component object.

sequence

public static <T> Component<T> sequence(Creator<T>... ccs)
Monadic 'sequence' operator that sequentially execute an array of components and keep the result of the last one.

Parameters:
ccs - the array of the components.
Returns:
the new Component object.

map

public static <From,To> Component<To> map(Creator<From> cc,
                                          Map<From,To> m)
Customizes a Component object so that upon creation, the new Component object transforms the result component instance to another instance.

Parameters:
cc - the Component object to customize.
m - the Map object to transform component instance.
Returns:
the new Component object.

map

public static <A,B,R> Component<R> map(Creator<A> c1,
                                       Creator<B> c2,
                                       Map2<A,B,R> m)
Creates a Component object which takes instances created from two other Creator object and transforms them to a new Object.

Parameters:
c1 - the first Creator object.
c2 - the second Creator object.
m - the Map2 object to transform.
Returns:
the Component object.

map

public static <A,B,C,R> Component<R> map(Creator<A> c1,
                                         Creator<B> c2,
                                         Creator<C> c3,
                                         Map3<A,B,C,R> m)
Creates a Component object which takes instances created from three other Creator object and transforms them to a new Object.

Parameters:
c1 - the first Creator object.
c2 - the second Creator object.
c3 - the third Creator object.
m - the Map3 object to transform.
Returns:
the Component object.

map

public static <A,B,C,D,R> Component<R> map(Creator<A> c1,
                                           Creator<B> c2,
                                           Creator<C> c3,
                                           Creator<D> c4,
                                           Map4<A,B,C,D,R> m)
Creates a Component object which takes instances created from four other Creator object and transforms them to a new Object.

Parameters:
c1 - the first Creator object.
c2 - the second Creator object.
c3 - the third Creator object.
c4 - the fourth Creator object.
m - the Map4 object to transform.
Returns:
the Component object.

map

public static <A,B,C,D,E,R> Component<R> map(Creator<A> c1,
                                             Creator<B> c2,
                                             Creator<C> c3,
                                             Creator<D> c4,
                                             Creator<E> c5,
                                             Map5<A,B,C,D,E,R> m)
Creates a Component object which takes instances created from five other Creator object and transforms them to a new Object.

Parameters:
c1 - the first Creator object.
c2 - the second Creator object.
c3 - the third Creator object.
c4 - the fourth Creator object.
c5 - the fifth Creator object.
m - the Map5 object to transform.
Returns:
the Component object.

mplus

public static <T> Component<T> mplus(Creator<T> c1,
                                     Creator<T> c2)
The monadic mplus operation, when the first component fails with resolution error, an alternative component is tried instead.

Parameters:
c1 - the first creator.
c2 - the alternative creator.
Returns:
the new Component.

fail

public static Component fail(java.lang.String msg)
Create a Component that always fails.

Parameters:
msg - the error message when fails.
Returns:
the new Component object.

mzero

public static Component mzero()
Create a monadic mzero Component that always fails.

Returns:
the new Component object.

recover

public static <T> Component<T> recover(Creator<T> c1,
                                       Recovery<T> r)
Create a new Component object that will recover errors happened from the provided Component.

Parameters:
c1 - the Component to recover.
r - the Recovery object.
Returns:
the new Component object.

onException

public static <T> Recovery<T> onException(java.lang.Class<? extends java.lang.Throwable> type,
                                          Creator<T> creator)
Create a Recovery object that will recover from a provided exception type by returning an alternative Creator object.

Parameters:
type - the exception type.
creator - the alternative Creator object.
Returns:
the new Recovery object.

followedBy

public static <T> Component<T> followedBy(Component<T> c1,
                                          ComponentBinder<T,?> binder)
Create a staged component. On instantiation, it first creates the base object using one component, then the ComponentBinder object is used to do certain side-effect to complete the work. The object created by the base component is finally returned as the instance.
Bean component, for example, is an application of staged component.
The ComponentBinder object also takes care of verification of the base component type.

Parameters:
c1 - the base component.
binder - the ComponentBinder object to do the side effect.
Returns:
the new Component object.

followedBy

public static <T> Component<T> followedBy(Component<T> c1,
                                          Binder<T,?> binder)
Create a staged component. On instantiation, it first creates the base object using one component, then the Binder object is used to do certain side-effect to complete the work. The object created by the base component is finally returned as the instance.
Bean component, for example, is an application of staged component.

Parameters:
c1 - the base component.
binder - the Binder object to do the side effect.
Returns:
the new Component object.

followedBy

public static <T> Component<T> followedBy(Component<T> c1,
                                          Creator<?> c2)
Create a staged component. On instantiation, it first creates the base object using one component, then another Creator object is used to do certain side-effect to complete the work. The object created by the base component is finally returned as the instance.
Bean component, for example, is an application of staged component.

Parameters:
c1 - the base component.
c2 - the Component object to do the side-effect.
Returns:
the new Component object.

pass

public static ComponentBinder pass()
Create a ComponentBinder object that always returns the same object passed in.

Returns:
the ComponentBinder object.

instantiator

public static ComponentBinder instantiator()
To create a ComponentBinder object that instantiates the Creator object created from the previous step.

Returns:
the ComponentBinder object.

toComponentBinder

public static <From,To> ComponentBinder<From,To> toComponentBinder(Binder<From,To> b)
Convert a Binder object to a ComponentBinder object by making the verification do nothing.

Parameters:
b - the Binder object.
Returns:
the ComponentBinder object, or the parameter b if it is already a ComponentBinder.

toVerificationBinder

public static <x,T> ComponentBinder<x,T> toVerificationBinder(Creator<T> c)
Create a ComponentBinder object that uses a Creator object for instantiation and verification regardless of the input.

Parameters:
c - the Component object.
Returns:
the ComponentBinder object.

ifelse

public static <T> Component<T> ifelse(Creator<java.lang.Boolean> cond,
                                      Component<T> a,
                                      Component<T> b)
Create a Component object according to the boolean value returned from another Component.

Parameters:
cond - the Component returning a Boolean value.
a - the Component when the condition is true.
b - the Component when the condition is false;
Returns:
the conditional Component.

verifyAs

public static Verifiable verifyAs(java.lang.Class t)
Create a Verifiable object that always succeed with a given result type.

Parameters:
t - the result type.
Returns:
the Verifiable object.