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 BetweenQueryExp extends QueryEval implements QueryExp {
016: private static final long serialVersionUID = -2933597532866307444L;
017:
018: /**
019: * @serial The lower value
020: */
021: private final ValueExp exp1;
022: /**
023: * @serial The value to test
024: */
025: private final ValueExp exp2;
026: /**
027: * The upper value
028: */
029: private final ValueExp exp3;
030:
031: BetweenQueryExp(ValueExp exp1, ValueExp exp2, ValueExp exp3) {
032: this .exp1 = exp1;
033: this .exp2 = exp2;
034: this .exp3 = exp3;
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: if (exp3 != null)
044: exp3.setMBeanServer(server);
045: }
046:
047: public boolean apply(ObjectName name)
048: throws BadStringOperationException,
049: BadBinaryOpValueExpException,
050: BadAttributeValueExpException, InvalidApplicationException {
051: if (exp1 != null && exp2 != null && exp3 != null) {
052: ValueExp val1 = exp1.apply(name);
053: ValueExp val2 = exp2.apply(name);
054: ValueExp val3 = exp3.apply(name);
055:
056: if (val1 instanceof NumericValueExp
057: && val2 instanceof NumericValueExp
058: && val3 instanceof NumericValueExp) {
059: NumericValueExp num1 = (NumericValueExp) val1;
060: NumericValueExp num2 = (NumericValueExp) val2;
061: NumericValueExp num3 = (NumericValueExp) val3;
062:
063: if (num1.isDouble() || num2.isDouble()
064: || num3.isDouble()) {
065: return isBetween(new Double(num1.doubleValue()),
066: new Double(num2.doubleValue()), new Double(
067: num3.doubleValue()));
068: } else {
069: return isBetween(new Long(num1.longValue()),
070: new Long(num2.longValue()), new Long(num3
071: .longValue()));
072: }
073: }
074: /*
075: else if (val1 instanceof StringValueExp && val2 instanceof StringValueExp && val3 instanceof StringValueExp)
076: {
077: String s1 = ((StringValueExp)val1).getValue();
078: String s2 = ((StringValueExp)val2).getValue();
079: String s3 = ((StringValueExp)val3).getValue();
080: return isBetween(s1, s2, s3);
081: }
082: */
083: else {
084: throw new InvalidApplicationException(
085: "Values are not numeric");
086: }
087: }
088:
089: return false;
090: }
091:
092: private boolean isBetween(Comparable c1, Comparable c2,
093: Comparable c3) {
094: if (c1 == null && c2 == null && c3 == null)
095: return true;
096: if (c1 == null && (c2 == null || c3 == null))
097: return true;
098: if (c1 == null)
099: return false;
100: if (c1 != null && (c2 == null || c3 == null))
101: return false;
102: return c1.compareTo(c2) >= 0 && c1.compareTo(c3) <= 0;
103: }
104: }
|