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 Operation that is an arguement to a 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 BinaryOpValueExp extends QueryEval implements ValueExp {
041: // Constants ---------------------------------------------------
042:
043: private static final long serialVersionUID = 1216286847881456786L;
044:
045: // Attributes --------------------------------------------------
046:
047: /**
048: * The operation
049: */
050: private int op;
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 BinaryOpValueExp() {
065: }
066:
067: /**
068: * Construct a binary operation value
069: *
070: * @param operation the operation as defined in Query
071: * @param first the first expression in the operation
072: * @param second the second expression in the operation
073: */
074: public BinaryOpValueExp(int operation, ValueExp first,
075: ValueExp second) {
076: this .op = operation;
077: this .exp1 = first;
078: this .exp2 = second;
079: }
080:
081: // Public ------------------------------------------------------
082:
083: // Value Exp Implementation ------------------------------------
084:
085: public ValueExp 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: if (((NumericValueExp) testFirst).isInteger()) {
095: switch (op) {
096: case Query.PLUS:
097: return Query.value(((NumericValueExp) testFirst)
098: .getLongValue()
099: + ((NumericValueExp) testSecond)
100: .getLongValue());
101: case Query.MINUS:
102: return Query.value(((NumericValueExp) testFirst)
103: .getLongValue()
104: - ((NumericValueExp) testSecond)
105: .getLongValue());
106: case Query.TIMES:
107: return Query.value(((NumericValueExp) testFirst)
108: .getLongValue()
109: * ((NumericValueExp) testSecond)
110: .getLongValue());
111: case Query.DIV:
112: return Query.value(((NumericValueExp) testFirst)
113: .getLongValue()
114: / ((NumericValueExp) testSecond)
115: .getLongValue());
116: }
117: } else {
118: switch (op) {
119: case Query.PLUS:
120: return Query.value(((NumericValueExp) testFirst)
121: .getDoubleValue()
122: + ((NumericValueExp) testSecond)
123: .getDoubleValue());
124: case Query.MINUS:
125: return Query.value(((NumericValueExp) testFirst)
126: .getDoubleValue()
127: - ((NumericValueExp) testSecond)
128: .getDoubleValue());
129: case Query.TIMES:
130: return Query.value(((NumericValueExp) testFirst)
131: .getDoubleValue()
132: * ((NumericValueExp) testSecond)
133: .getDoubleValue());
134: case Query.DIV:
135: return Query.value(((NumericValueExp) testFirst)
136: .getDoubleValue()
137: / ((NumericValueExp) testSecond)
138: .getDoubleValue());
139: }
140: }
141: } else if (testFirst instanceof StringValueExp
142: && testSecond instanceof StringValueExp) {
143: switch (op) {
144: case Query.PLUS:
145: return Query.value(new String(testFirst.toString()
146: + testSecond.toString()));
147: }
148: throw new BadStringOperationException("TODO");
149: }
150: // Review What happens now?
151: throw new BadBinaryOpValueExpException(testFirst);
152: }
153:
154: // Object overrides --------------------------------------------
155:
156: public String toString() {
157: StringBuffer buffer = new StringBuffer();
158: buffer.append("(");
159: buffer.append(exp1);
160: buffer.append(")");
161: switch (op) {
162: case Query.PLUS:
163: buffer.append(" + ");
164: break;
165: case Query.MINUS:
166: buffer.append(" - ");
167: break;
168: case Query.TIMES:
169: buffer.append(" * ");
170: break;
171: case Query.DIV:
172: buffer.append(" / ");
173: }
174: buffer.append("(");
175: buffer.append(exp2);
176: buffer.append(")");
177: return buffer.toString();
178: }
179:
180: // Protected ---------------------------------------------------
181:
182: // Package Private ---------------------------------------------
183:
184: // Private -----------------------------------------------------
185:
186: // Inner Classes -----------------------------------------------
187: }
|