1. /*
  2. * @(#)BinaryOpValueExp.java 4.17 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management;
  8. /**
  9. * This class is used by the query-building mechanism to represent binary
  10. * operations.
  11. * @serial include
  12. *
  13. * @since 1.5
  14. */
  15. class BinaryOpValueExp extends QueryEval implements ValueExp {
  16. /* Serial version */
  17. private static final long serialVersionUID = 1216286847881456786L;
  18. /**
  19. * @serial The operator
  20. */
  21. private int op;
  22. /**
  23. * @serial The first value
  24. */
  25. private ValueExp exp1;
  26. /**
  27. * @serial The second value
  28. */
  29. private ValueExp exp2;
  30. /**
  31. * Basic Constructor.
  32. */
  33. public BinaryOpValueExp() {
  34. }
  35. /**
  36. * Creates a new BinaryOpValueExp using operator o applied on v1 and
  37. * v2 values.
  38. */
  39. public BinaryOpValueExp(int o, ValueExp v1, ValueExp v2) {
  40. op = o;
  41. exp1 = v1;
  42. exp2 = v2;
  43. }
  44. /**
  45. * Returns the operator of the value expression.
  46. */
  47. public int getOperator() {
  48. return op;
  49. }
  50. /**
  51. * Returns the left value of the value expression.
  52. */
  53. public ValueExp getLeftValue() {
  54. return exp1;
  55. }
  56. /**
  57. * Returns the right value of the value expression.
  58. */
  59. public ValueExp getRightValue() {
  60. return exp2;
  61. }
  62. /**
  63. * Applies the BinaryOpValueExp on a MBean.
  64. *
  65. * @param name The name of the MBean on which the BinaryOpValueExp will be applied.
  66. *
  67. * @return The ValueExp.
  68. *
  69. * @exception BadStringOperationException
  70. * @exception BadBinaryOpValueExpException
  71. * @exception BadAttributeValueExpException
  72. * @exception InvalidApplicationException
  73. */
  74. public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
  75. BadAttributeValueExpException, InvalidApplicationException {
  76. ValueExp val1 = exp1.apply(name);
  77. ValueExp val2 = exp2.apply(name);
  78. String sval1;
  79. String sval2;
  80. double dval1;
  81. double dval2;
  82. long lval1;
  83. long lval2;
  84. boolean numeric = val1 instanceof NumericValueExp;
  85. if (numeric) {
  86. if (((NumericValueExp)val1).isLong()) {
  87. lval1 = ((NumericValueExp)val1).longValue();
  88. lval2 = ((NumericValueExp)val2).longValue();
  89. switch (op) {
  90. case Query.PLUS:
  91. return Query.value(lval1 + lval2);
  92. case Query.TIMES:
  93. return Query.value(lval1 * lval2);
  94. case Query.MINUS:
  95. return Query.value(lval1 - lval2);
  96. case Query.DIV:
  97. return Query.value(lval1 / lval2);
  98. }
  99. } else {
  100. dval1 = ((NumericValueExp)val1).doubleValue();
  101. dval2 = ((NumericValueExp)val2).doubleValue();
  102. switch (op) {
  103. case Query.PLUS:
  104. return Query.value(dval1 + dval2);
  105. case Query.TIMES:
  106. return Query.value(dval1 * dval2);
  107. case Query.MINUS:
  108. return Query.value(dval1 - dval2);
  109. case Query.DIV:
  110. return Query.value(dval1 / dval2);
  111. }
  112. }
  113. } else {
  114. sval1 = ((StringValueExp)val1).getValue();
  115. sval2 = ((StringValueExp)val2).getValue();
  116. switch (op) {
  117. case Query.PLUS:
  118. return new StringValueExp(sval1 + sval2);
  119. default:
  120. throw new BadStringOperationException(opString());
  121. }
  122. }
  123. throw new BadBinaryOpValueExpException(this);
  124. }
  125. /**
  126. * Returns the string representing the object
  127. */
  128. public String toString() {
  129. try {
  130. return exp1 + " " + opString() + " " + exp2;
  131. } catch (BadBinaryOpValueExpException ex) {
  132. return "invalid expression";
  133. }
  134. }
  135. private String opString() throws BadBinaryOpValueExpException {
  136. switch (op) {
  137. case Query.PLUS:
  138. return "+";
  139. case Query.TIMES:
  140. return "*";
  141. case Query.MINUS:
  142. return "-";
  143. case Query.DIV:
  144. return "/";
  145. }
  146. throw new BadBinaryOpValueExpException(this);
  147. }
  148. }