Functions: FnOgnl

org.op4j.ognl.functions.FnOgnl is a special functions hub class which allows the execution of OGNL (Object-Graph Navigation Language) expressions on target objects.

Here is an example: Imagine your application has a User class which contains a getName() method, returning a String. Let's get the names of all the users in a list:

List<User> users = ...;
List<String> userNames = Op.on(users).map(FnOgnl.evalForString("name")).get();

The evalForX(...) methods in the FnOgnl class give you access to the following functions:

MethodReturned function type
evalForBigDecimal(...)Function<Object,BigDecimal>
evalForBigInteger(...)Function<Object,BigInteger>
evalForBoolean(...)Function<Object,Boolean>
evalForByte(...)Function<Object,Byte>
evalForCalendar(...)Function<Object,Calendar>
evalForCharacter(...)Function<Object,Character>
evalForDate(...)Function<Object,Date>
evalForDouble(...)Function<Object,Double>
evalForFloat(...)Function<Object,Float>
evalForInteger(...)Function<Object,Integer>
evalForLong(...)Function<Object,Long>
evalForObject(...)Function<Object,Object>
evalForShort(...)Function<Object,Short>
evalForString(...)Function<Object,String>
evalFor(Type<T>,...)Function<Object,T>
evalForArrayOf(Type<T>,...)Function<Object,T[]>
evalForArrayOfString(...)Function<Object,String[]>
evalForListOf(Type<T>,...)Function<Object,List<T>>
evalForListOfString(...)Function<Object,List<String>>
evalForSetOf(Type<T>,...)Function<Object,Set<T>>
evalForSetOfString(...)Function<Object,Set<String>>

There also exist more convenient abbreviated methods, which are:

MethodReturned function type
bigDecimal(...)Function<Object,BigDecimal>
bigInteger(...)Function<Object,BigInteger>
bool(...)Function<Object,Boolean>
calendar(...)Function<Object,Calendar>
c(...)Function<Object,Character>
date(...)Function<Object,Date>
d(...)Function<Object,Double>
f(...)Function<Object,Float>
i(...)Function<Object,Integer>
l(...)Function<Object,Long>
obj(...)Function<Object,Object>
shr(...)Function<Object,Short>
s(...)Function<Object,String>
obj(Type<T>,...)Function<Object,T>
arrayOf(Type<T>)Function<Object,T[]>
arrayOfString()Function<Object,String[]>
listOf(Type<T>)Function<Object,List<T>>
listOfString()Function<Object,List<String>>
setOf(Type<T>)Function<Object,Set<T>>
setOfString()Function<Object,Set<String>>

Every evalFor(...) or abbreviated method can be passed parameters, and these can be accessed from the OGNL expression as an array called #param:

List<User> users = ...;
Group group = ...;
...
List<User> usersInGroup = 
    Op.on(users).removeAllFalse(FnOgnl.evalForBoolean("isInGroup(#param[0])", group).get();

Also the iteration index can be accessed when iterating a structure (array, list, map or set), by means of an expression variable called #index:

List<User> users = ...;
...
List<String> indexedNames = 
    Op.on(users).forEach().exec(FnOgnl.s("#index + '$' + #target.name")).get();

#target is the target object itself. This expression variable is useful when the OGNL expression does not use the target object as an expression root (as was the case with, for example, the "name" expression in the first example above).