[15023] | 1 | package ise.antelope.tasks.util.math;
|
---|
| 2 |
|
---|
| 3 | import java.util.Vector;
|
---|
| 4 | import java.util.Enumeration;
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Represents a mathematical operation.
|
---|
| 8 | * <p>Copyright 2003, Dale Anson, all rights reserved
|
---|
| 9 | * @author Dale Anson, [email protected]
|
---|
| 10 | */
|
---|
| 11 | public class Op {
|
---|
| 12 | // datatype for the result of this operation
|
---|
| 13 | private String datatype = null;
|
---|
| 14 |
|
---|
| 15 | // storage for the numbers to execute the operation on
|
---|
| 16 | Vector nums = new Vector();
|
---|
| 17 |
|
---|
| 18 | // storage for nested Ops
|
---|
| 19 | Vector ops = new Vector();
|
---|
| 20 |
|
---|
| 21 | // storage for operation
|
---|
| 22 | String operation = null;
|
---|
| 23 |
|
---|
| 24 | // should the StrictMath library be used?
|
---|
| 25 | private boolean _strict = false;
|
---|
| 26 |
|
---|
| 27 | public Op() {
|
---|
| 28 |
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public Op(String op) {
|
---|
| 32 | setOp(op);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public Op(String op, String type) {
|
---|
| 36 | setOp(op);
|
---|
| 37 | setDatatype(type);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Set the operation.
|
---|
| 42 | */
|
---|
| 43 | public void setOp( String op ) {
|
---|
| 44 | if (op.equals("+"))
|
---|
| 45 | operation = "add";
|
---|
| 46 | else if (op.equals("-"))
|
---|
| 47 | operation = "subtract";
|
---|
| 48 | else if (op.equals("*") || op.equals("x"))
|
---|
| 49 | operation = "multiply";
|
---|
| 50 | else if (op.equals("/") || op.equals( "÷" ) )
|
---|
| 51 | operation = "divide";
|
---|
| 52 | else if (op.equals("%") || op.equals("\\"))
|
---|
| 53 | operation = "mod";
|
---|
| 54 | else
|
---|
| 55 | operation = op;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Add a number to this operation. An operation can hold any number of
|
---|
| 60 | * numbers to support formulas like 5 + 4 + 3 + 2 + 1.
|
---|
| 61 | * @param num a number to use in this operation
|
---|
| 62 | */
|
---|
| 63 | public void addConfiguredNum( Num num ) {
|
---|
| 64 | nums.addElement( num );
|
---|
| 65 | //System.out.println("Op.addNum " + num);
|
---|
| 66 |
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Sets the datatype of this calculation. Allowed values are
|
---|
| 71 | * "int", "long", "float", or "double".
|
---|
| 72 | */
|
---|
| 73 | public void setDatatype( String p ) {
|
---|
| 74 | if ( p.equals( "int" ) ||
|
---|
| 75 | p.equals( "long" ) ||
|
---|
| 76 | p.equals( "float" ) ||
|
---|
| 77 | p.equals( "double" ) ||
|
---|
| 78 | p.equals( "bigint") ||
|
---|
| 79 | p.equals( "bigdecimal"))
|
---|
| 80 | datatype = p;
|
---|
| 81 | else
|
---|
| 82 | throw new IllegalArgumentException( "Invalid datatype: " + p +
|
---|
| 83 | ". Must be one of int, long, float, double, bigint, or bigdouble." );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Add a nested operation.
|
---|
| 88 | * @param op the operation to add.
|
---|
| 89 | */
|
---|
| 90 | public void addConfiguredOp( Op op ) {
|
---|
| 91 | if ( datatype != null )
|
---|
| 92 | op.setDatatype( datatype );
|
---|
| 93 | //ops.addElement( op );
|
---|
| 94 | //System.out.println("Op.addConfiguredOp");
|
---|
| 95 | addConfiguredNum(op.calculate());
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Use the StrictMath library.
|
---|
| 100 | */
|
---|
| 101 | public void setStrict( boolean b ) {
|
---|
| 102 | _strict = b;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Perform this operation.
|
---|
| 107 | * @return the value resulting from the calculation as a Num.
|
---|
| 108 | */
|
---|
| 109 | public Num calculate() {
|
---|
| 110 | if ( operation == null )
|
---|
| 111 | throw new RuntimeException( "Operation not specified." );
|
---|
| 112 |
|
---|
| 113 | // calculate nested Ops
|
---|
| 114 | /*
|
---|
| 115 | Enumeration en = ops.elements();
|
---|
| 116 | while ( en.hasMoreElements() ) {
|
---|
| 117 | Op op = ( Op ) en.nextElement();
|
---|
| 118 | if ( datatype != null )
|
---|
| 119 | op.setDatatype( datatype );
|
---|
| 120 | nums.addElement( op.calculate() );
|
---|
| 121 | }
|
---|
| 122 | */
|
---|
| 123 |
|
---|
| 124 | // make an array of operands
|
---|
| 125 | //System.out.println("operation is " + operation + " on these numbers:");
|
---|
| 126 | String[] operands = new String[ nums.size() ];
|
---|
| 127 | Enumeration en = nums.elements();
|
---|
| 128 | int i = 0;
|
---|
| 129 | while ( en.hasMoreElements() ) {
|
---|
| 130 | Num num = (Num)en.nextElement();
|
---|
| 131 | if (datatype != null)
|
---|
| 132 | num.setDatatype(datatype);
|
---|
| 133 | //System.out.println(num.toString());
|
---|
| 134 | operands[ i++ ] = num.toString();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | Math math = new Math(_strict);
|
---|
| 138 |
|
---|
| 139 | Number number = null;
|
---|
| 140 | try {
|
---|
| 141 | number = math.calculate( operation, datatype, operands );
|
---|
| 142 | }
|
---|
| 143 | catch(ArithmeticException e) {
|
---|
| 144 | throw e;
|
---|
| 145 | }
|
---|
| 146 | if (number == null)
|
---|
| 147 | throw new ArithmeticException("math error");
|
---|
| 148 | Num num = new Num();
|
---|
| 149 | num.setValue(number.toString());
|
---|
| 150 | num.setDatatype(datatype);
|
---|
| 151 | return num;
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 | }
|
---|