1. /*
  2. * @(#)Query.java 1.26 04/02/10
  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. * <p>Constructs query object constraints. The static methods provided
  10. * return query expressions that may be used in listing and
  11. * enumerating MBeans. Individual constraint construction methods
  12. * allow only appropriate types as arguments. Composition of calls can
  13. * construct arbitrary nestings of constraints, as the following
  14. * example illustrates:</p>
  15. *
  16. * <pre>
  17. * QueryExp exp = Query.and(Query.gt(Query.attr("age"),Query.value(5)),
  18. * Query.match(Query.attr("name"),
  19. * Query.value("Smith")));
  20. * </pre>
  21. *
  22. * @since 1.5
  23. */
  24. public class Query extends Object {
  25. /**
  26. * A code representing the {@link Query#gt} query. This is chiefly
  27. * of interest for the serialized form of queries.
  28. */
  29. public static final int GT = 0;
  30. /**
  31. * A code representing the {@link Query#lt} query. This is chiefly
  32. * of interest for the serialized form of queries.
  33. */
  34. public static final int LT = 1;
  35. /**
  36. * A code representing the {@link Query#geq} query. This is chiefly
  37. * of interest for the serialized form of queries.
  38. */
  39. public static final int GE = 2;
  40. /**
  41. * A code representing the {@link Query#leq} query. This is chiefly
  42. * of interest for the serialized form of queries.
  43. */
  44. public static final int LE = 3;
  45. /**
  46. * A code representing the {@link Query#eq} query. This is chiefly
  47. * of interest for the serialized form of queries.
  48. */
  49. public static final int EQ = 4;
  50. /**
  51. * A code representing the {@link Query#plus} expression. This
  52. * is chiefly of interest for the serialized form of queries.
  53. */
  54. public static final int PLUS = 0;
  55. /**
  56. * A code representing the {@link Query#minus} expression. This
  57. * is chiefly of interest for the serialized form of queries.
  58. */
  59. public static final int MINUS = 1;
  60. /**
  61. * A code representing the {@link Query#times} expression. This
  62. * is chiefly of interest for the serialized form of queries.
  63. */
  64. public static final int TIMES = 2;
  65. /**
  66. * A code representing the {@link Query#div} expression. This is
  67. * chiefly of interest for the serialized form of queries.
  68. */
  69. public static final int DIV = 3;
  70. /**
  71. * Basic constructor.
  72. */
  73. public Query() {
  74. }
  75. /**
  76. * Returns a query expression that is the conjunction of two other query
  77. * expressions.
  78. *
  79. * @param q1 A query expression.
  80. * @param q2 Another query expression.
  81. *
  82. * @return The conjunction of the two arguments.
  83. */
  84. public static QueryExp and(QueryExp q1, QueryExp q2) {
  85. return new AndQueryExp(q1, q2);
  86. }
  87. /**
  88. * Returns a query expression that is the disjunction of two other query
  89. * expressions.
  90. *
  91. * @param q1 A query expression.
  92. * @param q2 Another query expression.
  93. *
  94. * @return The disjunction of the two arguments.
  95. */
  96. public static QueryExp or(QueryExp q1, QueryExp q2) {
  97. return new OrQueryExp(q1, q2);
  98. }
  99. /**
  100. * Returns a query expression that represents a "greater than" constraint on
  101. * two values.
  102. *
  103. * @param v1 A value expression.
  104. * @param v2 Another value expression.
  105. *
  106. * @return A "greater than" constraint on the arguments.
  107. */
  108. public static QueryExp gt(ValueExp v1, ValueExp v2) {
  109. return new BinaryRelQueryExp(GT, v1, v2);
  110. }
  111. /**
  112. * Returns a query expression that represents a "greater than or equal
  113. * to" constraint on two values.
  114. *
  115. * @param v1 A value expression.
  116. * @param v2 Another value expression.
  117. *
  118. * @return A "greater than or equal to" constraint on the arguments.
  119. */
  120. public static QueryExp geq(ValueExp v1, ValueExp v2) {
  121. return new BinaryRelQueryExp(GE, v1, v2);
  122. }
  123. /**
  124. * Returns a query expression that represents a "less than or equal to"
  125. * constraint on two values.
  126. *
  127. * @param v1 A value expression.
  128. * @param v2 Another value expression.
  129. *
  130. * @return A "less than or equal to" constraint on the arguments.
  131. */
  132. public static QueryExp leq(ValueExp v1, ValueExp v2) {
  133. return new BinaryRelQueryExp(LE, v1, v2);
  134. }
  135. /**
  136. * Returns a query expression that represents a "less than" constraint on
  137. * two values.
  138. *
  139. * @param v1 A value expression.
  140. * @param v2 Another value expression.
  141. *
  142. * @return A "less than" constraint on the arguments.
  143. */
  144. public static QueryExp lt(ValueExp v1, ValueExp v2) {
  145. return new BinaryRelQueryExp(LT, v1, v2);
  146. }
  147. /**
  148. * Returns a query expression that represents an equality constraint on
  149. * two values.
  150. *
  151. * @param v1 A value expression.
  152. * @param v2 Another value expression.
  153. *
  154. * @return A "equal to" constraint on the arguments.
  155. */
  156. public static QueryExp eq(ValueExp v1, ValueExp v2) {
  157. return new BinaryRelQueryExp(EQ, v1, v2);
  158. }
  159. /**
  160. * Returns a query expression that represents the constraint that one
  161. * value is between two other values.
  162. *
  163. * @param v1 A value expression that is "between" v2 and v3.
  164. * @param v2 Value expression that represents a boundary of the constraint.
  165. * @param v3 Value expression that represents a boundary of the constraint.
  166. *
  167. * @return The constraint that v1 lies between v2 and v3.
  168. */
  169. public static QueryExp between(ValueExp v1, ValueExp v2, ValueExp v3) {
  170. return new BetweenQueryExp(v1, v2, v3);
  171. }
  172. /**
  173. * Returns a query expression that represents a matching constraint on
  174. * a string argument. The matching syntax is consistent with file globbing:
  175. * Supports "<code>?</code>", "<code>*</code>", "<code>[</code>",
  176. * each of which may be escaped with "<code>\</code>";
  177. * Character classes may use "<code>!</code>" for negation and
  178. * "<code>-</code>" for range.
  179. * (<code>*</code> for any character sequence,
  180. * <code>?</code> for a single arbitrary character,
  181. * <code>[...]</code> for a character sequence).
  182. * For example: <code>a*b?c</code> would match a string starting
  183. * with the character <code>a</code>, followed
  184. * by any number of characters, followed by a <code>b</code>,
  185. * any single character, and a <code>c</code>.
  186. *
  187. * @param a An attribute expression
  188. * @param s A string value expression representing a matching constraint
  189. *
  190. * @return A query expression that represents the matching constraint on the
  191. * string argument.
  192. */
  193. public static QueryExp match(AttributeValueExp a, StringValueExp s) {
  194. return new MatchQueryExp(a, s);
  195. }
  196. /**
  197. * <p>Returns a new attribute expression.</p>
  198. *
  199. * <p>Evaluating this expression for a given
  200. * <code>objectName</code> includes performing {@link
  201. * MBeanServer#getAttribute MBeanServer.getAttribute(objectName,
  202. * name)}.</p>
  203. *
  204. * @param name The name of the attribute.
  205. *
  206. * @return An attribute expression for the attribute named name.
  207. */
  208. public static AttributeValueExp attr(String name) {
  209. return new AttributeValueExp(name);
  210. }
  211. /**
  212. * <p>Returns a new qualified attribute expression.</p>
  213. *
  214. * <p>Evaluating this expression for a given
  215. * <code>objectName</code> includes performing {@link
  216. * MBeanServer#getObjectInstance
  217. * MBeanServer.getObjectInstance(objectName)} and {@link
  218. * MBeanServer#getAttribute MBeanServer.getAttribute(objectName,
  219. * name)}.</p>
  220. *
  221. * @param className The name of the class possessing the attribute.
  222. * @param name The name of the attribute.
  223. *
  224. * @return An attribute expression for the attribute named name.
  225. */
  226. public static AttributeValueExp attr(String className, String name) {
  227. return new QualifiedAttributeValueExp(className, name);
  228. }
  229. /**
  230. * <p>Returns a new class attribute expression which can be used in any
  231. * Query call that expects a ValueExp.</p>
  232. *
  233. * <p>Evaluating this expression for a given
  234. * <code>objectName</code> includes performing {@link
  235. * MBeanServer#getObjectInstance
  236. * MBeanServer.getObjectInstance(objectName)}.</p>
  237. *
  238. * @return A class attribute expression.
  239. */
  240. public static AttributeValueExp classattr() {
  241. return new ClassAttributeValueExp();
  242. }
  243. /**
  244. * Returns a constraint that is the negation of its argument.
  245. *
  246. * @param queryExp The constraint to negate.
  247. *
  248. * @return A negated constraint.
  249. */
  250. public static QueryExp not(QueryExp queryExp) {
  251. return new NotQueryExp(queryExp);
  252. }
  253. /**
  254. * Returns an expression constraining a value to be one of an explicit list.
  255. *
  256. * @param val A value to be constrained.
  257. * @param valueList An array of ValueExps.
  258. *
  259. * @return A QueryExp that represents the constraint.
  260. */
  261. public static QueryExp in(ValueExp val, ValueExp valueList[]) {
  262. return new InQueryExp(val, valueList);
  263. }
  264. /**
  265. * Returns a new string expression.
  266. *
  267. * @param val The string value.
  268. *
  269. * @return A ValueExp object containing the string argument.
  270. */
  271. public static StringValueExp value(String val) {
  272. return new StringValueExp(val);
  273. }
  274. /**
  275. * Returns a numeric value expression that can be used in any Query call
  276. * that expects a ValueExp.
  277. *
  278. * @param val An instance of Number.
  279. *
  280. * @return A ValueExp object containing the argument.
  281. */
  282. public static ValueExp value(Number val) {
  283. return new NumericValueExp(val);
  284. }
  285. /**
  286. * Returns a numeric value expression that can be used in any Query call
  287. * that expects a ValueExp.
  288. *
  289. * @param val An int value.
  290. *
  291. * @return A ValueExp object containing the argument.
  292. */
  293. public static ValueExp value(int val) {
  294. return new NumericValueExp(new Long(val));
  295. }
  296. /**
  297. * Returns a numeric value expression that can be used in any Query call
  298. * that expects a ValueExp.
  299. *
  300. * @param val A long value.
  301. *
  302. * @return A ValueExp object containing the argument.
  303. */
  304. public static ValueExp value(long val) {
  305. return new NumericValueExp(new Long(val));
  306. }
  307. /**
  308. * Returns a numeric value expression that can be used in any Query call
  309. * that expects a ValueExp.
  310. *
  311. * @param val A float value.
  312. *
  313. * @return A ValueExp object containing the argument.
  314. */
  315. public static ValueExp value(float val) {
  316. return new NumericValueExp(new Double(val));
  317. }
  318. /**
  319. * Returns a numeric value expression that can be used in any Query call
  320. * that expects a ValueExp.
  321. *
  322. * @param val A double value.
  323. *
  324. * @return A ValueExp object containing the argument.
  325. */
  326. public static ValueExp value(double val) {
  327. return new NumericValueExp(new Double(val));
  328. }
  329. /**
  330. * Returns a boolean value expression that can be used in any Query call
  331. * that expects a ValueExp.
  332. *
  333. * @param val A boolean value.
  334. *
  335. * @return A ValueExp object containing the argument.
  336. */
  337. public static ValueExp value(boolean val) {
  338. return new BooleanValueExp(val);
  339. }
  340. /**
  341. * Returns a binary expression representing the sum of two numeric values,
  342. * or the concatenation of two string values.
  343. *
  344. * @param value1 The first '+' operand.
  345. * @param value2 The second '+' operand.
  346. *
  347. * @return A ValueExp representing the sum or concatenation of the two
  348. * arguments.
  349. */
  350. public static ValueExp plus(ValueExp value1, ValueExp value2) {
  351. return new BinaryOpValueExp(PLUS, value1, value2);
  352. }
  353. /**
  354. * Returns a binary expression representing the product of two numeric values.
  355. *
  356. *
  357. * @param value1 The first '*' operand.
  358. * @param value2 The second '*' operand.
  359. *
  360. * @return A ValueExp representing the product.
  361. */
  362. public static ValueExp times(ValueExp value1,ValueExp value2) {
  363. return new BinaryOpValueExp(TIMES, value1, value2);
  364. }
  365. /**
  366. * Returns a binary expression representing the difference between two numeric
  367. * values.
  368. *
  369. * @param value1 The first '-' operand.
  370. * @param value2 The second '-' operand.
  371. *
  372. * @return A ValueExp representing the difference between two arguments.
  373. */
  374. public static ValueExp minus(ValueExp value1, ValueExp value2) {
  375. return new BinaryOpValueExp(MINUS, value1, value2);
  376. }
  377. /**
  378. * Returns a binary expression representing the quotient of two numeric
  379. * values.
  380. *
  381. * @param value1 The first '/' operand.
  382. * @param value2 The second '/' operand.
  383. *
  384. * @return A ValueExp representing the quotient of two arguments.
  385. */
  386. public static ValueExp div(ValueExp value1, ValueExp value2) {
  387. return new BinaryOpValueExp(DIV, value1, value2);
  388. }
  389. /**
  390. * Returns a query expression that represents a matching constraint on
  391. * a string argument. The value must start with the given string value.
  392. *
  393. * @param a An attribute expression.
  394. * @param s A string value expression representing the beginning of the string value.
  395. *
  396. * @return The constraint that a matches s.
  397. */
  398. public static QueryExp initialSubString(AttributeValueExp a, StringValueExp s) {
  399. return new MatchQueryExp(a, new StringValueExp(s.getValue()+"*"));
  400. }
  401. /**
  402. * Returns a query expression that represents a matching constraint on
  403. * a string argument. The value must contain the given string value.
  404. *
  405. * @param a An attribute expression.
  406. * @param s A string value expression representing the substring.
  407. *
  408. * @return The constraint that a matches s.
  409. */
  410. public static QueryExp anySubString(AttributeValueExp a, StringValueExp s) {
  411. return new MatchQueryExp(a, new StringValueExp("*"+s.getValue()+"*"));
  412. }
  413. /**
  414. * Returns a query expression that represents a matching constraint on
  415. * a string argument. The value must contain the given string value.
  416. *
  417. * @param a An attribute expression.
  418. * @param s A string value expression representing the end of the string value.
  419. *
  420. *@return The constraint that a matches s.
  421. */
  422. public static QueryExp finalSubString(AttributeValueExp a, StringValueExp s) {
  423. return new MatchQueryExp(a, new StringValueExp("*"+ s.getValue()));
  424. }
  425. }