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: * A Binary Comparison query.
026: *
027: * <p><b>Revisions:</b>
028: * <p><b>20020314 Adrian Brock:</b>
029: * <ul>
030: * <li>Added human readable string representation.
031: * </ul>
032: * <p><b>20020317 Adrian Brock:</b>
033: * <ul>
034: * <li>Make queries thread safe
035: * </ul>
036: *
037: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
038: * @version $Revision: 57200 $
039: */
040: class BinaryRelQueryExp extends QueryEval implements QueryExp {
041: // Constants ---------------------------------------------------
042:
043: private static final long serialVersionUID = -5690656271650491000L;
044:
045: // Attributes --------------------------------------------------
046:
047: /**
048: * The operation
049: */
050: private int relOp;
051:
052: /**
053: * The first expression
054: */
055: private ValueExp exp1;
056:
057: /**
058: * The second expression
059: */
060: private ValueExp exp2;
061:
062: // Constructors ------------------------------------------------
063:
064: public BinaryRelQueryExp() {
065: }
066:
067: /**
068: * Construct a binary comparison query
069: *
070: * @param operation the comparison as defined in Query
071: * @param first the first expression in the query
072: * @param second the second expression in the query
073: */
074: public BinaryRelQueryExp(int operation, ValueExp first,
075: ValueExp second) {
076: this .relOp = operation;
077: this .exp1 = first;
078: this .exp2 = second;
079: }
080:
081: // Public ------------------------------------------------------
082:
083: // Query Exp Implementation ------------------------------------
084:
085: public boolean apply(ObjectName name)
086: throws BadStringOperationException,
087: BadBinaryOpValueExpException,
088: BadAttributeValueExpException, InvalidApplicationException {
089: ValueExp testFirst = exp1.apply(name);
090: ValueExp testSecond = exp2.apply(name);
091:
092: if (testFirst instanceof NumericValueExp
093: && testSecond instanceof NumericValueExp) {
094: switch (relOp) {
095: case Query.GT:
096: return ((NumericValueExp) testFirst).getDoubleValue() > ((NumericValueExp) testSecond)
097: .getDoubleValue();
098: case Query.GE:
099: return ((NumericValueExp) testFirst).getDoubleValue() >= ((NumericValueExp) testSecond)
100: .getDoubleValue();
101: case Query.LT:
102: return ((NumericValueExp) testFirst).getDoubleValue() < ((NumericValueExp) testSecond)
103: .getDoubleValue();
104: case Query.LE:
105: return ((NumericValueExp) testFirst).getDoubleValue() <= ((NumericValueExp) testSecond)
106: .getDoubleValue();
107: case Query.EQ:
108: return ((NumericValueExp) testFirst).getDoubleValue() == ((NumericValueExp) testSecond)
109: .getDoubleValue();
110: default:
111: // fall through to the exception at the end of the method
112: break;
113: }
114: } else if (testFirst instanceof StringValueExp
115: && testSecond instanceof StringValueExp) {
116: switch (relOp) {
117: case Query.GT:
118: return testFirst.toString().compareTo(
119: testSecond.toString()) > 0;
120: case Query.GE:
121: return testFirst.toString().compareTo(
122: testSecond.toString()) >= 0;
123: case Query.LT:
124: return testFirst.toString().compareTo(
125: testSecond.toString()) < 0;
126: case Query.LE:
127: return testFirst.toString().compareTo(
128: testSecond.toString()) <= 0;
129: case Query.EQ:
130: return testFirst.toString().compareTo(
131: testSecond.toString()) == 0;
132: default:
133: throw new BadStringOperationException("TODO");
134: }
135: } else if (testFirst instanceof BooleanValueExp
136: && testSecond instanceof BooleanValueExp) {
137: switch (relOp) {
138: case Query.EQ:
139: return ((BooleanValueExp) testFirst).getValue() == ((BooleanValueExp) testSecond)
140: .getValue();
141: default:
142: // fall through to the exception at the end of the method
143: break;
144: }
145: }
146: // Review What happens now?
147: throw new BadBinaryOpValueExpException(testFirst);
148: }
149:
150: // Object overrides --------------------------------------------
151:
152: public String toString() {
153: StringBuffer buffer = new StringBuffer();
154: buffer.append("(");
155: buffer.append(exp1);
156: buffer.append(")");
157: switch (relOp) {
158: case Query.GT:
159: buffer.append(" > ");
160: break;
161: case Query.GE:
162: buffer.append(" >= ");
163: break;
164: case Query.LT:
165: buffer.append(" < ");
166: break;
167: case Query.LE:
168: buffer.append(" <= ");
169: break;
170: case Query.EQ:
171: buffer.append(" == ");
172: }
173: buffer.append("(");
174: buffer.append(exp2);
175: buffer.append(")");
176: return buffer.toString();
177: }
178:
179: }
|