001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.el; 018 019import java.util.List; 020 021import javax.servlet.jsp.el.ELException; 022import javax.servlet.jsp.el.FunctionMapper; 023import javax.servlet.jsp.el.VariableResolver; 024 025/** 026 * 027 * <p>An expression representing one or more unary operators on a 028 * value 029 * 030 * @author Nathan Abramson - Art Technology Group 031 * @author Shawn Bayern 032 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $ 033 **/ 034 035public class UnaryOperatorExpression 036 extends Expression 037{ 038 //------------------------------------- 039 // Properties 040 //------------------------------------- 041 // property operator 042 043 UnaryOperator mOperator; 044 public UnaryOperator getOperator () 045 { return mOperator; } 046 public void setOperator (UnaryOperator pOperator) 047 { mOperator = pOperator; } 048 049 //------------------------------------- 050 // property operators 051 052 List mOperators; 053 public List getOperators () 054 { return mOperators; } 055 public void setOperators (List pOperators) 056 { mOperators = pOperators; } 057 058 //------------------------------------- 059 // property expression 060 061 Expression mExpression; 062 public Expression getExpression () 063 { return mExpression; } 064 public void setExpression (Expression pExpression) 065 { mExpression = pExpression; } 066 067 //------------------------------------- 068 /** 069 * 070 * Constructor 071 **/ 072 public UnaryOperatorExpression (UnaryOperator pOperator, 073 List pOperators, 074 Expression pExpression) 075 { 076 mOperator = pOperator; 077 mOperators = pOperators; 078 mExpression = pExpression; 079 } 080 081 //------------------------------------- 082 // Expression methods 083 //------------------------------------- 084 /** 085 * 086 * Returns the expression in the expression language syntax 087 **/ 088 public String getExpressionString () 089 { 090 StringBuffer buf = new StringBuffer (); 091 buf.append ("("); 092 if (mOperator != null) { 093 buf.append (mOperator.getOperatorSymbol ()); 094 buf.append (" "); 095 } 096 else { 097 for (int i = 0, size = mOperators.size(); i < size; i++) { 098 UnaryOperator operator = (UnaryOperator) mOperators.get (i); 099 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}