1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }