View Javadoc
1   package com.randomnoun.common.jexl.eval.function;
2   
3   /* (c) 2013-2018 randomnoun. All Rights Reserved. This work is licensed under a
4    * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
5    */
6   
7   import java.util.List;
8   
9   import com.randomnoun.common.jexl.eval.EvalContext;
10  import com.randomnoun.common.jexl.eval.EvalException;
11  import com.randomnoun.common.jexl.eval.EvalFunction;
12  
13  /** Defines a endsWith() function.
14   *
15   * Returns true if the first argument to this function ends with the second argument.
16   *
17   * <p>The result of the function is of type Boolean.
18   */
19  public class EndsWithFunction
20      implements EvalFunction
21  {
22      /** Implements the function as per the class description. */
23      public Object evaluate(String functionName, EvalContext context, List<Object> arguments)
24          throws EvalException
25      {
26          if (arguments.size() != 2) { throw new EvalException(functionName + "() must contain two parameters"); }
27          if (!(arguments.get(0) instanceof String)) {
28              throw new EvalException(functionName + "() parameter 1 must be a string type");
29          }
30          if (!(arguments.get(1) instanceof String)) {
31              throw new EvalException(functionName + "() parameter 2 must be a string type");
32          }
33  
34          String arg0 = (String)arguments.get(0);
35          String arg1 = (String)arguments.get(1);
36          if (arg0 == null) {
37              throw new EvalException(functionName + "() first parameter cannot be null");
38          }
39          if (arg1 == null) {
40              throw new EvalException(functionName + "() second parameter cannot be null");
41          }
42  
43          return Boolean.valueOf(arg0.endsWith(arg1));
44      }
45  }