KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > BetweenQueryExp


1 /*
2  * @(#)BetweenQueryExp.java 4.17 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
8 package javax.management;
9
10
11 /**
12  * This class is used by the query-building mechanism to represent binary
13  * relations.
14  * @serial include
15  *
16  * @since 1.5
17  */

18 class BetweenQueryExp extends QueryEval JavaDoc implements QueryExp JavaDoc {
19
20     /* Serial version */
21     private static final long serialVersionUID = -2933597532866307444L;
22     
23     /**
24      * @serial The checked value
25      */

26     private ValueExp JavaDoc exp1;
27
28     /**
29      * @serial The lower bound value
30      */

31     private ValueExp JavaDoc exp2;
32
33     /**
34      * @serial The upper bound value
35      */

36     private ValueExp JavaDoc exp3;
37     
38     
39     /**
40      * Basic Constructor.
41      */

42     public BetweenQueryExp() {
43     }
44     
45     /**
46      * Creates a new BetweenQueryExp with v1 checked value, v2 lower bound
47      * and v3 upper bound values.
48      */

49     public BetweenQueryExp(ValueExp JavaDoc v1, ValueExp JavaDoc v2, ValueExp JavaDoc v3) {
50     exp1 = v1;
51     exp2 = v2;
52     exp3 = v3;
53     }
54
55
56     /**
57      * Returns the checked value of the query.
58      */

59     public ValueExp JavaDoc getCheckedValue() {
60     return exp1;
61     }
62
63     /**
64      * Returns the lower bound value of the query.
65      */

66     public ValueExp JavaDoc getLowerBound() {
67     return exp2;
68     }
69
70     /**
71      * Returns the upper bound value of the query.
72      */

73     public ValueExp JavaDoc getUpperBound() {
74     return exp3;
75     }
76
77     /**
78      * Applies the BetweenQueryExp on an MBean.
79      *
80      * @param name The name of the MBean on which the BetweenQueryExp will be applied.
81      *
82      * @return True if the query was successfully applied to the MBean, false otherwise.
83      *
84      * @exception BadStringOperationException
85      * @exception BadBinaryOpValueExpException
86      * @exception BadAttributeValueExpException
87      * @exception InvalidApplicationException
88      */

89     public boolean apply(ObjectName JavaDoc name) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc,
90     BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc {
91     ValueExp JavaDoc val1 = exp1.apply(name);
92     ValueExp JavaDoc val2 = exp2.apply(name);
93     ValueExp JavaDoc val3 = exp3.apply(name);
94     String JavaDoc sval1;
95     String JavaDoc sval2;
96     String JavaDoc sval3;
97     double dval1;
98     double dval2;
99     double dval3;
100     long lval1;
101     long lval2;
102     long lval3;
103     boolean numeric = val1 instanceof NumericValueExp JavaDoc;
104     
105     if (numeric) {
106         if (((NumericValueExp JavaDoc)val1).isLong()) {
107         lval1 = ((NumericValueExp JavaDoc)val1).longValue();
108         lval2 = ((NumericValueExp JavaDoc)val2).longValue();
109         lval3 = ((NumericValueExp JavaDoc)val3).longValue();
110         return lval2 <= lval1 && lval1 <= lval3;
111         } else {
112         dval1 = ((NumericValueExp JavaDoc)val1).doubleValue();
113         dval2 = ((NumericValueExp JavaDoc)val2).doubleValue();
114         dval3 = ((NumericValueExp JavaDoc)val3).doubleValue();
115         return dval2 <= dval1 && dval1 <= dval3;
116         }
117         
118     } else {
119         sval1 = ((StringValueExp JavaDoc)val1).toString();
120         sval2 = ((StringValueExp JavaDoc)val2).toString();
121         sval3 = ((StringValueExp JavaDoc)val3).toString();
122         return sval2.compareTo(sval1) <= 0 && sval1.compareTo(sval3) <= 0;
123     }
124     }
125   
126     /**
127      * Returns the string representing the object.
128      */

129     public String JavaDoc toString() {
130     return "(" + exp1 + ") between (" + exp2 + ") and (" + exp3 + ")";
131     }
132
133  }
134
135
Popular Tags