Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.op4j.ognl.functions.FnOgnl Maven / Gradle / Ivy
/*
* =============================================================================
*
* Copyright (c) 2010, The OP4J team (http://www.op4j.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.op4j.ognl.functions;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import ognl.OgnlException;
import org.apache.commons.lang.Validate;
import org.javaruntype.type.Type;
import org.javaruntype.type.Types;
import org.op4j.exceptions.ExecutionException;
import org.op4j.functions.ExecCtx;
import org.op4j.functions.Function;
import org.op4j.util.VarArgsUtil;
/**
*
* Function hub class for functions that evaluate OGNL expressions and return their results.
*
*
* @author Daniel Fernández
*
* @since 1.0
*/
public final class FnOgnl extends Function {
private static final OgnlExpressionMap parsedExpressionsByExpression = new OgnlExpressionMap();
public static final String TARGET_VARIABLE_NAME = "target";
public static final String PARAM_VARIABLE_NAME = "param";
public static final String INDEX_VARIABLE_NAME = "index";
private final Type resultType;
private final String ognlExpression;
private final Object[] parameters;
/**
*
* Evaluates an OGNL expression which returns Object. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForObject(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.OBJECT, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForObject(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function obj(final String ognlExpression, final Object... optionalParameters) {
return evalForObject(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns an object of type R . This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param resultType the type of the object returned by the expression
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalFor(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(resultType, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalFor(Type, String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function obj(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return evalFor(resultType, ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns BigInteger. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForBigInteger(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.BIG_INTEGER, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForBigInteger(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function bigInteger(final String ognlExpression, final Object... optionalParameters) {
return evalForBigInteger(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns BigDecimal. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForBigDecimal(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.BIG_DECIMAL, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForBigDecimal(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function bigDecimal(final String ognlExpression, final Object... optionalParameters) {
return evalForBigDecimal(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Boolean. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForBoolean(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.BOOLEAN, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForBoolean(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function bool(final String ognlExpression, final Object... optionalParameters) {
return evalForBoolean(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Byte. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForByte(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.BYTE, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForByte(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function b(final String ognlExpression, final Object... optionalParameters) {
return evalForByte(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Character. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForCharacter(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.CHARACTER, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalCharacter(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function c(final String ognlExpression, final Object... optionalParameters) {
return evalForCharacter(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Calendar. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForCalendar(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.CALENDAR, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForCalendar(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function calendar(final String ognlExpression, final Object... optionalParameters) {
return evalForCalendar(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Date. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForDate(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.DATE, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForDate(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function date(final String ognlExpression, final Object... optionalParameters) {
return evalForDate(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Double. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForDouble(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.DOUBLE, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForDouble(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function d(final String ognlExpression, final Object... optionalParameters) {
return evalForDouble(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Float. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForFloat(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.FLOAT, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForFloat(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function f(final String ognlExpression, final Object... optionalParameters) {
return evalForFloat(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Integer. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForInteger(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.INTEGER, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForInteger(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function i(final String ognlExpression, final Object... optionalParameters) {
return evalForInteger(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Long. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForLong(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.LONG, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForLong(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function l(final String ognlExpression, final Object... optionalParameters) {
return evalForLong(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Short. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForShort(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.SHORT, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForShort(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function shr(final String ognlExpression, final Object... optionalParameters) {
return evalForShort(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns String. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForString(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.STRING, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class, optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForString(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function s(final String ognlExpression, final Object... optionalParameters) {
return evalForString(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns List<String>. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> evalForListOfString(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl>(Types.LIST_OF_STRING, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class,optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForListOfString(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> listOfString(final String ognlExpression, final Object... optionalParameters) {
return evalForListOfString(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Set<String>. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> evalForSetOfString(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl>(Types.SET_OF_STRING, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class,optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForSetOfString(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> setOfString(final String ognlExpression, final Object... optionalParameters) {
return evalForSetOfString(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns String[]. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForArrayOfString(final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.ARRAY_OF_STRING, ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class,optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForArrayOfString(String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function arrayOfString(final String ognlExpression, final Object... optionalParameters) {
return evalForArrayOfString(ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns List<R>. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param resultType the type of the resulting list components
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> evalForListOf(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl>(Types.listOf(resultType), ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class,optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForListOf(Type, String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> listOf(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return evalForListOf(resultType, ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns Set<R>. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param resultType the type of the resulting set components
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> evalForSetOf(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl>(Types.setOf(resultType), ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class,optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForSetOf(Type, String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function> setOf(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return evalForSetOf(resultType, ognlExpression, optionalParameters);
}
/**
*
* Evaluates an OGNL expression which returns R[]. This
* expression can have parameters (specified as the varargs optionalParameters
* method parameter).
*
*
* The following predefined variables can be used inside the expression:
*
*
* #target : the target object (the target object is also the root of the expression)
* #param : the specified parameters, as an array (specific parameters are thus accessible with "#param[0] ", "#param[1] ", ...)
* #index : if the expression is executed during the iteration of an array, list,
* map or set, this variable will hold the iteration index.
*
*
* @param resultType the type of the resulting array components
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function evalForArrayOf(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return new FnOgnl(Types.arrayOf(resultType), ognlExpression, VarArgsUtil.asOptionalObjectArray(Object.class,optionalParameters));
}
/**
*
* Abbreviation for {{@link #evalForArrayOf(Type, String, Object...)}.
*
*
* @since 1.1
*
* @param ognlExpression the OGNL expression
* @param optionalParameters the optional parameters
* @return the result of evaluating the expression
*/
public static Function arrayOf(final Type resultType, final String ognlExpression, final Object... optionalParameters) {
return evalForArrayOf(resultType, ognlExpression, optionalParameters);
}
private FnOgnl(final Type resultType, final String ognlExpression, final Object[] parameters) {
Validate.notNull(resultType, "Result type cannot be null");
Validate.notNull(ognlExpression, "Expression cannot be null");
this.resultType = resultType;
this.ognlExpression = ognlExpression;
this.parameters = parameters;
}
public R execute(final T input, final ExecCtx ctx) throws Exception {
return evalOgnlExpression(this.resultType, this.ognlExpression, input, this.parameters, ctx);
}
@SuppressWarnings("unchecked")
private static X evalOgnlExpression(
final Type resultType, final String ognlExpression, final Object targetObject, final Object parametersObject,
final ExecCtx execCtx) {
Object parsedExpression = parsedExpressionsByExpression.get(ognlExpression);
final Class super X> resultClass = resultType.getRawClass();
if (parsedExpression == null) {
try {
parsedExpression = ognl.Ognl.parseExpression(ognlExpression);
} catch (OgnlException e) {
throw new ExecutionException(e);
}
parsedExpressionsByExpression.put(ognlExpression,parsedExpression);
}
try {
final Map ctx = new HashMap();
ctx.put(TARGET_VARIABLE_NAME, targetObject);
ctx.put(PARAM_VARIABLE_NAME, parametersObject);
ctx.put(INDEX_VARIABLE_NAME, execCtx.getIndex());
final Object result = ognl.Ognl.getValue(parsedExpression, ctx, targetObject);
if (result != null && resultClass != null && !Object.class.equals(resultClass)) {
if (!(resultClass.isAssignableFrom(result.getClass()))) {
throw new IllegalStateException("Result of expression \"" + ognlExpression + "\" is not " +
"assignable from class " + resultClass.getName());
}
}
return (X) result;
} catch (OgnlException e) {
throw new ExecutionException(e);
}
}
}