View Javadoc

1   /*
2    * Copyright 2004-2009 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.springmodules.validation.valang.functions;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  
22  public abstract class AbstractMathFunction implements Function {
23  
24      private Function leftFunction = null;
25  
26      private Function rightFunction = null;
27  
28      private FunctionTemplate template = null;
29  
30      public AbstractMathFunction(Function leftFunction, Function rightFunction, int line, int column) {
31          super();
32  
33          setLeftFunction(leftFunction);
34          setRightFunction(rightFunction);
35          setTemplate(new FunctionTemplate(line, column));
36      }
37  
38      private void setLeftFunction(Function leftFunction) {
39          if (leftFunction == null) {
40              throw new IllegalArgumentException("Left function parameter should not be null!");
41          }
42  
43          this.leftFunction = leftFunction;
44      }
45  
46      public final Function getLeftFunction() {
47          return this.leftFunction;
48      }
49  
50      private void setRightFunction(Function rightFunction) {
51          if (rightFunction == null) {
52              throw new IllegalArgumentException("Right function parameter should not be null!");
53          }
54  
55          this.rightFunction = rightFunction;
56      }
57  
58      public final Function getRightFunction() {
59          return this.rightFunction;
60      }
61  
62      private void setTemplate(FunctionTemplate template) {
63          this.template = template;
64      }
65  
66      protected FunctionTemplate getTemplate() {
67          return this.template;
68      }
69  
70      protected static double transform(Object o) {
71          if (o instanceof BigInteger) {
72              return new BigDecimal((BigInteger) o).doubleValue();
73          } else if (o instanceof BigDecimal) {
74              return ((BigDecimal) o).doubleValue();
75          } else if (o instanceof Number) {
76              return new BigDecimal(o.toString()).doubleValue();
77          } else if (o instanceof String) {
78              return new BigDecimal((String) o).doubleValue();
79          } else {
80              throw new IllegalArgumentException("Could not parse instance of class [" + o.getClass().getName() + "]!");
81          }
82      }
83  }