op4j 1.0 Released and Ready for Spoon Bending

DZone 's Guide to

op4j 1.0 Released and Ready for Spoon Bending

· Java Zone ·
Free Resource
The "developer happiness tool" has finally arrived at (GA) version 1.0.  Of course, I'm referring to op4j (pronounced "op-ah"), which is a powerful implementation of the Fluent Interface concept.  It is a Java library that helps you polish the quality and readability of Java code with specific emphasis on auxiliary code such as structure iteration, filtering, mapping, data conversion, and more.  op4j is also a large repository for hundreds of functions with over one thousand different parameterizations.

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).

...and more.

op4j contains seven key concepts:

  • Expressions
  • Actions
  • Operators
  • Targets
  • The operators as a state machine
  • Functions
  • Types

op4j mainly works with auxiliary code when implementing expressions.  Here is one example:
output = Op.on(input).[ACTION].[ACTION]...[ACTION].get();
function = Fn.on(inputType).[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.

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 = ...;
List<Integer> intValueList = Op.on(values).toList().forEach().exec(FnString.toInteger()).get();
The example contains three chained actions: toList, forEach and exec

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.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}