1. /*
  2. * @(#)BetweenQueryExp.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. * relations.
  11. * @serial include
  12. *
  13. * @since 1.5
  14. */
  15. class BetweenQueryExp extends QueryEval implements QueryExp {
  16. /* Serial version */
  17. private static final long serialVersionUID = -2933597532866307444L;
  18. /**
  19. * @serial The checked value
  20. */
  21. private ValueExp exp1;
  22. /**
  23. * @serial The lower bound value
  24. */
  25. private ValueExp exp2;
  26. /**
  27. * @serial The upper bound value
  28. */
  29. private ValueExp exp3;
  30. /**
  31. * Basic Constructor.
  32. */
  33. public BetweenQueryExp() {
  34. }
  35. /**
  36. * Creates a new BetweenQueryExp with v1 checked value, v2 lower bound
  37. * and v3 upper bound values.
  38. */
  39. public BetweenQueryExp(ValueExp v1, ValueExp v2, ValueExp v3) {
  40. exp1 = v1;
  41. exp2 = v2;
  42. exp3 = v3;
  43. }
  44. /**
  45. * Returns the checked value of the query.
  46. */
  47. public ValueExp getCheckedValue() {
  48. return exp1;
  49. }
  50. /**
  51. * Returns the lower bound value of the query.
  52. */
  53. public ValueExp getLowerBound() {
  54. return exp2;
  55. }
  56. /**
  57. * Returns the upper bound value of the query.
  58. */
  59. public ValueExp getUpperBound() {
  60. return exp3;
  61. }
  62. /**
  63. * Applies the BetweenQueryExp on an MBean.
  64. *
  65. * @param name The name of the MBean on which the BetweenQueryExp 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. ValueExp val1 = exp1.apply(name);
  77. ValueExp val2 = exp2.apply(name);
  78. ValueExp val3 = exp3.apply(name);
  79. String sval1;
  80. String sval2;
  81. String sval3;
  82. double dval1;
  83. double dval2;
  84. double dval3;
  85. long lval1;
  86. long lval2;
  87. long lval3;
  88. boolean numeric = val1 instanceof NumericValueExp;
  89. if (numeric) {
  90. if (((NumericValueExp)val1).isLong()) {
  91. lval1 = ((NumericValueExp)val1).longValue();
  92. lval2 = ((NumericValueExp)val2).longValue();
  93. lval3 = ((NumericValueExp)val3).longValue();
  94. return lval2 <= lval1 && lval1 <= lval3;
  95. } else {
  96. dval1 = ((NumericValueExp)val1).doubleValue();
  97. dval2 = ((NumericValueExp)val2).doubleValue();
  98. dval3 = ((NumericValueExp)val3).doubleValue();
  99. return dval2 <= dval1 && dval1 <= dval3;
  100. }
  101. } else {
  102. sval1 = ((StringValueExp)val1).toString();
  103. sval2 = ((StringValueExp)val2).toString();
  104. sval3 = ((StringValueExp)val3).toString();
  105. return sval2.compareTo(sval1) <= 0 && sval1.compareTo(sval3) <= 0;
  106. }
  107. }
  108. /**
  109. * Returns the string representing the object.
  110. */
  111. public String toString() {
  112. return "(" + exp1 + ") between (" + exp2 + ") and (" + exp3 + ")";
  113. }
  114. }