001package com.randomnoun.common.jexl.eval.function;
002
003/* (c) 2013-2018 randomnoun. All Rights Reserved. This work is licensed under a
004 * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
005 */
006
007import java.util.List;
008
009import com.randomnoun.common.jexl.eval.EvalContext;
010import com.randomnoun.common.jexl.eval.EvalException;
011import com.randomnoun.common.jexl.eval.EvalFunction;
012
013/** Defines a endsWith() function.
014 *
015 * Returns true if the first argument to this function ends with the second argument.
016 *
017 * <p>The result of the function is of type Boolean.
018 */
019public class EndsWithFunction
020    implements EvalFunction
021{
022    /** Implements the function as per the class description. */
023    public Object evaluate(String functionName, EvalContext context, List<Object> arguments)
024        throws EvalException
025    {
026        if (arguments.size() != 2) { throw new EvalException(functionName + "() must contain two parameters"); }
027        if (!(arguments.get(0) instanceof String)) {
028            throw new EvalException(functionName + "() parameter 1 must be a string type");
029        }
030        if (!(arguments.get(1) instanceof String)) {
031            throw new EvalException(functionName + "() parameter 2 must be a string type");
032        }
033
034        String arg0 = (String)arguments.get(0);
035        String arg1 = (String)arguments.get(1);
036        if (arg0 == null) {
037            throw new EvalException(functionName + "() first parameter cannot be null");
038        }
039        if (arg1 == null) {
040            throw new EvalException(functionName + "() second parameter cannot be null");
041        }
042
043        return Boolean.valueOf(arg0.endsWith(arg1));
044    }
045}