-
Notifications
You must be signed in to change notification settings - Fork 2
Lambda
Staccatissimo-Lambda is a minimalistic and lightweight library for instantiating simple Staccatissimo-Defs' Function
s and Predicate
s in a DSL style, without using anonymous classes. This project is aimed to simplify higher-order programming within Staccatissimo until Java adds lambdas to its language (planned for Java 8)
The entry point to Staccatissimo-Lambda is the Lambda
class, which allows to create functions in a concise and redable way.
There are two ways of using it, this introduction will just show one of them - using a shared lambda factory. The alternative, more performant but a bit more verbose way - using local lambda factories - is discussed in the API docs.
The simplest function to create are one argument functions. Without Staccatissimo-Lambda, implementing such functions require to extend AbstractFunction. For example, a function that return book's ISBN looks like the following:
import net.sf.staccatocommons.lang.function.AbstractFunction;
...
new AbstractFunction<Book, String>() {
public String apply(@NonNull Book book){
return book.getIsbn();
}
};
Which is OK, but is far from being the panacea. In particular, it is not too readable. This can be improved statically importing Lambda
:
import static net.sf.staccatocommons.lambda.*;
...
lambda($(Book.class).getIsbn());
Which, although is odd at first glance, is much more intention revealing.
Functions that can be instantiated using this syntax are not restricted to getters, for example:
lambda($(List.class).size());
lambda($(List.class).contains("hello"));
lambda($(List.class).get(2));
Creating instances of Function2
and Function3
is also quite simple. For example, the following code:
new AbstractFunction2<Book, WeightUnit, BigDecimal>(){
public BigDecimal apply(Book book, WeightUnit unit) {
return book.getWeigth(unit);
}
};
can be rewritten using lambdas:
lambda2($(Book.class).getWeight(_(WeightUnit.class));
Check Javadoc:
- Lambda
- Staccatissimo-Defs functions
- Staccatissimo-Lang abstract functions
- Staccatissimo-Defs predicates
- Staccatissimo-Lang abstract predicates
Add it as a Maven dependency
<dependencies>
<dependency>
<groupId>net.sf.staccatocommons</groupId>
<artifactId>staccatissimo-lambda</artifactId>
<version>${staccatissimo.version}</version>
</dependency>
</dependencies>