Coverage Report - org.apache.commons.el.BinaryOperatorExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
BinaryOperatorExpression
37%
14/37
40%
4/10
1.5
 
 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  
 import java.util.Iterator;
 21  
 import java.util.ArrayList;
 22  
 
 23  
 import javax.servlet.jsp.el.ELException;
 24  
 import javax.servlet.jsp.el.FunctionMapper;
 25  
 import javax.servlet.jsp.el.VariableResolver;
 26  
 
 27  
 /**
 28  
  *
 29  
  * <p>An expression representing a binary operator on a value
 30  
  * 
 31  
  * @author Nathan Abramson - Art Technology Group
 32  
  * @author Shawn Bayern
 33  
  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
 34  
  **/
 35  
 
 36  
 public class BinaryOperatorExpression
 37  
   extends Expression
 38  
 {
 39  
   //-------------------------------------
 40  
   // Properties
 41  
   //-------------------------------------
 42  
   // property expression
 43  
 
 44  
   Expression mExpression;
 45  
   public Expression getExpression ()
 46  0
   { return mExpression; }
 47  
   public void setExpression (Expression pExpression)
 48  0
   { mExpression = pExpression; }
 49  
 
 50  
   //-------------------------------------
 51  
   // property operators
 52  
 
 53  
   List mOperators;
 54  
   public List getOperators ()
 55  0
   { return mOperators; }
 56  
   public void setOperators (List pOperators)
 57  0
   { mOperators = pOperators; }
 58  
 
 59  
   //-------------------------------------
 60  
   // property expressions
 61  
 
 62  
   List mExpressions;
 63  
   public List getExpressions ()
 64  0
   { return mExpressions; }
 65  
   public void setExpressions (List pExpressions)
 66  0
   { mExpressions = pExpressions; }
 67  
 
 68  
   //-------------------------------------
 69  
   /**
 70  
    *
 71  
    * Constructor
 72  
    **/
 73  
   public BinaryOperatorExpression (Expression pExpression,
 74  
                                    List pOperators,
 75  
                                    List pExpressions)
 76  2
   {
 77  2
     mExpression = pExpression;
 78  2
     mOperators = pOperators;
 79  2
     mExpressions = pExpressions;
 80  2
   }
 81  
 
 82  
   //-------------------------------------
 83  
   // Expression methods
 84  
   //-------------------------------------
 85  
   /**
 86  
    *
 87  
    * Returns the expression in the expression language syntax
 88  
    **/
 89  
   public String getExpressionString ()
 90  
   {
 91  0
     StringBuffer buf = new StringBuffer ();
 92  0
     buf.append ("(");
 93  0
     buf.append (mExpression.getExpressionString ());
 94  0
     for (int i = 0, size = mOperators.size(); i < size; i++) {
 95  0
       BinaryOperator operator = (BinaryOperator) mOperators.get (i);
 96  0
       Expression expression = (Expression) mExpressions.get (i);
 97  0
       buf.append (" ");
 98  0
       buf.append (operator.getOperatorSymbol ());
 99  0
       buf.append (" ");
 100  0
       buf.append (expression.getExpressionString ());
 101  
     }
 102  0
     buf.append (")");
 103  
 
 104  0
     return buf.toString ();
 105  
   }
 106  
 
 107  
   //-------------------------------------
 108  
   /**
 109  
    *
 110  
    * Evaluates to the literal value
 111  
    **/
 112  
   public Object evaluate (VariableResolver pResolver,
 113  
                           FunctionMapper functions)
 114  
     throws ELException
 115  
   {
 116  2
     Object value = mExpression.evaluate (pResolver, functions);
 117  4
     for (int i = 0, size = mOperators.size(); i < size; i++) {
 118  2
       BinaryOperator operator = (BinaryOperator) mOperators.get (i);
 119  
 
 120  
       // For the And/Or operators, we need to coerce to a boolean
 121  
       // before testing if we shouldEvaluate
 122  2
       if (operator.shouldCoerceToBoolean ()) {
 123  0
         value = Coercions.coerceToBoolean (value);
 124  
       }
 125  
 
 126  2
       if (operator.shouldEvaluate (value)) {
 127  2
         Expression expression = (Expression) mExpressions.get (i);
 128  2
         Object nextValue = expression.evaluate (pResolver,
 129  
                                                 functions);
 130  
 
 131  2
         value = operator.apply (value, nextValue);
 132  
       }
 133  
     }
 134  2
     return value;
 135  
   }
 136  
 
 137  
     public Expression bindFunctions(final FunctionMapper functions) throws ELException {
 138  0
         final List args = new ArrayList(mExpressions.size());
 139  0
         for (Iterator argIter = mExpressions.iterator(); argIter.hasNext();) {
 140  0
             args.add(((Expression)argIter.next()).bindFunctions(functions));
 141  
         }
 142  
         // it would be nice if we knew for sure that the operators list
 143  
         // was immutable, but we'll just assume so for now.
 144  0
         return new BinaryOperatorExpression(
 145  
                 mExpression.bindFunctions(functions),
 146  
                 mOperators,
 147  
                 args);
 148  
     }
 149  
 
 150  
   //-------------------------------------
 151  
 }