op4j enables 'chained expressions' to improve the semantics and cleanness of your code while reducing the complexity of executing low-level auxiliary tasks in Java. The chained expressions apply pre-defined or user-defined functions to objects in a fluid and readable manner.
Here is the bullet list of things that op4j can do:
Apply functions on objects.
- Including more than 200 predefined functions for data conversion, string handling, calendar operation, math operations, etc...
Manage structures (arrays, lists, maps and sets), including:
- Easily creating them from their elements (building).
- Modifying them (adding / removing / replacing elements).
- Iterating them, applying functions on each element.
- Transforming them into other structures.
Execute conditional logic (execute actions depending on conditions).
Define functions from expressions, for later use (similar to closures).
op4j contains seven key concepts:
- The operators as a state machine
op4j mainly works with auxiliary code when implementing expressions. Here is one example:
output = Op.on(input).[ACTION].[ACTION]...[ACTION].get();The expressions first call the Op.on(…) or the Fn.on(…) static methods. These methods receive the input object (Op.on) or input type (Fn.on) as a parameter. The documentation should be referenced to understand the variation between "on(…)" method names.
function = Fn.on(inputType).[ACTION].[ACTION]...[ACTION].get();
A chained method executed within an expression is called an action - another key concept. They go between the "on(...)" and the "get()" methods as you can see from this example:
String values = ...;The example contains three chained actions: toList, forEach and exec
List<Integer> intValueList = Op.on(values).toList().forEach().exec(FnString.toInteger()).get();
op4j is a repository with over 200 functions out-of-box, and these are another key concept. The op4j function is: "an object of a class which implements the org.op4j.functions.IFunction interface." Most of the actions available in operators correspond to to the execution of these predefined functions. For example, the distinct() action on a set operator created for a Set<String> input corresponds to the internal execution of the predefined FnSet.ofString().distinct() function (which implements IFunction<Set<String>,Set<String>>).
For more information on the key concepts, go to op4j's "Basics" page. You can also read the project's blog: "Bending the Java Spoon" for short examples on how to use op4j. op4j is open source, and it is distributed under the terms of the Apache License 2.0.