View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.el;
18  
19  import javax.servlet.jsp.el.ELException;
20  import javax.servlet.jsp.el.FunctionMapper;
21  import javax.servlet.jsp.el.VariableResolver;
22  
23  /**
24   *
25   * <p>Represents a conditional expression.  I've decided not to produce an
26   * abstract base "TernaryOperatorExpression" class since (a) future ternary
27   * operators are unlikely and (b) it's not clear that there would be a
28   * meaningful way to abstract them.  (For instance, would they all be right-
29   * associative?  Would they all have two fixed operator symbols?)
30   * 
31   * @author Shawn Bayern
32   **/
33  
34  public class ConditionalExpression
35    extends Expression
36  {
37    //-------------------------------------
38    // Properties
39    //-------------------------------------
40    // property condition
41  
42    Expression mCondition;
43    public Expression getCondition ()
44    { return mCondition; }
45    public void setCondition (Expression pCondition)
46    { mCondition = pCondition; }
47  
48    //-------------------------------------
49    // property trueBranch
50  
51    Expression mTrueBranch;
52    public Expression getTrueBranch ()
53    { return mTrueBranch; }
54    public void setTrueBranch (Expression pTrueBranch)
55    { mTrueBranch = pTrueBranch; }
56  
57    //-------------------------------------
58    // property falseBranch
59  
60    Expression mFalseBranch;
61    public Expression getFalseBranch ()
62    { return mFalseBranch; }
63    public void setFalseBranch (Expression pFalseBranch)
64    { mFalseBranch = pFalseBranch; }
65  
66    //-------------------------------------
67    /**
68     *
69     * Constructor
70     **/
71    public ConditionalExpression (Expression pCondition,
72  				Expression pTrueBranch,
73  				Expression pFalseBranch)
74    {
75      mCondition = pCondition;
76      mTrueBranch = pTrueBranch;
77      mFalseBranch = pFalseBranch;
78    }
79  
80    //-------------------------------------
81    // Expression methods
82    //-------------------------------------
83    /**
84     *
85     * Returns the expression in the expression language syntax
86     **/
87    public String getExpressionString ()
88    {
89      return
90        "( " + mCondition.getExpressionString() + " ? " + 
91        mTrueBranch.getExpressionString() + " : " +
92        mFalseBranch.getExpressionString() + " )";
93    }
94  
95    //-------------------------------------
96    /**
97     *
98     * Evaluates the conditional expression and returns the literal result
99     **/
100   public Object evaluate (VariableResolver vr,
101 			  FunctionMapper f)
102     throws ELException
103   {
104     // first, evaluate the condition (and coerce the result to a boolean value)
105     boolean condition =
106       Coercions.coerceToBoolean(
107         mCondition.evaluate(vr, f)).booleanValue();
108 
109     // then, use this boolean to branch appropriately
110     if (condition)
111       return mTrueBranch.evaluate(vr, f);
112     else
113       return mFalseBranch.evaluate(vr, f);
114   }
115 
116   public Expression bindFunctions(final FunctionMapper functions) throws ELException {
117        return new ConditionalExpression(
118                mCondition.bindFunctions(functions),
119                mTrueBranch.bindFunctions(functions),
120                mFalseBranch.bindFunctions(functions));
121   }
122 
123   //-------------------------------------
124 }