01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package javax.management;
10:
11: /**
12: * @version $Revision: 1.6 $
13: */
14: class BinaryOpValueExp extends QueryEval implements ValueExp {
15: private static final long serialVersionUID = 1216286847881456786L;
16:
17: private final int op;
18: private final ValueExp exp1;
19: private final ValueExp exp2;
20:
21: BinaryOpValueExp(int op, ValueExp exp1, ValueExp exp2) {
22: this .op = op;
23: this .exp1 = exp1;
24: this .exp2 = exp2;
25: }
26:
27: public void setMBeanServer(MBeanServer server) {
28: super .setMBeanServer(server);
29: if (exp1 != null)
30: exp1.setMBeanServer(server);
31: if (exp2 != null)
32: exp2.setMBeanServer(server);
33: }
34:
35: public ValueExp apply(ObjectName name)
36: throws BadStringOperationException,
37: BadBinaryOpValueExpException,
38: BadAttributeValueExpException, InvalidApplicationException {
39: if (exp1 != null && exp2 != null) {
40: ValueExp val1 = exp1.apply(name);
41: ValueExp val2 = exp2.apply(name);
42:
43: if (val1 instanceof NumericValueExp) {
44: if (val2 instanceof NumericValueExp) {
45: NumericValueExp num1 = (NumericValueExp) val1;
46: NumericValueExp num2 = (NumericValueExp) val2;
47:
48: if (num1.isDouble() || num2.isDouble()) {
49: double d1 = num1.doubleValue();
50: double d2 = num2.doubleValue();
51: switch (op) {
52: case Query.PLUS:
53: return Query.value(d1 + d2);
54: case Query.MINUS:
55: return Query.value(d1 - d2);
56: case Query.TIMES:
57: return Query.value(d1 * d2);
58: case Query.DIV:
59: return Query.value(d1 / d2);
60: }
61: } else {
62: long l1 = num1.longValue();
63: long l2 = num2.longValue();
64: switch (op) {
65: case Query.PLUS:
66: return Query.value(l1 + l2);
67: case Query.MINUS:
68: return Query.value(l1 - l2);
69: case Query.TIMES:
70: return Query.value(l1 * l2);
71: case Query.DIV:
72: return Query.value(l1 / l2);
73: }
74: }
75: } else {
76: throw new BadBinaryOpValueExpException(val2);
77: }
78: } else if (val1 instanceof StringValueExp) {
79: if (val2 instanceof StringValueExp) {
80: String s1 = ((StringValueExp) val1).getValue();
81: String s2 = ((StringValueExp) val2).getValue();
82: switch (op) {
83: case Query.PLUS:
84: return Query.value(String.valueOf(s1)
85: + String.valueOf(s2));
86: default:
87: throw new BadStringOperationException(
88: "Trying to perform an operation on Strings that is not concatenation");
89: }
90: } else {
91: throw new BadBinaryOpValueExpException(val2);
92: }
93: } else {
94: throw new BadBinaryOpValueExpException(val1);
95: }
96: }
97: throw new BadBinaryOpValueExpException(null);
98: }
99: }
|