1 7 8 package javax.management; 9 10 11 18 class BinaryOpValueExp extends QueryEval implements ValueExp { 19 20 21 private static final long serialVersionUID = 1216286847881456786L; 22 23 26 private int op; 27 28 31 private ValueExp exp1; 32 33 36 private ValueExp exp2; 37 38 39 42 public BinaryOpValueExp() { 43 } 44 45 49 public BinaryOpValueExp(int o, ValueExp v1, ValueExp v2) { 50 op = o; 51 exp1 = v1; 52 exp2 = v2; 53 } 54 55 56 59 public int getOperator() { 60 return op; 61 } 62 63 66 public ValueExp getLeftValue() { 67 return exp1; 68 } 69 70 73 public ValueExp getRightValue() { 74 return exp2; 75 } 76 77 89 public ValueExp apply(ObjectName name) throws BadStringOperationException , BadBinaryOpValueExpException , 90 BadAttributeValueExpException , InvalidApplicationException { 91 ValueExp val1 = exp1.apply(name); 92 ValueExp val2 = exp2.apply(name); 93 String sval1; 94 String sval2; 95 double dval1; 96 double dval2; 97 long lval1; 98 long lval2; 99 boolean numeric = val1 instanceof NumericValueExp ; 100 101 if (numeric) { 102 if (((NumericValueExp )val1).isLong()) { 103 lval1 = ((NumericValueExp )val1).longValue(); 104 lval2 = ((NumericValueExp )val2).longValue(); 105 106 switch (op) { 107 case Query.PLUS: 108 return Query.value(lval1 + lval2); 109 case Query.TIMES: 110 return Query.value(lval1 * lval2); 111 case Query.MINUS: 112 return Query.value(lval1 - lval2); 113 case Query.DIV: 114 return Query.value(lval1 / lval2); 115 } 116 117 } else { 118 dval1 = ((NumericValueExp )val1).doubleValue(); 119 dval2 = ((NumericValueExp )val2).doubleValue(); 120 121 switch (op) { 122 case Query.PLUS: 123 return Query.value(dval1 + dval2); 124 case Query.TIMES: 125 return Query.value(dval1 * dval2); 126 case Query.MINUS: 127 return Query.value(dval1 - dval2); 128 case Query.DIV: 129 return Query.value(dval1 / dval2); 130 } 131 } 132 } else { 133 sval1 = ((StringValueExp )val1).getValue(); 134 sval2 = ((StringValueExp )val2).getValue(); 135 136 switch (op) { 137 case Query.PLUS: 138 return new StringValueExp (sval1 + sval2); 139 default: 140 throw new BadStringOperationException (opString()); 141 } 142 } 143 144 throw new BadBinaryOpValueExpException (this); 145 } 146 147 150 public String toString() { 151 try { 152 return exp1 + " " + opString() + " " + exp2; 153 } catch (BadBinaryOpValueExpException ex) { 154 return "invalid expression"; 155 } 156 } 157 158 private String opString() throws BadBinaryOpValueExpException { 159 switch (op) { 160 case Query.PLUS: 161 return "+"; 162 case Query.TIMES: 163 return "*"; 164 case Query.MINUS: 165 return "-"; 166 case Query.DIV: 167 return "/"; 168 } 169 170 throw new BadBinaryOpValueExpException (this); 171 } 172 173 } 174 | Popular Tags |