net.sf.staccatocommons.lambda
Class Lambda

java.lang.Object
  extended by net.sf.staccatocommons.lambda.Lambda

public final class Lambda
extends Object

An utility class for accessing in a shared, thread safe LambdaFactory , or creating standalone LambdaFactorys Lambda and LambdaFactory a DSL for instantiating simple Functions and Predicates. There are two manners of using those classes:

Using shared factories

This is the simplest use case. Just it needs to static-import Lambda, and combine $(Class) with lambda(Object) and variants, in the form lambdaVariant($(ArgumentType).message(args...)) . For example:
 lambda($(Collection.class).isEmpty());
 
This creates a new Predicate that takes a Collection argument and answers if it is empty. It is equivalent to the more verbose:
 new Predicate<Collection>() {
   public boolean eval(Collection arg) {
     return arg.isEmpty();
   }
 };
 
Although this syntax is quite convenient, accessing the shared LambdaFactory - hidden by the static-imports - needs a thread local variable to make it thread safe, which may be seen in some contexts like resources waste.

Using Local factories

For applications that want to avoid the previously described synchronization, then a better idea is to use local factories instead of a shared one, which looks a little less fancy, though. Local factories works exactly in the same was as shared factories, but need an explicit factory instantiation and reference. For example:
 LambdaFactory l = Lambda.factory();
 l.lambda(l.$(Collection.class).isEmpty());
 
Local factories must be declared as local variables.

Author:
flbulgarelli
See Also:
LambdaFactory

Field Summary
static Object _
          Equivalent to _(Object.class)
 
Constructor Summary
Lambda()
           
 
Method Summary
static
<A> A
_(Class<A> clazz)
          Answers a placeholder for lambdas arguments with arity > 1.
static
<A> A
$(Class<A> clazz)
          Stubs a type for creating a lambda.
static LambdaFactory factory()
          Answers a new LambdaFactory that is capable of stubbing interfaces and non-final classes
static
<A,B> net.sf.staccatocommons.defs.function.Function<A,B>
lambda(B returnType)
          Answers a Function that when applied sends to its argument the message previously sent to the last stubbed type.
static
<A> net.sf.staccatocommons.defs.predicate.Predicate<A>
lambda(boolean returnType)
          Answers a Predicate that when evaluated sends to its argument the message previously sent to the last stubbed type.
static
<A,B> net.sf.staccatocommons.defs.predicate.Predicate2<A,B>
lambda2(boolean returnType)
          Answers a Predicate1 that when evaluated sends to its first argument the message previously sent to the last stubbed type, passing its second argument as the first message argument.
static
<A,B,C> net.sf.staccatocommons.defs.function.Function2<A,B,C>
lambda2(C returnType)
          Answers a Function2 that when applied sends to its first argument the message previously sent to the last stubbed type, passing its second argument as the first message argument.
static
<A,B,C,D> net.sf.staccatocommons.defs.function.Function3<A,B,C,D>
lambda3(D returnType)
          Answers a Function3 that when applied sends to its first argument the message previously sent to the last stubbed type, passing its second argument as the first message argument, and its third argument to the second message argument.
static net.sf.staccatocommons.defs.predicate.Predicate ulambda(boolean returnType)
          Same that lambda(boolean), but discarding type parameters
static net.sf.staccatocommons.defs.function.Function ulambda(Object returnType)
          Same that lambda(Object), but discarding type parameters
static net.sf.staccatocommons.defs.predicate.Predicate2 ulambda2(boolean returnType)
          Same that lambda2(boolean), but discarding type parameters
static net.sf.staccatocommons.defs.function.Function2 ulambda2(Object returnType)
          Same that lambda2(Object), but discarding type parameters
static net.sf.staccatocommons.defs.function.Function3 ulambda3(Object returnType)
          Same that lambda3(Object), but discarding type parameters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_

public static final Object _
Equivalent to _(Object.class)

Constructor Detail

Lambda

public Lambda()
Method Detail

$

public static <A> A $(Class<A> clazz)
Stubs a type for creating a lambda. The resulting stub must receive one and only one message. Not all LambdaFactorys can stub all types - ie, stubbing interfaces, concrete classes or final classes. Their stubbing capabilities depend on the ProxyFactory passed as constructor argument to this LambdaFactory

Type Parameters:
A -
Parameters:
clazz - the type to stub
Returns:
a new stub
See Also:
LambdaFactory.$(java.lang.Class)

lambda

public static <A,B> net.sf.staccatocommons.defs.function.Function<A,B> lambda(B returnType)
Answers a Function that when applied sends to its argument the message previously sent to the last stubbed type. Refer to the use cases described in Lambda

Parameters:
returnType - meaningless, this argument is simply ignored
Returns:
a new Function
See Also:
LambdaFactory.lambda(java.lang.Object)

ulambda

public static net.sf.staccatocommons.defs.function.Function ulambda(Object returnType)
Same that lambda(Object), but discarding type parameters


lambda

public static <A> net.sf.staccatocommons.defs.predicate.Predicate<A> lambda(boolean returnType)
Answers a Predicate that when evaluated sends to its argument the message previously sent to the last stubbed type. Refer to the use cases described in Lambda

Parameters:
returnType - meaningless, this argument is simply ignored
Returns:
a new Predicate
See Also:
Lambda, LambdaFactory.lambda(boolean)

ulambda

public static net.sf.staccatocommons.defs.predicate.Predicate ulambda(boolean returnType)
Same that lambda(boolean), but discarding type parameters


lambda2

public static <A,B,C> net.sf.staccatocommons.defs.function.Function2<A,B,C> lambda2(C returnType)
Answers a Function2 that when applied sends to its first argument the message previously sent to the last stubbed type, passing its second argument as the first message argument. Refer to the use cases described in Lambda

Parameters:
returnType - meaningless, this argument is simply ignored
Returns:
a new Function2
See Also:
LambdaFactory.lambda2(java.lang.Object)

ulambda2

public static net.sf.staccatocommons.defs.function.Function2 ulambda2(Object returnType)
Same that lambda2(Object), but discarding type parameters


lambda2

public static <A,B> net.sf.staccatocommons.defs.predicate.Predicate2<A,B> lambda2(boolean returnType)
Answers a Predicate1 that when evaluated sends to its first argument the message previously sent to the last stubbed type, passing its second argument as the first message argument. Refer to the use cases described in Lambda

Parameters:
returnType - meaningless, this argument is simply ignored
Returns:
a new Predicate
See Also:
Lambda, LambdaFactory.lambda2(boolean)

ulambda2

public static net.sf.staccatocommons.defs.predicate.Predicate2 ulambda2(boolean returnType)
Same that lambda2(boolean), but discarding type parameters


lambda3

public static <A,B,C,D> net.sf.staccatocommons.defs.function.Function3<A,B,C,D> lambda3(D returnType)
Answers a Function3 that when applied sends to its first argument the message previously sent to the last stubbed type, passing its second argument as the first message argument, and its third argument to the second message argument. Refer to the use cases described in Lambda

Parameters:
returnType - meaningless, this argument is simply ignored
Returns:
a new Function3
See Also:
LambdaFactory.lambda3(java.lang.Object)

ulambda3

public static net.sf.staccatocommons.defs.function.Function3 ulambda3(Object returnType)
Same that lambda3(Object), but discarding type parameters


factory

public static LambdaFactory factory()
Answers a new LambdaFactory that is capable of stubbing interfaces and non-final classes

Returns:
a new LambdaFactory

_

public static <A> A _(Class<A> clazz)
Answers a placeholder for lambdas arguments with arity > 1.

Type Parameters:
A -
Parameters:
clazz -
Returns:
a placeholder for a lambda argument. Its value is opaque to client code


Copyright © 2011-2012 StaccatoCommons. All Rights Reserved.