KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > InQueryExp


1 /*
2  * @(#)InQueryExp.java 4.19 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 InQueryExp extends QueryEval JavaDoc implements QueryExp JavaDoc {
19
20     /* Serial version */
21     private static final long serialVersionUID = -5801329450358952434L;
22
23     /**
24      * @serial The {@link ValueExp} to be found
25      */

26     private ValueExp JavaDoc val;
27
28     /**
29      * @serial The array of {@link ValueExp} to be searched
30      */

31     private ValueExp JavaDoc[] valueList;
32
33
34     /**
35      * Basic Constructor.
36      */

37     public InQueryExp() {
38     }
39     
40     /**
41      * Creates a new InQueryExp with the specified ValueExp to be found in
42      * a specified array of ValueExp.
43      */

44     public InQueryExp(ValueExp JavaDoc v1, ValueExp JavaDoc items[]) {
45     val = v1;
46     valueList = items;
47     }
48     
49
50     /**
51      * Returns the checked value of the query.
52      */

53     public ValueExp JavaDoc getCheckedValue() {
54     return val;
55     }
56     
57     /**
58      * Returns the array of values of the query.
59      */

60     public ValueExp JavaDoc[] getExplicitValues() {
61     return valueList;
62     }
63
64     /**
65      * Applies the InQueryExp on a MBean.
66      *
67      * @param name The name of the MBean on which the InQueryExp will be applied.
68      *
69      * @return True if the query was successfully applied to the MBean, false otherwise.
70      *
71      * @exception BadStringOperationException
72      * @exception BadBinaryOpValueExpException
73      * @exception BadAttributeValueExpException
74      * @exception InvalidApplicationException
75      */

76     public boolean apply(ObjectName JavaDoc name) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc,
77     BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc {
78     if (valueList != null) {
79         ValueExp JavaDoc v = val.apply(name);
80         boolean numeric = v instanceof NumericValueExp JavaDoc;
81         
82         for (int i = 0; i < valueList.length; i++) {
83         if (numeric) {
84             if (((NumericValueExp JavaDoc)valueList[i]).doubleValue() ==
85             ((NumericValueExp JavaDoc)v).doubleValue()) {
86             return true;
87             }
88         } else {
89             if (((StringValueExp JavaDoc)valueList[i]).getValue().equals(
90             ((StringValueExp JavaDoc)v).getValue())) {
91             return true;
92             }
93         }
94         }
95     }
96     return false;
97     }
98
99     /**
100      * Returns the string representing the object.
101      */

102     public String JavaDoc toString() {
103     return val + " in (" + generateValueList() + ")";
104     }
105
106
107     private String JavaDoc generateValueList() {
108     if (valueList == null || valueList.length == 0) {
109         return "";
110     }
111     
112     StringBuffer JavaDoc result = new StringBuffer JavaDoc(valueList[0].toString());
113     
114     for (int i = 1; i < valueList.length; i++) {
115         result.append(", ");
116         result.append(valueList[i]);
117     }
118     
119     return result.toString();
120     }
121  
122  }
123
Popular Tags