News: Powerful Fluent Interfaces is Now Easier than Ever: op4j 1.0 Released.

  1. op4j is a Java library aimed at improving quality, semantics, cleanness and readability of Java code, especially auxiliary code like data conversion, structure iteration, filtering, mapping, etc.

    op4j is a powerful implementation of the Fluent Interface style of coding. It allows you to create chained expressions which apply both predefined or user-defined functions to your objects in a fluid and readable way. This improves the way your code looks and greatly reduces the complexity of executing auxiliary low-level tasks in the highly bureaucratic, statically -and strongly- typed language that Java is.

    But also, op4j is a huge repository functions with more than one thousand different parameterizations ready to be used in your expressions. From regular expression matching on Strings to locale-savvy number conversion, to easy Calendar creation.

    Here's an example of just how easy op4j is to use.

    Say you wanted to insert a new element into an array at a specific position.


    Our minerals variable, of type String[], contains:

    // minerals == ARRAY "Talc", "Quartz", "Diamond"
    ...and we want to add "Fluorite" at the second position of the array, as it is harder than talc but softer than quartz:

    // minerals == ARRAY "Talc", "Fluorite", "Quartz", "Diamond"

    How do you do it with o4j?
    Use the insert(...) action on the array, specifying the position at which the new element will be added to the array. Positions start with zero.

    minerals = Op.on(minerals).insert(1 ,"Fluorite").get();

    This recipe is also valid for lists, maps and sets.

    If we wanted to add more than one element, we could have done:

    minerals = Op.on(minerals).insertAll(1, "Fluorite", "Apatite").get();

    And just compare this to writing a bunch of Java code:

    minerals = Arrays.copyOf(minerals, minerals.length + 1); 
    for (int i = (minerals.length - 1), z = 1; i > z; i--) { 
       minerals[i] = minerals[i - 1]; 

    minerals[1] = "Fluorite";

    Not bad, eh?

    op4j is open source, and it is distributed under the terms of the Apache License 2.0.

    See the project web site at http://www.op4j.org

    See the example recipes blog at http://www.bendingthejavaspoon.com

  2. Pretty interesting to see this kind of jQuery-style programming spilling over to Java-land! I will definitely try to wrap my head around whether this might be useful in future projects. Have there been any other libraries doing things this way, or is this a "first"?