View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.expression.spel.ast;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  
22  import org.springframework.expression.EvaluationException;
23  import org.springframework.expression.Operation;
24  import org.springframework.expression.TypedValue;
25  import org.springframework.expression.spel.ExpressionState;
26  import org.springframework.util.NumberUtils;
27  
28  /**
29   * The power operator.
30   *
31   * @author Andy Clement
32   * @author Giovanni Dall'Oglio Risso
33   * @since 3.0
34   */
35  public class OperatorPower extends Operator {
36  
37  	public OperatorPower(int pos, SpelNodeImpl... operands) {
38  		super("^", pos, operands);
39  	}
40  
41  
42  	@Override
43  	public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
44  		SpelNodeImpl leftOp = getLeftOperand();
45  		SpelNodeImpl rightOp = getRightOperand();
46  
47  		Object leftOperand = leftOp.getValueInternal(state).getValue();
48  		Object rightOperand = rightOp.getValueInternal(state).getValue();
49  
50  		if (leftOperand instanceof Number && rightOperand instanceof Number) {
51  			Number leftNumber = (Number) leftOperand;
52  			Number rightNumber = (Number) rightOperand;
53  
54  			if (leftNumber instanceof BigDecimal) {
55  				BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
56  				return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
57  			}
58  			else if (leftNumber instanceof BigInteger) {
59  				BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
60  				return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
61  			}
62  			else if (leftNumber instanceof Double || rightNumber instanceof Double) {
63  				return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
64  			}
65  			else if (leftNumber instanceof Float || rightNumber instanceof Float) {
66  				return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
67  			}
68  
69  			double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
70  			if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
71  				return new TypedValue((long) d);
72  			}
73  			else {
74  				return new TypedValue((int) d);
75  			}
76  		}
77  
78  		return state.operate(Operation.POWER, leftOperand, rightOperand);
79  	}
80  
81  }