1. /*
  2. * @(#)BinaryRelQueryExp.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 BinaryRelQueryExp extends QueryEval implements QueryExp {
  16. /* Serial version */
  17. private static final long serialVersionUID = -5690656271650491000L;
  18. /**
  19. * @serial The operator
  20. */
  21. private int relOp;
  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 BinaryRelQueryExp() {
  34. }
  35. /**
  36. * Creates a new BinaryRelQueryExp with operator op applied on v1 and
  37. * v2 values.
  38. */
  39. public BinaryRelQueryExp(int op, ValueExp v1, ValueExp v2) {
  40. relOp = op;
  41. exp1 = v1;
  42. exp2 = v2;
  43. }
  44. /**
  45. * Returns the operator of the query.
  46. */
  47. public int getOperator() {
  48. return relOp;
  49. }
  50. /**
  51. * Returns the left value of the query.
  52. */
  53. public ValueExp getLeftValue() {
  54. return exp1;
  55. }
  56. /**
  57. * Returns the right value of the query.
  58. */
  59. public ValueExp getRightValue() {
  60. return exp2;
  61. }
  62. /**
  63. * Applies the BinaryRelQueryExp on an MBean.
  64. *
  65. * @param name The name of the MBean on which the BinaryRelQueryExp will be applied.
  66. *
  67. * @return True if the query was successfully applied to the MBean, false otherwise.
  68. *
  69. * @exception BadStringOperationException
  70. * @exception BadBinaryOpValueExpException
  71. * @exception BadAttributeValueExpException
  72. * @exception InvalidApplicationException
  73. */
  74. public boolean apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
  75. BadAttributeValueExpException, InvalidApplicationException {
  76. Object val1 = exp1.apply(name);
  77. Object val2 = exp2.apply(name);
  78. String sval1;
  79. String sval2;
  80. double dval1;
  81. double dval2;
  82. long lval1;
  83. long lval2;
  84. boolean bval1;
  85. boolean bval2;
  86. boolean numeric = val1 instanceof NumericValueExp;
  87. boolean bool = val1 instanceof BooleanValueExp;
  88. if (numeric) {
  89. if (((NumericValueExp)val1).isLong()) {
  90. lval1 = ((NumericValueExp)val1).longValue();
  91. lval2 = ((NumericValueExp)val2).longValue();
  92. switch (relOp) {
  93. case Query.GT:
  94. return lval1 > lval2;
  95. case Query.LT:
  96. return lval1 < lval2;
  97. case Query.GE:
  98. return lval1 >= lval2;
  99. case Query.LE:
  100. return lval1 <= lval2;
  101. case Query.EQ:
  102. return lval1 == lval2;
  103. }
  104. } else {
  105. dval1 = ((NumericValueExp)val1).doubleValue();
  106. dval2 = ((NumericValueExp)val2).doubleValue();
  107. switch (relOp) {
  108. case Query.GT:
  109. return dval1 > dval2;
  110. case Query.LT:
  111. return dval1 < dval2;
  112. case Query.GE:
  113. return dval1 >= dval2;
  114. case Query.LE:
  115. return dval1 <= dval2;
  116. case Query.EQ:
  117. return dval1 == dval2;
  118. }
  119. }
  120. } else if (bool) {
  121. bval1 = ((BooleanValueExp)val1).getValue().booleanValue();
  122. bval2 = ((BooleanValueExp)val2).getValue().booleanValue();
  123. switch (relOp) {
  124. case Query.GT:
  125. return bval1 && !bval2;
  126. case Query.LT:
  127. return !bval1 && bval2;
  128. case Query.GE:
  129. return bval1 || !bval2;
  130. case Query.LE:
  131. return !bval1 || bval2;
  132. case Query.EQ:
  133. return bval1 == bval2;
  134. }
  135. } else {
  136. sval1 = ((StringValueExp)val1).getValue();
  137. sval2 = ((StringValueExp)val2).getValue();
  138. switch (relOp) {
  139. case Query.GT:
  140. return sval1.compareTo(sval2) > 0;
  141. case Query.LT:
  142. return sval1.compareTo(sval2) < 0;
  143. case Query.GE:
  144. return sval1.compareTo(sval2) >= 0;
  145. case Query.LE:
  146. return sval1.compareTo(sval2) <= 0;
  147. case Query.EQ:
  148. return sval1.compareTo(sval2) == 0;
  149. }
  150. }
  151. return false;
  152. }
  153. /**
  154. * Returns the string representing the object.
  155. */
  156. public String toString() {
  157. return "(" + exp1 + ") " + relOpString() + " (" + exp2 + ")";
  158. }
  159. private String relOpString() {
  160. switch (relOp) {
  161. case Query.GT:
  162. return ">";
  163. case Query.LT:
  164. return "<";
  165. case Query.GE:
  166. return ">=";
  167. case Query.LE:
  168. return "<=";
  169. case Query.EQ:
  170. return "=";
  171. }
  172. return "=";
  173. }
  174. }