1. /*
  2. * @(#)InQueryExp.java 4.19 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 InQueryExp extends QueryEval implements QueryExp {
  16. /* Serial version */
  17. private static final long serialVersionUID = -5801329450358952434L;
  18. /**
  19. * @serial The {@link ValueExp} to be found
  20. */
  21. private ValueExp val;
  22. /**
  23. * @serial The array of {@link ValueExp} to be searched
  24. */
  25. private ValueExp[] valueList;
  26. /**
  27. * Basic Constructor.
  28. */
  29. public InQueryExp() {
  30. }
  31. /**
  32. * Creates a new InQueryExp with the specified ValueExp to be found in
  33. * a specified array of ValueExp.
  34. */
  35. public InQueryExp(ValueExp v1, ValueExp items[]) {
  36. val = v1;
  37. valueList = items;
  38. }
  39. /**
  40. * Returns the checked value of the query.
  41. */
  42. public ValueExp getCheckedValue() {
  43. return val;
  44. }
  45. /**
  46. * Returns the array of values of the query.
  47. */
  48. public ValueExp[] getExplicitValues() {
  49. return valueList;
  50. }
  51. /**
  52. * Applies the InQueryExp on a MBean.
  53. *
  54. * @param name The name of the MBean on which the InQueryExp will be applied.
  55. *
  56. * @return True if the query was successfully applied to the MBean, false otherwise.
  57. *
  58. * @exception BadStringOperationException
  59. * @exception BadBinaryOpValueExpException
  60. * @exception BadAttributeValueExpException
  61. * @exception InvalidApplicationException
  62. */
  63. public boolean apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
  64. BadAttributeValueExpException, InvalidApplicationException {
  65. if (valueList != null) {
  66. ValueExp v = val.apply(name);
  67. boolean numeric = v instanceof NumericValueExp;
  68. for (int i = 0; i < valueList.length; i++) {
  69. if (numeric) {
  70. if (((NumericValueExp)valueList[i]).doubleValue() ==
  71. ((NumericValueExp)v).doubleValue()) {
  72. return true;
  73. }
  74. } else {
  75. if (((StringValueExp)valueList[i]).getValue().equals(
  76. ((StringValueExp)v).getValue())) {
  77. return true;
  78. }
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84. /**
  85. * Returns the string representing the object.
  86. */
  87. public String toString() {
  88. return val + " in (" + generateValueList() + ")";
  89. }
  90. private String generateValueList() {
  91. if (valueList == null || valueList.length == 0) {
  92. return "";
  93. }
  94. StringBuffer result = new StringBuffer(valueList[0].toString());
  95. for (int i = 1; i < valueList.length; i++) {
  96. result.append(", ");
  97. result.append(valueList[i]);
  98. }
  99. return result.toString();
  100. }
  101. }