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: * An In Query Expression.<p>
026: *
027: * Returns true only when any of the values are match.
028: *
029: * Returns true only when both expressions are true.
030: *
031: * <p><b>Revisions:</b>
032: * <p><b>20020315 Adrian Brock:</b>
033: * <ul>
034: * <li>Don't put ; on the end of if statements :-)
035: * </ul>
036: * <p><b>20020317 Adrian Brock:</b>
037: * <ul>
038: * <li>Make queries thread safe
039: * </ul>
040: *
041: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
042: * @version $Revision: 57200 $
043: */
044: class InQueryExp extends QueryEval implements QueryExp {
045: // Constants ---------------------------------------------------
046:
047: private static final long serialVersionUID = -5801329450358952434L;
048:
049: // Attributes --------------------------------------------------
050:
051: /**
052: * The value to test
053: */
054: private ValueExp val;
055:
056: /**
057: * The list of values
058: */
059: private ValueExp[] valueList;
060:
061: // Static ------------------------------------------------------
062:
063: // Constructors ------------------------------------------------
064:
065: public InQueryExp() {
066: }
067:
068: /**
069: * Create a new IN query Expression
070: *
071: * @param test the value to test
072: * @param list the list of values
073: */
074: public InQueryExp(ValueExp test, ValueExp[] list) {
075: this .val = test;
076: this .valueList = list;
077: }
078:
079: // Public ------------------------------------------------------
080:
081: // QueryExp implementation -------------------------------------
082:
083: public boolean apply(ObjectName name)
084: throws BadStringOperationException,
085: BadBinaryOpValueExpException,
086: BadAttributeValueExpException, InvalidApplicationException {
087: // REVIEW: Cast Exceptions
088: ValueExp calcTest = val.apply(name);
089: for (int i = 0; i < valueList.length; i++) {
090: ValueExp calcList = valueList[i].apply(name);
091: // Number
092: if (calcTest instanceof NumericValueExp) {
093: if (((NumericValueExp) calcTest).getDoubleValue() == ((NumericValueExp) calcList)
094: .getDoubleValue())
095: return true;
096: }
097: // String
098: else if (calcTest instanceof StringValueExp) {
099: if (calcTest.toString().equals(calcList.toString()))
100: return true;
101: }
102: // Single Value, includes Boolean
103: else if (calcTest instanceof BooleanValueExp) {
104: if (((BooleanValueExp) calcTest).getValue() == ((BooleanValueExp) calcList)
105: .getValue())
106: return true;
107: }
108: }
109: // No match
110: return false;
111: }
112:
113: // Object overrides --------------------------------------------
114:
115: public String toString() {
116: StringBuffer buffer = new StringBuffer("(");
117: buffer.append(val.toString());
118: buffer.append(" in ");
119: for (int i = 1; i < valueList.length; i++) {
120: buffer.append(valueList[i].toString());
121: buffer.append(" ");
122: }
123: buffer.append(")");
124: return buffer.toString();
125: }
126:
127: // Protected ---------------------------------------------------
128:
129: // Private -----------------------------------------------------
130:
131: // Inner classes -----------------------------------------------
132: }
|