21 April 2010

Executing an op4j function directly (without an expression)

Valid since: op4j 1.0

Execute an op4j predefined function (or any other org.op4j.functions.Function<T,R> object) without having to create an operation or function expression.

Our value String contains some accented characters, escaped for HTML:
// value == "Saint-&Eacute;tienne est une ville de France"
...and we want to unescape it so that we obtain a String like:
// value == "Saint-Étienne est une ville de France"

Of course we could create an operation expression for executing the FnString.unescapeHTML function:

value = Op.on(value).exec(FnString.unescapeHTML()).get();

But, if we prefer, we can directly execute the function on the target String:

value = FnString.unescapeHTML().execute(value);

This is because the function object returned by FnString.unescapeHTML() is Function<String,String> so, in fact, if we look at it step by step:

Function<String,String> fn = FnString.unescapeHTML();
value = fn.execute(value);

All Function objects in op4j allow direct execution by means of their execute(...) method.

No comments:

Post a Comment