Op4j and Lambda-J. For more fluency in Java

I recently blogged about simple constructs, such as Java’s Arrays.asList() and the fact that it is not used often enough: https://blog.jooq.org/javas-arrays-aslist-is-underused/ I like to work with fluent API’s, which are still quite a rare thing in the Java world, compared to other languages that support features such as language extensions, operator overloading, true generics, extension methods, closures, lambda expressions, functional constructs etc etc. But I also like Java’s JVM and the general syntax. And the many libraries that exist. I now came across Op4j, a really nice-looking library: http://www.op4j.org/ It features exactly the kind of constructs I’d like to use every day. Some examples (taken from the documentation):

// Always static import Op.* as the main entry point
import static org.op4j.Op.*;
import static org.op4j.functions.FnString.*;

// Transform an array to uppercase
String[] values = ...;
List upperStrs =

// Convert strings to integers
String[] values = ...;
List intValueList =

There are many more examples on their documentation page, and the API is huge and looks quite extensible: http://www.op4j.org/apidocs/op4j/index.html This library reminds me of Lambda-J, another attempt to bring more fluency to Java by introducing closure/lambda-like expressions in a static way: https://code.google.com/p/lambdaj/ From a first look, Op4j looks more object oriented and straight-forward, though, whereas Lambda-J seems to depend on instrumentation and some advanced usage of reflection. A sample of some non-trivial Lambda-J usage:

Closure println = closure(); {

The above syntax is not easy to grasp. “closure()” seems to modify some static (ThreadLocal) state of the library, which can be used thereafter by the static method “of()”. “of()” in turn can take any type of parameter assuming its identity and type (!). Somehow, you can then “apply” objects of type String to the defined closure:

println.each("one", "two", "three");

Leave a Reply