Mitch Pronschinske is a Senior Content Analyst at DZone. That means he writes and searches for the finest developer content in the land so that you don't have to. He often eats peanut butter and bananas, likes to make his own ringtones, enjoys card and board games, and is married to an underwear model. Mitch is a DZone Zone Leader and has posted 2573 posts at DZone. You can read more from them at their website. View Full User Profile

op4j 1.0 Released and Ready for Spoon Bending

  • submit to reddit
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.


Nicolas Frankel replied on Mon, 2010/04/19 - 3:26pm

Hi Mitchell,

On Monday 2010/04/19 at 10:29 PM (European time), neither links work :-(

Mitch Pronschinske replied on Mon, 2010/04/19 - 4:50pm in response to: Nicolas Frankel

Hmm.  All the links and code snippets are working for me.  I'll keep an eye on this post.

Andy Jefferson replied on Tue, 2010/04/20 - 10:27am

op4j (pronounced "op-ah")

So log4j should be pronounced 'log-ah'? Please, if the project name has to have an explanation of how to pronounce it then really the emphasis is somehow wrong. It's 'op for jay' :-)

Daniel Fernández replied on Tue, 2010/04/20 - 11:28am in response to: Andy Jefferson

:-D. Obviously, that "op-ah" thing is not a hint on how it would be pronounced it in English language, but rather just the way the authors like to pronounce it, and the "family name" they want the software to be known by. But of course you can pronounce it "op-for-jay" if you prefer! :-)

Jacek Furmankiewicz replied on Wed, 2010/04/21 - 8:32pm

Can you give some performance figures?

 I recall when I looked at lambdaj, they suffered a 400-1200% performance penalty compared to regular Java code.In the end that was the main reason we decided not to use it.

Does op4j have similar issues?

Mitch Pronschinske replied on Mon, 2010/04/26 - 8:12am in response to: Jacek Furmankiewicz

That's a good question, Jacek.  I'm going to talk with the project developers soon and ask them about it.  I might also search for blogs by people who have used op4j.

Mario Fusco replied on Tue, 2010/05/04 - 7:52am in response to: Jacek Furmankiewicz

Did you measure if those performance penalities are really the bottlenecks of your application? I bet they aren't. In memory operations very rarely are.

I heard complains about lambdaj performances many times, but the biggest part of the time they come from people who make a huge use of libraries like Spring and Hibernate that in turns are heavily based on dynamic proxy creation through cglib, exactly as lambdaj does. Don't you think this is weird?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.