001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management;
023:
024: /**
025: * This class is a factory for constructing queries.<p>
026: *
027: * REVIEW: Full explanation. See the spec for now for what it's worth.
028: *
029: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
030: * @version $Revision: 57200 $
031: */
032: public class Query {
033: // Constants ---------------------------------------------------
034:
035: /**
036: * Divide expression
037: */
038: public static final int DIV = 3;
039:
040: /**
041: * Equals expression
042: */
043: public static final int EQ = 4;
044:
045: /**
046: * Greater than or equals expression
047: */
048: public static final int GE = 2;
049:
050: /**
051: * Greater than expression
052: */
053: public static final int GT = 0;
054:
055: /**
056: * Less than or equals expression
057: */
058: public static final int LE = 3;
059:
060: /**
061: * Less than expression
062: */
063: public static final int LT = 1;
064:
065: /**
066: * Minus expression
067: */
068: public static final int MINUS = 1;
069:
070: /**
071: * Plus expression
072: */
073: public static final int PLUS = 0;
074:
075: /**
076: * Times expression
077: */
078: public static final int TIMES = 2;
079:
080: // Attributes --------------------------------------------------
081:
082: // Static -----------------------------------------------------
083:
084: /**
085: * And Query expression. Return true only when both expressions are true.
086: *
087: * @param first the first expression
088: * @param second the second expression
089: * @return the expression
090: */
091: public static QueryExp and(QueryExp first, QueryExp second) {
092: return new AndQueryExp(first, second);
093: }
094:
095: /**
096: * Tests an attribute contains a string as a subset. Return true
097: * when this is the case, false otherwise.
098: *
099: * @param attr the attribute
100: * @param string the string
101: * @return the expression
102: */
103: public static QueryExp anySubString(AttributeValueExp attr,
104: StringValueExp string) {
105: return new MatchQueryExp(attr, "*" + string.getValue() + "*");
106: }
107:
108: /**
109: * An attribute expression
110: *
111: * @param value the name of the attribute
112: * @return the expression
113: */
114: public static AttributeValueExp attr(String value) {
115: return new AttributeValueExp(value);
116: }
117:
118: /**
119: * An attribute expression restricted to a specific class
120: *
121: * @param className the name of the class
122: * @param value the name of the attribute
123: * @return the expression
124: */
125: public static AttributeValueExp attr(String className, String value) {
126: return new QualifiedAttributeValueExp(className, value);
127: }
128:
129: /**
130: * Tests a value is between two other values. Returns true when this is
131: * case, false otherwise.
132: *
133: * @param test the value to test
134: * @param lower the lower bound
135: * @param higher the higer bound
136: * @return the expression
137: */
138: public static QueryExp between(ValueExp test, ValueExp lower,
139: ValueExp higher) {
140: return new BetweenQueryExp(test, lower, higher);
141: }
142:
143: /**
144: * What is this?
145: *
146: * @return the expression
147: */
148: public static AttributeValueExp classattr() {
149: return new ClassAttributeValueExp();
150: }
151:
152: /**
153: * An expression that divides the first expression by the second
154: *
155: * @param first the first expression
156: * @param second the second expression
157: * @return the expression
158: */
159: public static ValueExp div(ValueExp first, ValueExp second) {
160: return new BinaryOpValueExp(DIV, first, second);
161: }
162:
163: /**
164: * Equals Comparison.
165: *
166: * @param first the first expression
167: * @param second the second expression
168: * @return true when first equals second
169: */
170: public static QueryExp eq(ValueExp first, ValueExp second) {
171: return new BinaryRelQueryExp(EQ, first, second);
172: }
173:
174: /**
175: * Tests an attribute ends with a string as a subset. Return true
176: * when this is the case, false otherwise.
177: *
178: * @param attr the attribute
179: * @param string the string
180: * @return the expression
181: */
182: public static QueryExp finalSubString(AttributeValueExp attr,
183: StringValueExp string) {
184: return new MatchQueryExp(attr, "*" + string.getValue());
185: }
186:
187: /**
188: * Greater than or Equals Comparison.
189: *
190: * @param first the first expression
191: * @param second the second expression
192: * @return true when first >= second
193: */
194: public static QueryExp geq(ValueExp first, ValueExp second) {
195: return new BinaryRelQueryExp(GE, first, second);
196: }
197:
198: /**
199: * Greater than.
200: *
201: * @param first the first expression
202: * @param second the second expression
203: * @return true when first > second
204: */
205: public static QueryExp gt(ValueExp first, ValueExp second) {
206: return new BinaryRelQueryExp(GT, first, second);
207: }
208:
209: /**
210: * Tests a value is in one of the listed values. Returns true when this is
211: * case, false otherwise.
212: *
213: * @param test the value to test
214: * @param list an array of values
215: * @return the expression
216: */
217: public static QueryExp in(ValueExp test, ValueExp[] list) {
218: return new InQueryExp(test, list);
219: }
220:
221: /**
222: * Tests an attribute starts with a string as a subset. Return true
223: * when this is the case, false otherwise.
224: *
225: * @param attr the attribute
226: * @param string the string
227: * @return the expression
228: */
229: public static QueryExp initialSubString(AttributeValueExp attr,
230: StringValueExp string) {
231: return new MatchQueryExp(attr, string.getValue() + "*");
232: }
233:
234: /**
235: * Less than or equal.
236: *
237: * @param first the first expression
238: * @param second the second expression
239: * @return true when first <= second
240: */
241: public static QueryExp leq(ValueExp first, ValueExp second) {
242: return new BinaryRelQueryExp(LE, first, second);
243: }
244:
245: /**
246: * Less than.
247: *
248: * @param first the first expression
249: * @param second the second expression
250: * @return true when first < second
251: */
252: public static QueryExp lt(ValueExp first, ValueExp second) {
253: return new BinaryRelQueryExp(LT, first, second);
254: }
255:
256: /**
257: * Tests an attribute equals a string value. Return true
258: * when this is the case, false otherwise.
259: *
260: * @param attr the attribute
261: * @param string the string
262: * @return the expression
263: */
264: public static QueryExp match(AttributeValueExp attr,
265: StringValueExp string) {
266: return new MatchQueryExp(attr, string.getValue());
267: }
268:
269: /**
270: * An expression that subtracts the second expression from the first
271: *
272: * @param first the first expression
273: * @param second the second expression
274: * @return the expression
275: */
276: public static ValueExp minus(ValueExp first, ValueExp second) {
277: return new BinaryOpValueExp(MINUS, first, second);
278: }
279:
280: /**
281: * Not Query expression. Return true only when expression is false.
282: *
283: * @param expression the expression to negate
284: * @return the expression
285: */
286: public static QueryExp not(QueryExp expression) {
287: return new NotQueryExp(expression);
288: }
289:
290: /**
291: * Or Query expression. Return true when either expression is true.
292: *
293: * @param first the first expression
294: * @param second the second expression
295: * @return the expression
296: */
297: public static QueryExp or(QueryExp first, QueryExp second) {
298: return new OrQueryExp(first, second);
299: }
300:
301: /**
302: * An expression that adds the second expression to the first
303: *
304: * @param first the first expression
305: * @param second the second expression
306: * @return the expression
307: */
308: public static ValueExp plus(ValueExp first, ValueExp second) {
309: return new BinaryOpValueExp(PLUS, first, second);
310: }
311:
312: /**
313: * An expression that multiplies the first expression by the second
314: *
315: * @param first the first expression
316: * @param second the second expression
317: * @return the expression
318: */
319: public static ValueExp times(ValueExp first, ValueExp second) {
320: return new BinaryOpValueExp(TIMES, first, second);
321: }
322:
323: /**
324: * Create a boolean value expression for use in a Query.
325: *
326: * @return the expression
327: */
328: public static ValueExp value(boolean value) {
329: return new BooleanValueExp(new Boolean(value));
330: }
331:
332: /**
333: * Create a double value expression for use in a Query.
334: *
335: * @return the expression
336: */
337: public static ValueExp value(double value) {
338: return new NumericValueExp(new Double(value));
339: }
340:
341: /**
342: * Create a float value expression for use in a Query.
343: *
344: * @return the expression
345: */
346: public static ValueExp value(float value) {
347: return new NumericValueExp(new Double(value));
348: }
349:
350: /**
351: * Create an integer value expression for use in a Query.
352: *
353: * @return the expression
354: */
355: public static ValueExp value(int value) {
356: return new NumericValueExp(new Long(value));
357: }
358:
359: /**
360: * Create a long value expression for use in a Query.
361: *
362: * @return the expression
363: */
364: public static ValueExp value(long value) {
365: return new NumericValueExp(new Long(value));
366: }
367:
368: /**
369: * Create a number value expression for use in a Query.
370: *
371: * @return the expression
372: */
373: public static ValueExp value(Number value) {
374: return new NumericValueExp(value);
375: }
376:
377: /**
378: * Create a string value expression for use in a Query.
379: *
380: * @return the expression
381: */
382: public static StringValueExp value(String value) {
383: return new StringValueExp(value);
384: }
385:
386: // Constructors ------------------------------------------------
387:
388: /**
389: * Construct a new Query
390: */
391: public Query() {
392: }
393:
394: // Public ------------------------------------------------------
395:
396: // X Implementation --------------------------------------------
397:
398: // Y overrides -------------------------------------------------
399:
400: // Protected ---------------------------------------------------
401:
402: // Package Private ---------------------------------------------
403:
404: // Private -----------------------------------------------------
405:
406: // Inner Classes -----------------------------------------------
407: }
|