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 java.util.List;
20  
21  import javax.servlet.jsp.el.ELException;
22  import javax.servlet.jsp.el.FunctionMapper;
23  import javax.servlet.jsp.el.VariableResolver;
24  
25  /**
26   *
27   * <p>An expression representing one or more unary operators on a
28   * value
29   * 
30   * @author Nathan Abramson - Art Technology Group
31   * @author Shawn Bayern
32   * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
33   **/
34  
35  public class UnaryOperatorExpression
36    extends Expression
37  {
38    //-------------------------------------
39    // Properties
40    //-------------------------------------
41    // property operator
42  
43    UnaryOperator mOperator;
44    public UnaryOperator getOperator ()
45    { return mOperator; }
46    public void setOperator (UnaryOperator pOperator)
47    { mOperator = pOperator; }
48  
49    //-------------------------------------
50    // property operators
51  
52    List mOperators;
53    public List getOperators ()
54    { return mOperators; }
55    public void setOperators (List pOperators)
56    { mOperators = pOperators; }
57  
58    //-------------------------------------
59    // property expression
60  
61    Expression mExpression;
62    public Expression getExpression ()
63    { return mExpression; }
64    public void setExpression (Expression pExpression)
65    { mExpression = pExpression; }
66  
67    //-------------------------------------
68    /**
69     *
70     * Constructor
71     **/
72    public UnaryOperatorExpression (UnaryOperator pOperator,
73  				  List pOperators,
74  				  Expression pExpression)
75    {
76      mOperator = pOperator;
77      mOperators = pOperators;
78      mExpression = pExpression;
79    }
80  
81    //-------------------------------------
82    // Expression methods
83    //-------------------------------------
84    /**
85     *
86     * Returns the expression in the expression language syntax
87     **/
88    public String getExpressionString ()
89    {
90      StringBuffer buf = new StringBuffer ();
91      buf.append ("(");
92      if (mOperator != null) {
93        buf.append (mOperator.getOperatorSymbol ());
94        buf.append (" ");
95      }
96      else {
97        for (int i = 0, size = mOperators.size(); i < size; i++) {
98  	UnaryOperator operator = (UnaryOperator) mOperators.get (i);
99  	buf.append (operator.getOperatorSymbol ());
100 	buf.append (" ");
101       }
102     }
103     buf.append (mExpression.getExpressionString ());
104     buf.append (")");
105     return buf.toString ();
106   }
107 
108   //-------------------------------------
109   /**
110    *
111    * Evaluates to the literal value
112    **/
113   public Object evaluate (VariableResolver pResolver, FunctionMapper functions)
114     throws ELException
115   {
116     Object value = mExpression.evaluate (pResolver, functions);
117     if (mOperator != null) {
118       value = mOperator.apply (value);
119     }
120     else {
121       for (int i = mOperators.size () - 1; i >= 0; i--) {
122 	UnaryOperator operator = (UnaryOperator) mOperators.get (i);
123 	value = operator.apply (value);
124       }
125     }
126     return value;
127   }
128 
129   public Expression bindFunctions(final FunctionMapper functions) throws ELException {
130       return new UnaryOperatorExpression(
131               mOperator,
132               mOperators,
133               mExpression.bindFunctions(functions));
134   }
135 
136   //-------------------------------------
137 }