001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management;
010:
011: /**
012: * @version $Revision: 1.7 $
013: * @serial include
014: */
015: class BinaryRelQueryExp extends QueryEval implements QueryExp {
016: private static final long serialVersionUID = -5690656271650491000L;
017:
018: /**
019: * @serial The left side expression
020: */
021: private final ValueExp exp1;
022: /**
023: * @serial The infix operator
024: */
025: private final int relOp;
026: /**
027: * @serial The right side expression
028: */
029: private final ValueExp exp2;
030:
031: BinaryRelQueryExp(int operation, ValueExp exp1, ValueExp exp2) {
032: this .relOp = operation;
033: this .exp1 = exp1;
034: this .exp2 = exp2;
035: }
036:
037: public void setMBeanServer(MBeanServer server) {
038: super .setMBeanServer(server);
039: if (exp1 != null)
040: exp1.setMBeanServer(server);
041: if (exp2 != null)
042: exp2.setMBeanServer(server);
043: }
044:
045: public boolean apply(ObjectName name)
046: throws BadStringOperationException,
047: BadBinaryOpValueExpException,
048: BadAttributeValueExpException, InvalidApplicationException {
049: if (exp1 == null
050: && exp2 == null
051: && (relOp == Query.EQ || relOp == Query.GE || relOp == Query.LE)) {
052: return true;
053: }
054:
055: if (exp1 != null && exp2 != null) {
056: ValueExp val1 = exp1.apply(name);
057: ValueExp val2 = exp2.apply(name);
058:
059: if (val1 instanceof NumericValueExp
060: && val2 instanceof NumericValueExp) {
061: NumericValueExp num1 = (NumericValueExp) val1;
062: NumericValueExp num2 = (NumericValueExp) val2;
063:
064: if (num1.isDouble() || num2.isDouble()) {
065: return compare(new Double(num1.doubleValue()),
066: new Double(num2.doubleValue()));
067: } else {
068: return compare(new Long(num1.longValue()),
069: new Long(num2.longValue()));
070: }
071: } else if (val1 instanceof BooleanValueExp
072: && val2 instanceof BooleanValueExp) {
073: boolean b1 = ((BooleanValueExp) val1).booleanValue();
074: boolean b2 = ((BooleanValueExp) val2).booleanValue();
075: return compare(new Long(b1 ? 1 : 0), new Long(b2 ? 1
076: : 0));
077: } else if (val1 instanceof StringValueExp
078: && val2 instanceof StringValueExp) {
079: String s1 = ((StringValueExp) val1).getValue();
080: String s2 = ((StringValueExp) val2).getValue();
081: return compare(s1, s2);
082: }
083: }
084:
085: return false;
086: }
087:
088: private boolean compare(Comparable c1, Comparable c2) {
089: switch (relOp) {
090: case Query.EQ:
091: if (c1 == null && c2 == null)
092: return true;
093: if (c1 == null || c2 == null)
094: return false;
095: return c1.equals(c2);
096: case Query.GE:
097: if (c1 == null && c2 == null)
098: return true;
099: if (c1 == null && c2 != null)
100: return false;
101: if (c1 != null && c2 == null)
102: return true;
103: return c1.compareTo(c2) >= 0;
104: case Query.LE:
105: if (c1 == null && c2 == null)
106: return true;
107: if (c1 == null && c2 != null)
108: return true;
109: if (c1 != null && c2 == null)
110: return false;
111: return c1.compareTo(c2) <= 0;
112: case Query.GT:
113: if (c1 == null && c2 == null)
114: return false;
115: if (c1 == null && c2 != null)
116: return false;
117: if (c1 != null && c2 == null)
118: return true;
119: return c1.compareTo(c2) > 0;
120: case Query.LT:
121: if (c1 == null && c2 == null)
122: return false;
123: if (c1 == null && c2 != null)
124: return true;
125: if (c1 != null && c2 == null)
126: return false;
127: return c1.compareTo(c2) < 0;
128: }
129: return false;
130: }
131: }
|