001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management;
009:
010: import org.huihoo.jfox.jmx.queries.AndQueryExp;
011: import org.huihoo.jfox.jmx.queries.BetweenQueryExp;
012: import org.huihoo.jfox.jmx.queries.BooleanValueExp;
013: import org.huihoo.jfox.jmx.queries.ClassAttributeValueExp;
014: import org.huihoo.jfox.jmx.queries.InQueryExp;
015: import org.huihoo.jfox.jmx.queries.MatchQueryExp;
016: import org.huihoo.jfox.jmx.queries.NotQueryExp;
017: import org.huihoo.jfox.jmx.queries.NumericValueExp;
018: import org.huihoo.jfox.jmx.queries.OrQueryExp;
019: import org.huihoo.jfox.jmx.queries.QualifiedAttributeValueExp;
020: import org.huihoo.jfox.jmx.queries.MinusOpValueExp;
021: import org.huihoo.jfox.jmx.queries.PlusOpValueExp;
022: import org.huihoo.jfox.jmx.queries.TimesOpValueExp;
023: import org.huihoo.jfox.jmx.queries.DivOpValueExp;
024: import org.huihoo.jfox.jmx.queries.GtRelQueryExp;
025: import org.huihoo.jfox.jmx.queries.GeqRelQueryExp;
026: import org.huihoo.jfox.jmx.queries.LeqRelQueryExp;
027: import org.huihoo.jfox.jmx.queries.LtRelQueryExp;
028: import org.huihoo.jfox.jmx.queries.EqRelQueryExp;
029:
030: /**
031: * Constructs query object constraints. The static methods provided return query expressions that may be used
032: * in listing and enumerating MBeans. Individual constraint construction methods
033: * allow only appropriate types as arguments. Composition of calls can
034: * construct arbitrary nestings of constraints, as the following example
035: * illustrates:
036: * QueryExp exp = Query.and(Query.gt(Query.attr("age"),Query.value(5)),
037: * Query.match(Query.attr("name"),
038: * Query.value("Smith")));
039: *
040: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
041: */
042:
043: public class Query {
044:
045: public static final int GT = 0;
046: public static final int LT = 1;
047: public static final int GE = 2;
048: public static final int LE = 3;
049: public static final int EQ = 4;
050: public static final int PLUS = 0;
051: public static final int MINUS = 1;
052: public static final int TIMES = 2;
053: public static final int DIV = 3;
054:
055: private Query() {
056: }
057:
058: /**
059: * Returns a query expression that is the conjunction of two other query
060: * expressions.
061: *
062: * @param leftexp A query expression.
063: * @param rightexp Another query expression.
064: *
065: * @return The conjunction of the two arguments.
066: */
067: public static QueryExp and(QueryExp leftexp, QueryExp rightexp) {
068: return new AndQueryExp(leftexp, rightexp);
069: }
070:
071: /**
072: * Returns a query expression that is the disjunction of two other query
073: * expressions.
074: *
075: * @param leftexp A query expression.
076: * @param rightexp Another query expression.
077: *
078: * @return The disjunction of the two arguments.
079: */
080: public static QueryExp or(QueryExp leftexp, QueryExp rightexp) {
081: return new OrQueryExp(leftexp, rightexp);
082: }
083:
084: /**
085: * Returns a query expression that represents a "greater than" constraint on
086: * two values.
087: *
088: * @param leftvexp A value expression.
089: * @param rightvexp Another value expression.
090: *
091: * @return A "greater than" constraint on the arguments.
092: */
093: public static QueryExp gt(ValueExp leftvexp, ValueExp rightvexp) {
094: return new GtRelQueryExp(leftvexp, rightvexp);
095: }
096:
097: /**
098: * Returns a query expression that represents a "greater than or equal
099: * to" constraint on two values.
100: *
101: * @param leftvexp A value expression.
102: * @param rightvexp Another value expression.
103: *
104: * @return A "greater than or equal to" constraint on the arguments.
105: */
106: public static QueryExp geq(ValueExp leftvexp, ValueExp rightvexp) {
107: return new GeqRelQueryExp(leftvexp, rightvexp);
108: }
109:
110: /**
111: * Returns a query expression that represents a "less than or equal to"
112: * constraint on two values.
113: *
114: * @param leftvexp A value expression.
115: * @param rightvexp Another value expression.
116: *
117: * @return A "less than or equal to" constraint on the arguments.
118: */
119: public static QueryExp leq(ValueExp leftvexp, ValueExp rightvexp) {
120: return new LeqRelQueryExp(leftvexp, rightvexp);
121: }
122:
123: /**
124: * Returns a query expression that represents a "less than" constraint on
125: * two values.
126: *
127: * @param leftvexp A value expression.
128: * @param rightvexp Another value expression.
129: *
130: * @return A "less than" constraint on the arguments.
131: */
132: public static QueryExp lt(ValueExp leftvexp, ValueExp rightvexp) {
133: return new LtRelQueryExp(leftvexp, rightvexp);
134: }
135:
136: /**
137: * Returns a query expression that represents an equality constraint on
138: * two values.
139: *
140: * @param leftvexp A value expression.
141: * @param rightvexp Another value expression.
142: *
143: * @return A "equal to" constraint on the arguments.
144: */
145: public static QueryExp eq(ValueExp leftvexp, ValueExp rightvexp) {
146: return new EqRelQueryExp(leftvexp, rightvexp);
147: }
148:
149: /**
150: * Returns a query expression that represents the constraint that one
151: * value is between two other values.
152: *
153: * @param leftvexp A value expression that is "between" v2 and v3.
154: * @param checkvexp Value expression that represents a boundary of the constraint.
155: * @param rightvexp Value expression that represents a boundary of the constraint.
156: *
157: * @return The constraint that v1 lies between v2 and v3.
158: */
159: public static QueryExp between(ValueExp leftvexp,
160: ValueExp checkvexp, ValueExp rightvexp) {
161: return new BetweenQueryExp(leftvexp, checkvexp, rightvexp);
162: }
163:
164: /**
165: * Returns a query expression that represents a matching constraint on
166: * a string argument. The matching syntax is consistent with file globbing:
167: * Supports "?", "*", "[", each of which may be escaped with "\";
168: * Character classes may use "!" for negation and "-" for range.
169: * (* for any character sequence ? for a single arbitrary character
170: * [...] for a character sequence).
171: * For example: a*b?c would match a string starting with the character a, followed
172: * by any number of characters, followed by a b, any single character, and a c.
173: *
174: * @param attrvexp An attribute expression
175: * @param strvexp A string value expression representing a matching constraint
176: *
177: * @return A query expression that represents the matching constraint on the
178: * string argument.
179: */
180: public static QueryExp match(AttributeValueExp attrvexp,
181: StringValueExp strvexp) {
182: return new MatchQueryExp(attrvexp, strvexp);
183: }
184:
185: /**
186: * Returns a new attribute expression.
187: *
188: * @param name The name of the attribute.
189: *
190: * @return An attribute expression for the attribute named name.
191: */
192: public static AttributeValueExp attr(String name) {
193: return new AttributeValueExp(name);
194: }
195:
196: /**
197: * Returns a new qualified attribute expression.
198: *
199: * @param className The name of the class possessing the attribute.
200: * @param name The name of the attribute.
201: *
202: * @return An attribute expression for the attribute named name.
203: */
204: public static AttributeValueExp attr(String className, String name) {
205: return new QualifiedAttributeValueExp(className, name);
206: }
207:
208: /**
209: * Returns a new class attribute expression which can be used in any
210: * Query call that expects a ValueExp.
211: *
212: * @return A class attribute expression.
213: */
214: public static AttributeValueExp classattr() {
215: return new ClassAttributeValueExp();
216: }
217:
218: /**
219: * Returns a constraint that is the negation of its argument.
220: *
221: * @param queryexp The constraint to negate.
222: *
223: * @return A negated constraint.
224: */
225: public static QueryExp not(QueryExp queryexp) {
226: return new NotQueryExp(queryexp);
227: }
228:
229: /**
230: * Returns an expression constraining a value to be one of an explicit list.
231: *
232: * @param vexp A value to be constrained.
233: * @param vexps An array of ValueExps.
234: *
235: * @return A QueryExp that represents the constraint.
236: */
237: public static QueryExp in(ValueExp vexp, ValueExp[] vexps) {
238: return new InQueryExp(vexp, vexps);
239: }
240:
241: /**
242: * Returns a new string expression.
243: *
244: * @param value The string value.
245: *
246: * @return A ValueExp object containing the string argument.
247: */
248: public static StringValueExp value(String value) {
249: return new StringValueExp(value);
250: }
251:
252: /**
253: * Returns a numeric value expression that can be used in any Query call
254: * that expects a ValueExp.
255: *
256: * @param value An instance of Number.
257: *
258: * @return A ValueExp object containing the argument.
259: */
260: public static ValueExp value(Number value) {
261: return new NumericValueExp(value);
262: }
263:
264: /**
265: * Returns a numeric value expression that can be used in any Query call
266: * that expects a ValueExp.
267: *
268: * @param value An int value.
269: *
270: * @return A ValueExp object containing the argument.
271: */
272: public static ValueExp value(int value) {
273: return new NumericValueExp(new Long(value));
274: }
275:
276: /**
277: * Returns a numeric value expression that can be used in any Query call
278: * that expects a ValueExp.
279: *
280: * @param value A long value.
281: *
282: * @return A ValueExp object containing the argument.
283: */
284: public static ValueExp value(long value) {
285: return new NumericValueExp(new Long(value));
286: }
287:
288: /**
289: * Returns a numeric value expression that can be used in any Query call
290: * that expects a ValueExp.
291: *
292: * @param value A float value.
293: *
294: * @return A ValueExp object containing the argument.
295: */
296: public static ValueExp value(float value) {
297: return new NumericValueExp(new Double(value));
298: }
299:
300: /**
301: * Returns a numeric value expression that can be used in any Query call
302: * that expects a ValueExp.
303: *
304: * @param value A double value.
305: *
306: * @return A ValueExp object containing the argument.
307: */
308: public static ValueExp value(double value) {
309: return new NumericValueExp(new Double(value));
310: }
311:
312: /**
313: * Returns a boolean value expression that can be used in any Query call
314: * that expects a ValueExp.
315: *
316: * @param flag A boolean value.
317: *
318: * @return A ValueExp object containing the argument.
319: */
320: public static ValueExp value(boolean flag) {
321: return new BooleanValueExp(flag);
322: }
323:
324: /**
325: * Returns a binary expression representing the sum of two numeric values,
326: * or the concatenation of two string values.
327: *
328: * @param leftvexp The first '+' operand.
329: * @param rightvexp The second '+' operand.
330: *
331: * @return A ValueExp representing the sum or concatenation of the two
332: * arguments.
333: */
334: public static ValueExp plus(ValueExp leftvexp, ValueExp rightvexp) {
335: return new PlusOpValueExp(leftvexp, rightvexp);
336: }
337:
338: /**
339: * Returns a binary expression representing the product of two numeric values.
340: *
341: *
342: * @param leftvexp The first '*' operand.
343: * @param rightvexp The second '*' operand.
344: *
345: * @return A ValueExp representing the product.
346: */
347: public static ValueExp times(ValueExp leftvexp, ValueExp rightvexp) {
348: return new TimesOpValueExp(leftvexp, rightvexp);
349: }
350:
351: /**
352: * Returns a binary expression representing the difference between two numeric
353: * values.
354: *
355: * @param leftvexp The first '-' operand.
356: * @param rightvexp The second '-' operand.
357: *
358: * @return A ValueExp representing the difference between two arguments.
359: */
360: public static ValueExp minus(ValueExp leftvexp, ValueExp rightvexp) {
361: return new MinusOpValueExp(leftvexp, rightvexp);
362: }
363:
364: /**
365: * Returns a binary expression representing the quotient of two numeric
366: * values.
367: *
368: * @param leftvexp The first '/' operand.
369: * @param rightvexp The second '/' operand.
370: *
371: * @return A ValueExp representing the quotient of two arguments.
372: */
373: public static ValueExp div(ValueExp leftvexp, ValueExp rightvexp) {
374: return new DivOpValueExp(leftvexp, rightvexp);
375: }
376:
377: /**
378: * Returns a query expression that represents a matching constraint on
379: * a string argument. The value must doStart with the given string value.
380: *
381: * @param attrvexp An attribute expression.
382: * @param strvexp A string value expression representing the beginning of the string value.
383: *
384: * @return The constraint that a matches s.
385: */
386: public static QueryExp initialSubString(AttributeValueExp attrvexp,
387: StringValueExp strvexp) {
388: return new MatchQueryExp(attrvexp, new StringValueExp(strvexp
389: .getValue()
390: + "*"));
391: }
392:
393: /**
394: * Returns a query expression that represents a matching constraint on
395: * a string argument. The value must contain the given string value.
396: *
397: * @param attrvexp An attribute expression.
398: * @param strvexp A string value expression representing the substring.
399: *
400: * @return The constraint that a matches s.
401: */
402: public static QueryExp anySubString(AttributeValueExp attrvexp,
403: StringValueExp strvexp) {
404: return new MatchQueryExp(attrvexp, new StringValueExp("*"
405: + strvexp.getValue() + "*"));
406: }
407:
408: /**
409: * Returns a query expression that represents a matching constraint on
410: * a string argument. The value must contain the given string value.
411: *
412: * @param attrvexp An attribute expression.
413: * @param strvexp A string value expression representing the end of the string value.
414: *
415: *@return The constraint that a matches s.
416: */
417: public static QueryExp finalSubString(AttributeValueExp attrvexp,
418: StringValueExp strvexp) {
419: return new MatchQueryExp(attrvexp, new StringValueExp("*"
420: + strvexp.getValue()));
421: }
422: }
|