KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > Query


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
8 package javax.management;
9
10
11 /**
12  * <p>Constructs query object constraints. The static methods provided
13  * return query expressions that may be used in listing and
14  * enumerating MBeans. Individual constraint construction methods
15  * allow only appropriate types as arguments. Composition of calls can
16  * construct arbitrary nestings of constraints, as the following
17  * example illustrates:</p>
18  *
19  * <pre>
20  * QueryExp exp = Query.and(Query.gt(Query.attr("age"),Query.value(5)),
21  * Query.match(Query.attr("name"),
22  * Query.value("Smith")));
23  * </pre>
24  *
25  * @since 1.5
26  */

27  public class Query extends Object JavaDoc {
28      
29      
30      /**
31       * A code representing the {@link Query#gt} query. This is chiefly
32       * of interest for the serialized form of queries.
33       */

34      public static final int GT = 0;
35
36      /**
37       * A code representing the {@link Query#lt} query. This is chiefly
38       * of interest for the serialized form of queries.
39       */

40      public static final int LT = 1;
41
42      /**
43       * A code representing the {@link Query#geq} query. This is chiefly
44       * of interest for the serialized form of queries.
45       */

46      public static final int GE = 2;
47
48      /**
49       * A code representing the {@link Query#leq} query. This is chiefly
50       * of interest for the serialized form of queries.
51       */

52      public static final int LE = 3;
53
54      /**
55       * A code representing the {@link Query#eq} query. This is chiefly
56       * of interest for the serialized form of queries.
57       */

58      public static final int EQ = 4;
59      
60
61      /**
62       * A code representing the {@link Query#plus} expression. This
63       * is chiefly of interest for the serialized form of queries.
64       */

65      public static final int PLUS = 0;
66
67      /**
68       * A code representing the {@link Query#minus} expression. This
69       * is chiefly of interest for the serialized form of queries.
70       */

71      public static final int MINUS = 1;
72
73      /**
74       * A code representing the {@link Query#times} expression. This
75       * is chiefly of interest for the serialized form of queries.
76       */

77      public static final int TIMES = 2;
78
79      /**
80       * A code representing the {@link Query#div} expression. This is
81       * chiefly of interest for the serialized form of queries.
82       */

83      public static final int DIV = 3;
84      
85
86      /**
87       * Basic constructor.
88       */

89      public Query() {
90      }
91
92
93      /**
94       * Returns a query expression that is the conjunction of two other query
95       * expressions.
96       *
97       * @param q1 A query expression.
98       * @param q2 Another query expression.
99       *
100       * @return The conjunction of the two arguments.
101       */

102      public static QueryExp JavaDoc and(QueryExp JavaDoc q1, QueryExp JavaDoc q2) {
103      return new AndQueryExp JavaDoc(q1, q2);
104      }
105      
106      /**
107       * Returns a query expression that is the disjunction of two other query
108       * expressions.
109       *
110       * @param q1 A query expression.
111       * @param q2 Another query expression.
112       *
113       * @return The disjunction of the two arguments.
114       */

115      public static QueryExp JavaDoc or(QueryExp JavaDoc q1, QueryExp JavaDoc q2) {
116      return new OrQueryExp JavaDoc(q1, q2);
117      }
118      
119      /**
120       * Returns a query expression that represents a "greater than" constraint on
121       * two values.
122       *
123       * @param v1 A value expression.
124       * @param v2 Another value expression.
125       *
126       * @return A "greater than" constraint on the arguments.
127       */

128      public static QueryExp JavaDoc gt(ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
129      return new BinaryRelQueryExp JavaDoc(GT, v1, v2);
130      }
131
132      /**
133       * Returns a query expression that represents a "greater than or equal
134       * to" constraint on two values.
135       *
136       * @param v1 A value expression.
137       * @param v2 Another value expression.
138       *
139       * @return A "greater than or equal to" constraint on the arguments.
140       */

141      public static QueryExp JavaDoc geq(ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
142      return new BinaryRelQueryExp JavaDoc(GE, v1, v2);
143      }
144
145      /**
146       * Returns a query expression that represents a "less than or equal to"
147       * constraint on two values.
148       *
149       * @param v1 A value expression.
150       * @param v2 Another value expression.
151       *
152       * @return A "less than or equal to" constraint on the arguments.
153       */

154      public static QueryExp JavaDoc leq(ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
155      return new BinaryRelQueryExp JavaDoc(LE, v1, v2);
156      }
157
158      /**
159       * Returns a query expression that represents a "less than" constraint on
160       * two values.
161       *
162       * @param v1 A value expression.
163       * @param v2 Another value expression.
164       *
165       * @return A "less than" constraint on the arguments.
166       */

167      public static QueryExp JavaDoc lt(ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
168      return new BinaryRelQueryExp JavaDoc(LT, v1, v2);
169      }
170
171      /**
172       * Returns a query expression that represents an equality constraint on
173       * two values.
174       *
175       * @param v1 A value expression.
176       * @param v2 Another value expression.
177       *
178       * @return A "equal to" constraint on the arguments.
179       */

180      public static QueryExp JavaDoc eq(ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
181      return new BinaryRelQueryExp JavaDoc(EQ, v1, v2);
182      }
183
184      /**
185       * Returns a query expression that represents the constraint that one
186       * value is between two other values.
187       *
188       * @param v1 A value expression that is "between" v2 and v3.
189       * @param v2 Value expression that represents a boundary of the constraint.
190       * @param v3 Value expression that represents a boundary of the constraint.
191       *
192       * @return The constraint that v1 lies between v2 and v3.
193       */

194      public static QueryExp JavaDoc between(ValueExp JavaDoc v1, ValueExp JavaDoc v2, ValueExp JavaDoc v3) {
195      return new BetweenQueryExp JavaDoc(v1, v2, v3);
196      }
197
198      /**
199       * Returns a query expression that represents a matching constraint on
200       * a string argument. The matching syntax is consistent with file globbing:
201       * Supports "<code>?</code>", "<code>*</code>", "<code>[</code>",
202       * each of which may be escaped with "<code>\</code>";
203       * Character classes may use "<code>!</code>" for negation and
204       * "<code>-</code>" for range.
205       * (<code>*</code> for any character sequence,
206       * <code>?</code> for a single arbitrary character,
207       * <code>[...]</code> for a character sequence).
208       * For example: <code>a*b?c</code> would match a string starting
209       * with the character <code>a</code>, followed
210       * by any number of characters, followed by a <code>b</code>,
211       * any single character, and a <code>c</code>.
212       *
213       * @param a An attribute expression
214       * @param s A string value expression representing a matching constraint
215       *
216       * @return A query expression that represents the matching constraint on the
217       * string argument.
218       */

219      public static QueryExp JavaDoc match(AttributeValueExp JavaDoc a, StringValueExp JavaDoc s) {
220      return new MatchQueryExp JavaDoc(a, s);
221      }
222
223      /**
224       * <p>Returns a new attribute expression.</p>
225       *
226       * <p>Evaluating this expression for a given
227       * <code>objectName</code> includes performing {@link
228       * MBeanServer#getAttribute MBeanServer.getAttribute(objectName,
229       * name)}.</p>
230       *
231       * @param name The name of the attribute.
232       *
233       * @return An attribute expression for the attribute named name.
234       */

235      public static AttributeValueExp JavaDoc attr(String JavaDoc name) {
236      return new AttributeValueExp JavaDoc(name);
237      }
238
239      /**
240       * <p>Returns a new qualified attribute expression.</p>
241       *
242       * <p>Evaluating this expression for a given
243       * <code>objectName</code> includes performing {@link
244       * MBeanServer#getObjectInstance
245       * MBeanServer.getObjectInstance(objectName)} and {@link
246       * MBeanServer#getAttribute MBeanServer.getAttribute(objectName,
247       * name)}.</p>
248       *
249       * @param className The name of the class possessing the attribute.
250       * @param name The name of the attribute.
251       *
252       * @return An attribute expression for the attribute named name.
253       */

254      public static AttributeValueExp JavaDoc attr(String JavaDoc className, String JavaDoc name) {
255      return new QualifiedAttributeValueExp JavaDoc(className, name);
256      }
257      
258      /**
259       * <p>Returns a new class attribute expression which can be used in any
260       * Query call that expects a ValueExp.</p>
261       *
262       * <p>Evaluating this expression for a given
263       * <code>objectName</code> includes performing {@link
264       * MBeanServer#getObjectInstance
265       * MBeanServer.getObjectInstance(objectName)}.</p>
266       *
267       * @return A class attribute expression.
268       */

269      public static AttributeValueExp JavaDoc classattr() {
270      return new ClassAttributeValueExp JavaDoc();
271      }
272      
273      /**
274       * Returns a constraint that is the negation of its argument.
275       *
276       * @param queryExp The constraint to negate.
277       *
278       * @return A negated constraint.
279       */

280      public static QueryExp JavaDoc not(QueryExp JavaDoc queryExp) {
281      return new NotQueryExp JavaDoc(queryExp);
282      }
283     
284      /**
285       * Returns an expression constraining a value to be one of an explicit list.
286       *
287       * @param val A value to be constrained.
288       * @param valueList An array of ValueExps.
289       *
290       * @return A QueryExp that represents the constraint.
291       */

292      public static QueryExp JavaDoc in(ValueExp JavaDoc val, ValueExp JavaDoc valueList[]) {
293      return new InQueryExp JavaDoc(val, valueList);
294      }
295      
296      /**
297       * Returns a new string expression.
298       *
299       * @param val The string value.
300       *
301       * @return A ValueExp object containing the string argument.
302       */

303      public static StringValueExp JavaDoc value(String JavaDoc val) {
304      return new StringValueExp JavaDoc(val);
305      }
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 An instance of Number.
312       *
313       * @return A ValueExp object containing the argument.
314       */

315      public static ValueExp JavaDoc value(Number JavaDoc val) {
316      return new NumericValueExp JavaDoc(val);
317      }
318
319      /**
320       * Returns a numeric value expression that can be used in any Query call
321       * that expects a ValueExp.
322       *
323       * @param val An int value.
324       *
325       * @return A ValueExp object containing the argument.
326       */

327      public static ValueExp JavaDoc value(int val) {
328      return new NumericValueExp JavaDoc(new Long JavaDoc(val));
329      }
330
331      /**
332       * Returns a numeric value expression that can be used in any Query call
333       * that expects a ValueExp.
334       *
335       * @param val A long value.
336       *
337       * @return A ValueExp object containing the argument.
338       */

339      public static ValueExp JavaDoc value(long val) {
340      return new NumericValueExp JavaDoc(new Long JavaDoc(val));
341      }
342      
343      /**
344       * Returns a numeric value expression that can be used in any Query call
345       * that expects a ValueExp.
346       *
347       * @param val A float value.
348       *
349       * @return A ValueExp object containing the argument.
350       */

351      public static ValueExp JavaDoc value(float val) {
352      return new NumericValueExp JavaDoc(new Double JavaDoc(val));
353      }
354      
355      /**
356       * Returns a numeric value expression that can be used in any Query call
357       * that expects a ValueExp.
358       *
359       * @param val A double value.
360       *
361       * @return A ValueExp object containing the argument.
362       */

363      public static ValueExp JavaDoc value(double val) {
364      return new NumericValueExp JavaDoc(new Double JavaDoc(val));
365      }
366
367      /**
368       * Returns a boolean value expression that can be used in any Query call
369       * that expects a ValueExp.
370       *
371       * @param val A boolean value.
372       *
373       * @return A ValueExp object containing the argument.
374       */

375      public static ValueExp JavaDoc value(boolean val) {
376      return new BooleanValueExp JavaDoc(val);
377      }
378
379      /**
380       * Returns a binary expression representing the sum of two numeric values,
381       * or the concatenation of two string values.
382       *
383       * @param value1 The first '+' operand.
384       * @param value2 The second '+' operand.
385       *
386       * @return A ValueExp representing the sum or concatenation of the two
387       * arguments.
388       */

389      public static ValueExp JavaDoc plus(ValueExp JavaDoc value1, ValueExp JavaDoc value2) {
390      return new BinaryOpValueExp JavaDoc(PLUS, value1, value2);
391      }
392      
393      /**
394       * Returns a binary expression representing the product of two numeric values.
395       *
396       *
397       * @param value1 The first '*' operand.
398       * @param value2 The second '*' operand.
399       *
400       * @return A ValueExp representing the product.
401       */

402      public static ValueExp JavaDoc times(ValueExp JavaDoc value1,ValueExp JavaDoc value2) {
403      return new BinaryOpValueExp JavaDoc(TIMES, value1, value2);
404      }
405      
406      /**
407       * Returns a binary expression representing the difference between two numeric
408       * values.
409       *
410       * @param value1 The first '-' operand.
411       * @param value2 The second '-' operand.
412       *
413       * @return A ValueExp representing the difference between two arguments.
414       */

415      public static ValueExp JavaDoc minus(ValueExp JavaDoc value1, ValueExp JavaDoc value2) {
416      return new BinaryOpValueExp JavaDoc(MINUS, value1, value2);
417      }
418      
419      /**
420       * Returns a binary expression representing the quotient of two numeric
421       * values.
422       *
423       * @param value1 The first '/' operand.
424       * @param value2 The second '/' operand.
425       *
426       * @return A ValueExp representing the quotient of two arguments.
427       */

428      public static ValueExp JavaDoc div(ValueExp JavaDoc value1, ValueExp JavaDoc value2) {
429      return new BinaryOpValueExp JavaDoc(DIV, value1, value2);
430      }
431      
432      /**
433       * Returns a query expression that represents a matching constraint on
434       * a string argument. The value must start with the given string value.
435       *
436       * @param a An attribute expression.
437       * @param s A string value expression representing the beginning of the string value.
438       *
439       * @return The constraint that a matches s.
440       */

441      public static QueryExp JavaDoc initialSubString(AttributeValueExp JavaDoc a, StringValueExp JavaDoc s) {
442      return new MatchQueryExp JavaDoc(a, new StringValueExp JavaDoc(s.getValue()+"*"));
443      }
444
445      /**
446       * Returns a query expression that represents a matching constraint on
447       * a string argument. The value must contain the given string value.
448       *
449       * @param a An attribute expression.
450       * @param s A string value expression representing the substring.
451       *
452       * @return The constraint that a matches s.
453       */

454      public static QueryExp JavaDoc anySubString(AttributeValueExp JavaDoc a, StringValueExp JavaDoc s) {
455      return new MatchQueryExp JavaDoc(a, new StringValueExp JavaDoc("*"+s.getValue()+"*"));
456      }
457
458      /**
459       * Returns a query expression that represents a matching constraint on
460       * a string argument. The value must contain the given string value.
461       *
462       * @param a An attribute expression.
463       * @param s A string value expression representing the end of the string value.
464       *
465       *@return The constraint that a matches s.
466       */

467      public static QueryExp JavaDoc finalSubString(AttributeValueExp JavaDoc a, StringValueExp JavaDoc s) {
468      return new MatchQueryExp JavaDoc(a, new StringValueExp JavaDoc("*"+ s.getValue()));
469      }
470  }
471
Popular Tags