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