Debian Codegraph @

src/main/java/net/sf/antcontrib/math/Operation.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 package net.sf.antcontrib.math;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.DynamicConfigurator;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Vector;
25
26 /**
27 * Class to represent a mathematical operation.
28 *
29 * @author inger
30 */
31
32
33 public class Operation
34 implements Evaluateable, DynamicConfigurator {
35 private String operation = "add";
36 private Vector operands = new Vector();
37 private String datatype = "double";
38 private boolean strict = false;
39
40 private boolean hasLocalOperands = false;
41 private Numeric localOperands[] = new Numeric[5];
42
43 public void setDynamicAttribute(String s, String s1)
44 throws BuildException {
45 throw new BuildException("no dynamic attributes for this element");
46 }
47
48 public Object createDynamicElement(String name)
49 throws BuildException {
50 Operation op = new Operation();
51 op.setOperation(name);
52 operands.add(op);
53 return op;
54 }
55
56 private void setLocalOperand(String value, int index) {
57 hasLocalOperands = true;
58 localOperands[index - 1] = new Numeric();
59 localOperands[index - 1].setValue(value);
60 }
61
62 public void setArg1(String value) {
63 setLocalOperand(value, 1);
64 }
65
66 public void setArg2(String value) {
67 setLocalOperand(value, 2);
68 }
69
70 public void setArg3(String value) {
71 setLocalOperand(value, 3);
72 }
73
74 public void setArg4(String value) {
75 setLocalOperand(value, 4);
76 }
77
78 public void setArg5(String value) {
79 setLocalOperand(value, 5);
80 }
81
82 public void addConfiguredNumeric(Numeric numeric) {
83 if (hasLocalOperands)
84 throw new BuildException("Cannot combine operand attributes with subelements");
85
86 operands.add(numeric);
87 }
88
89 public void addConfiguredOperation(Operation operation) {
90 if (hasLocalOperands)
91 throw new BuildException("Cannot combine operand attributes with subelements");
92
93 operands.add(operation);
94 }
95
96 public void addConfiguredNum(Numeric numeric) {
97 if (hasLocalOperands)
98 throw new BuildException("Cannot combine operand attributes with subelements");
99
100 operands.add(numeric);
101 }
102
103 public void addConfiguredOp(Operation operation) {
104 if (hasLocalOperands)
105 throw new BuildException("Cannot combine operand attributes with subelements");
106
107 operands.add(operation);
108 }
109
110 public void setOp(String operation) {
111 setOperation(operation);
112 }
113
114 public void setOperation(String operation) {
115 if (operation.equals("+"))
116 this.operation = "add";
117 else if (operation.equals("-"))
118 this.operation = "subtract";
119 else if (operation.equals("*"))
120 this.operation = "multiply";
121 else if (operation.equals("/"))
122 this.operation = "divide";
123 else if (operation.equals("%"))
124 this.operation = "mod";
125 else
126 this.operation = operation;
127 }
128
129 public void setDatatype(String datatype) {
130 this.datatype = datatype;
131 }
132
133 public void setStrict(boolean strict) {
134 this.strict = strict;
135 }
136
137 public Number evaluate() {
138 Evaluateable ops[] = null;
139
140 if (hasLocalOperands) {
141 List localOps = new ArrayList();
142 for (int i = 0; i < localOperands.length; i++) {
143 if (localOperands[i] != null)
144 localOps.add(localOperands[i]);
145 }
146
147 ops = (Evaluateable[]) localOps.toArray(new Evaluateable[localOps.size()]);
148 }
149 else {
150 ops = (Evaluateable[]) operands.toArray(new Evaluateable[operands.size()]);
151 }
152
153 return Math.evaluate(operation,
154 datatype,
155 strict,
156 ops);
157 }
158
159 public String toString() {
160 return "Operation[operation=" + operation
161 + ";datatype=" + datatype
162 + ";strict=" + strict
163 + ";localoperands=" + Arrays.asList(localOperands)
164 + ";operands=" + operands
165 + "]";
166 }
167 }