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: import org.jboss.mx.util.QueryExpSupport;
025:
026: /**
027: * A Between Query Expression.<p>
028: *
029: * Returns true only when the test expression is between the lower and
030: * upper bounds inclusive.
031: *
032: * <p><b>Revisions:</b>
033: * <p><b>20020314 Adrian Brock:</b>
034: * <ul>
035: * <li>Fix the human readable expression
036: * </ul>
037: * <p><b>20020317 Adrian Brock:</b>
038: * <ul>
039: * <li>Make queries thread safe
040: * </ul>
041: *
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
043: * @version $Revision: 57200 $
044: */
045: class BetweenQueryExp extends QueryEval implements QueryExp {
046: // Constants ---------------------------------------------------
047:
048: private static final long serialVersionUID = -2933597532866307444L;
049:
050: // Attributes --------------------------------------------------
051:
052: /**
053: * The value to test
054: */
055: private ValueExp exp1;
056:
057: /**
058: * The lower bound
059: */
060: private ValueExp exp2;
061:
062: /**
063: * The upper bound
064: */
065: private ValueExp exp3;
066:
067: // Static ------------------------------------------------------
068:
069: // Constructors ------------------------------------------------
070:
071: public BetweenQueryExp() {
072: }
073:
074: /**
075: * Create a new BETWEEN query Expression
076: *
077: * @param test the value to test
078: * @param lower the lower bound
079: * @param upper the upper bound
080: */
081: public BetweenQueryExp(ValueExp test, ValueExp lower, ValueExp upper) {
082: this .exp1 = test;
083: this .exp2 = lower;
084: this .exp3 = upper;
085: }
086:
087: // Public ------------------------------------------------------
088:
089: // QueryExp implementation -------------------------------------
090:
091: public boolean apply(ObjectName name)
092: throws BadStringOperationException,
093: BadBinaryOpValueExpException,
094: BadAttributeValueExpException, InvalidApplicationException {
095: ValueExp calcTest = exp1.apply(name);
096: ValueExp calcLower = exp2.apply(name);
097: ValueExp calcUpper = exp3.apply(name);
098:
099: // Number
100: if (calcTest instanceof NumericValueExp
101: && calcLower instanceof NumericValueExp
102: && calcUpper instanceof NumericValueExp) {
103: // REVIEW: Exceptions for cast problems, which one?
104: double valueTest = ((NumericValueExp) calcTest)
105: .getDoubleValue();
106: double valueLower = ((NumericValueExp) calcLower)
107: .getDoubleValue();
108: double valueUpper = ((NumericValueExp) calcUpper)
109: .getDoubleValue();
110: return (valueLower <= valueTest && valueTest <= valueUpper);
111: }
112: // String
113: else if (calcTest instanceof StringValueExp
114: && calcLower instanceof StringValueExp
115: && calcUpper instanceof StringValueExp) {
116: // REVIEW: Exceptions for cast problems, which one?
117: String valueTest = calcTest.toString();
118: String valueLower = calcLower.toString();
119: String valueUpper = calcUpper.toString();
120: return (valueLower.compareTo(valueTest) <= 0 && valueUpper
121: .compareTo(valueTest) >= 0);
122: }
123: // Review What happens now?
124: throw new BadBinaryOpValueExpException(calcTest);
125: }
126:
127: public void setMBeanServer(MBeanServer server) {
128: QueryExpSupport.server.set(server);
129: }
130:
131: // Object overrides --------------------------------------------
132:
133: public String toString() {
134: return new String("(" + exp2.toString() + ") <= ("
135: + exp1.toString() + ") <= (" + exp3.toString())
136: + ")";
137: }
138:
139: // Protected ---------------------------------------------------
140:
141: // Private -----------------------------------------------------
142:
143: // Inner classes -----------------------------------------------
144: }
|