1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package javax.management;
8
9 /**
10 * A Binary Comparison query.
11 *
12 * <p><b>Revisions:</b>
13 * <p><b>20020314 Adrian Brock:</b>
14 * <ul>
15 * <li>Added human readable string representation.
16 * </ul>
17 * <p><b>20020317 Adrian Brock:</b>
18 * <ul>
19 * <li>Make queries thread safe
20 * </ul>
21 *
22 * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
23 * @version $Revision: 1.3 $
24 */
25 /*package*/ class BinaryComparisonQueryExp
26 extends QueryExpSupport
27 {
28 // Constants ---------------------------------------------------
29
30 // Attributes --------------------------------------------------
31
32 /**
33 * The operation
34 */
35 int operation;
36
37 /**
38 * The first expression
39 */
40 ValueExp first;
41
42 /**
43 * The second expression
44 */
45 ValueExp second;
46
47 // Static -----------------------------------------------------
48
49 // Constructors ------------------------------------------------
50
51 /**
52 * Construct a binary comparison query
53 *
54 * @param operation the comparison as defined in Query
55 * @param first the first expression in the query
56 * @param second the second expression in the query
57 */
58 public BinaryComparisonQueryExp(int operation, ValueExp first, ValueExp second)
59 {
60 this.operation = operation;
61 this.first = first;
62 this.second = second;
63 }
64
65 // Public ------------------------------------------------------
66
67 // Query Exp Implementation ------------------------------------
68
69 public boolean apply(ObjectName name)
70 throws BadStringOperationException,
71 BadBinaryOpValueExpException,
72 BadAttributeValueExpException,
73 InvalidApplicationException
74 {
75 ValueExp testFirst = first.apply(name);
76 ValueExp testSecond = second.apply(name);
77
78 if (testFirst instanceof NumberValueExp)
79 {
80 switch (operation)
81 {
82 case Query.GT:
83 return ((NumberValueExp)testFirst).getDoubleValue() >
84 ((NumberValueExp)testSecond).getDoubleValue();
85 case Query.GE:
86 return ((NumberValueExp)testFirst).getDoubleValue() >=
87 ((NumberValueExp)testSecond).getDoubleValue();
88 case Query.LT:
89 return ((NumberValueExp)testFirst).getDoubleValue() <
90 ((NumberValueExp)testSecond).getDoubleValue();
91 case Query.LE:
92 return ((NumberValueExp)testFirst).getDoubleValue() <=
93 ((NumberValueExp)testSecond).getDoubleValue();
94 case Query.EQ:
95 return ((NumberValueExp)testFirst).getDoubleValue() ==
96 ((NumberValueExp)testSecond).getDoubleValue();
97 }
98 }
99 else if (testFirst instanceof StringValueExp)
100 {
101 switch (operation)
102 {
103 case Query.GT:
104 return ((StringValueExp)testFirst).toString().compareTo(
105 ((StringValueExp)testSecond).toString()) > 0;
106 case Query.GE:
107 return ((StringValueExp)testFirst).toString().compareTo(
108 ((StringValueExp)testSecond).toString()) >= 0;
109 case Query.LT:
110 return ((StringValueExp)testFirst).toString().compareTo(
111 ((StringValueExp)testSecond).toString()) < 0;
112 case Query.LE:
113 return ((StringValueExp)testFirst).toString().compareTo(
114 ((StringValueExp)testSecond).toString()) <= 0;
115 case Query.EQ:
116 return ((StringValueExp)testFirst).toString().compareTo(
117 ((StringValueExp)testSecond).toString()) == 0;
118 }
119 throw new BadStringOperationException("TODO");
120 }
121 else if (testFirst instanceof SingleValueExpSupport)
122 {
123 switch (operation)
124 {
125 case Query.EQ:
126 return ((SingleValueExpSupport)testFirst).getValue().equals(
127 ((SingleValueExpSupport)testSecond).getValue());
128 }
129 }
130 // Review What happens now?
131 throw new BadBinaryOpValueExpException(testFirst);
132 }
133
134 // Object overrides --------------------------------------------
135
136 public String toString()
137 {
138 StringBuffer buffer = new StringBuffer();
139 buffer.append("(");
140 buffer.append(first);
141 buffer.append(")");
142 switch (operation)
143 {
144 case Query.GT:
145 buffer.append(" > "); break;
146 case Query.GE:
147 buffer.append(" >= "); break;
148 case Query.LT:
149 buffer.append(" < "); break;
150 case Query.LE:
151 buffer.append(" <= "); break;
152 case Query.EQ:
153 buffer.append(" == ");
154 }
155 buffer.append("(");
156 buffer.append(second);
157 buffer.append(")");
158 return buffer.toString();
159 }
160
161 // Protected ---------------------------------------------------
162
163 // Package Private ---------------------------------------------
164
165 // Private -----------------------------------------------------
166
167 // Inner Classes -----------------------------------------------
168 }