1 /***
2 * IfOperator.java
3 *
4 * $Author: mballesteros $
5 * $Date: 2003/11/28 19:18:03 $
6 * $Revision: 1.1 $
7 */
8 package net.sf.jec.namedop;
9
10 import net.sf.jec.ConversionException;
11 import net.sf.jec.EvaluationException;
12 import net.sf.jec.ObjectConverter;
13
14 /*** IfOperator evaluates a first boolean argument, evaluating and returning the
15 * second or thirth arguments when true or false respectively.
16 *
17 * @author mballesteros
18 */
19 public class IfOperator extends GroupOperator {
20
21 private boolean throwExceptions;
22
23 /*** Creates a new SumOperator
24 */
25 public IfOperator() {
26 }
27
28 /*** Returns a function representation String for this operator
29 */
30 public String toFunctionString() {
31 return "If(" + argOps[0] + ", " + argOps[1] + ", " + argOps[2] + ")";
32 }
33
34 /*** Returns an expression String representation for this operator
35 */
36 public String toExpressionString() {
37 return (nestedOp == null ? "" : nestedOp.toString() + ".")
38 + "if("
39 + argOps[0]
40 + ", "
41 + argOps[1]
42 + ", "
43 + argOps[2]
44 + ")";
45 }
46
47 /*** Returns the associated value to the given context and root context
48 * @param rootCtx The root context, needed for operators that require
49 * expression evaluation from root point. Example: Indexer operators
50 * @param ctx The current context where the operator will work over
51 */
52 protected Object directMap(Object rootCtx, Object ctx)
53 throws EvaluationException {
54 boolean test;
55 try {
56 test = ObjectConverter.toBoolean(argOps[0].apply(rootCtx, ctx));
57 } catch (ConversionException ce) {
58 if (throwExceptions)
59 throw new EvaluationException(
60 "Error converting value: " + ce.getMessage());
61 else
62 return null;
63 }
64 if (test) {
65 return argOps[1].apply(rootCtx, ctx);
66 } else {
67 return argOps[2].apply(rootCtx, ctx);
68 }
69 }
70 }
This page was automatically generated by Maven