KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > BinaryRelQueryExp


1 /*
2  * @(#)BinaryRelQueryExp.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  * operations.
14  * @serial include
15  *
16  * @since 1.5
17  */

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

26     private int relOp;
27
28     /**
29      * @serial The first value
30      */

31     private ValueExp JavaDoc exp1;
32
33     /**
34      * @serial The second value
35      */

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

42     public BinaryRelQueryExp() {
43     }
44
45     /**
46      * Creates a new BinaryRelQueryExp with operator op applied on v1 and
47      * v2 values.
48      */

49     public BinaryRelQueryExp(int op, ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
50     relOp = op;
51     exp1 = v1;
52     exp2 = v2;
53     }
54
55
56     /**
57      * Returns the operator of the query.
58      */

59     public int getOperator() {
60     return relOp;
61     }
62
63     /**
64      * Returns the left value of the query.
65      */

66     public ValueExp JavaDoc getLeftValue() {
67     return exp1;
68     }
69     
70     /**
71      * Returns the right value of the query.
72      */

73     public ValueExp JavaDoc getRightValue() {
74     return exp2;
75     }
76     
77     /**
78      * Applies the BinaryRelQueryExp on an MBean.
79      *
80      * @param name The name of the MBean on which the BinaryRelQueryExp 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     Object JavaDoc val1 = exp1.apply(name);
92     Object JavaDoc val2 = exp2.apply(name);
93     String JavaDoc sval1;
94     String JavaDoc sval2;
95     double dval1;
96     double dval2;
97     long lval1;
98     long lval2;
99     boolean bval1;
100     boolean bval2;
101     boolean numeric = val1 instanceof NumericValueExp JavaDoc;
102     boolean bool = val1 instanceof BooleanValueExp JavaDoc;
103     if (numeric) {
104         if (((NumericValueExp JavaDoc)val1).isLong()) {
105         lval1 = ((NumericValueExp JavaDoc)val1).longValue();
106         lval2 = ((NumericValueExp JavaDoc)val2).longValue();
107
108         switch (relOp) {
109         case Query.GT:
110             return lval1 > lval2;
111         case Query.LT:
112             return lval1 < lval2;
113         case Query.GE:
114             return lval1 >= lval2;
115         case Query.LE:
116             return lval1 <= lval2;
117         case Query.EQ:
118             return lval1 == lval2;
119         }
120         } else {
121         dval1 = ((NumericValueExp JavaDoc)val1).doubleValue();
122         dval2 = ((NumericValueExp JavaDoc)val2).doubleValue();
123
124         switch (relOp) {
125         case Query.GT:
126             return dval1 > dval2;
127         case Query.LT:
128             return dval1 < dval2;
129         case Query.GE:
130             return dval1 >= dval2;
131         case Query.LE:
132             return dval1 <= dval2;
133         case Query.EQ:
134             return dval1 == dval2;
135         }
136         }
137         
138     } else if (bool) {
139
140         bval1 = ((BooleanValueExp JavaDoc)val1).getValue().booleanValue();
141         bval2 = ((BooleanValueExp JavaDoc)val2).getValue().booleanValue();
142
143         switch (relOp) {
144         case Query.GT:
145             return bval1 && !bval2;
146         case Query.LT:
147             return !bval1 && bval2;
148         case Query.GE:
149             return bval1 || !bval2;
150         case Query.LE:
151             return !bval1 || bval2;
152         case Query.EQ:
153             return bval1 == bval2;
154         }
155
156     } else {
157         sval1 = ((StringValueExp JavaDoc)val1).getValue();
158         sval2 = ((StringValueExp JavaDoc)val2).getValue();
159
160         switch (relOp) {
161         case Query.GT:
162         return sval1.compareTo(sval2) > 0;
163         case Query.LT:
164         return sval1.compareTo(sval2) < 0;
165         case Query.GE:
166         return sval1.compareTo(sval2) >= 0;
167         case Query.LE:
168         return sval1.compareTo(sval2) <= 0;
169         case Query.EQ:
170         return sval1.compareTo(sval2) == 0;
171         }
172     }
173
174     return false;
175     }
176     
177     /**
178      * Returns the string representing the object.
179      */

180     public String JavaDoc toString() {
181     return "(" + exp1 + ") " + relOpString() + " (" + exp2 + ")";
182     }
183
184     private String JavaDoc relOpString() {
185     switch (relOp) {
186     case Query.GT:
187         return ">";
188     case Query.LT:
189         return "<";
190     case Query.GE:
191         return ">=";
192     case Query.LE:
193         return "<=";
194     case Query.EQ:
195         return "=";
196     }
197     
198     return "=";
199     }
200
201  }
202
Popular Tags