KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > BinaryOpValueExp


1 /*
2  * @(#)BinaryOpValueExp.java 4.17 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10
11 /**
12  * This class is used by the query-building mechanism to represent binary
13  * operations.
14  * @serial include
15  *
16  * @since 1.5
17  */

18 class BinaryOpValueExp extends QueryEval JavaDoc implements ValueExp JavaDoc {
19     
20     /* Serial version */
21     private static final long serialVersionUID = 1216286847881456786L;
22
23     /**
24      * @serial The operator
25      */

26     private int op;
27     
28     /**
29      * @serial The first value
30      */

31     private ValueExp JavaDoc exp1;
32
33     /**
34      * @serial The second value
35      */

36     private ValueExp JavaDoc exp2;
37
38
39     /**
40      * Basic Constructor.
41      */

42     public BinaryOpValueExp() {
43     }
44
45     /**
46      * Creates a new BinaryOpValueExp using operator o applied on v1 and
47      * v2 values.
48      */

49     public BinaryOpValueExp(int o, ValueExp JavaDoc v1, ValueExp JavaDoc v2) {
50     op = o;
51     exp1 = v1;
52     exp2 = v2;
53     }
54
55
56     /**
57      * Returns the operator of the value expression.
58      */

59     public int getOperator() {
60     return op;
61     }
62     
63     /**
64      * Returns the left value of the value expression.
65      */

66     public ValueExp JavaDoc getLeftValue() {
67     return exp1;
68     }
69    
70     /**
71      * Returns the right value of the value expression.
72      */

73     public ValueExp JavaDoc getRightValue() {
74     return exp2;
75     }
76
77     /**
78      * Applies the BinaryOpValueExp on a MBean.
79      *
80      * @param name The name of the MBean on which the BinaryOpValueExp will be applied.
81      *
82      * @return The ValueExp.
83      *
84      * @exception BadStringOperationException
85      * @exception BadBinaryOpValueExpException
86      * @exception BadAttributeValueExpException
87      * @exception InvalidApplicationException
88      */

89     public ValueExp JavaDoc apply(ObjectName JavaDoc name) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc,
90     BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc {
91     ValueExp JavaDoc val1 = exp1.apply(name);
92     ValueExp JavaDoc val2 = exp2.apply(name);
93     String JavaDoc sval1;
94     String JavaDoc sval2;
95     double dval1;
96     double dval2;
97     long lval1;
98     long lval2;
99     boolean numeric = val1 instanceof NumericValueExp JavaDoc;
100     
101     if (numeric) {
102         if (((NumericValueExp JavaDoc)val1).isLong()) {
103         lval1 = ((NumericValueExp JavaDoc)val1).longValue();
104         lval2 = ((NumericValueExp JavaDoc)val2).longValue();
105
106         switch (op) {
107         case Query.PLUS:
108             return Query.value(lval1 + lval2);
109         case Query.TIMES:
110             return Query.value(lval1 * lval2);
111         case Query.MINUS:
112             return Query.value(lval1 - lval2);
113         case Query.DIV:
114             return Query.value(lval1 / lval2);
115         }
116         
117         } else {
118         dval1 = ((NumericValueExp JavaDoc)val1).doubleValue();
119         dval2 = ((NumericValueExp JavaDoc)val2).doubleValue();
120
121         switch (op) {
122         case Query.PLUS:
123             return Query.value(dval1 + dval2);
124         case Query.TIMES:
125             return Query.value(dval1 * dval2);
126         case Query.MINUS:
127             return Query.value(dval1 - dval2);
128         case Query.DIV:
129             return Query.value(dval1 / dval2);
130         }
131         }
132     } else {
133         sval1 = ((StringValueExp JavaDoc)val1).getValue();
134         sval2 = ((StringValueExp JavaDoc)val2).getValue();
135
136         switch (op) {
137         case Query.PLUS:
138         return new StringValueExp JavaDoc(sval1 + sval2);
139         default:
140         throw new BadStringOperationException JavaDoc(opString());
141         }
142     }
143     
144     throw new BadBinaryOpValueExpException JavaDoc(this);
145     }
146
147     /**
148      * Returns the string representing the object
149      */

150     public String JavaDoc toString() {
151     try {
152         return exp1 + " " + opString() + " " + exp2;
153     } catch (BadBinaryOpValueExpException JavaDoc ex) {
154         return "invalid expression";
155     }
156     }
157     
158     private String JavaDoc opString() throws BadBinaryOpValueExpException JavaDoc {
159     switch (op) {
160     case Query.PLUS:
161         return "+";
162     case Query.TIMES:
163         return "*";
164     case Query.MINUS:
165         return "-";
166     case Query.DIV:
167         return "/";
168     }
169     
170     throw new BadBinaryOpValueExpException JavaDoc(this);
171     }
172
173  }
174
Popular Tags