1/*
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.management;
27
28
29/**
30 * This class is used by the query-building mechanism to represent binary
31 * operations.
32 * @serial include
33 *
34 * @since 1.5
35 */
36class InQueryExp extends QueryEval implements QueryExp {
37
38    /* Serial version */
39    private static final long serialVersionUID = -5801329450358952434L;
40
41    /**
42     * @serial The {@link ValueExp} to be found
43     */
44    private ValueExp val;
45
46    /**
47     * @serial The array of {@link ValueExp} to be searched
48     */
49    private ValueExp[]  valueList;
50
51
52    /**
53     * Basic Constructor.
54     */
55    public InQueryExp() {
56    }
57
58    /**
59     * Creates a new InQueryExp with the specified ValueExp to be found in
60     * a specified array of ValueExp.
61     */
62    public InQueryExp(ValueExp v1, ValueExp items[]) {
63        val       = v1;
64        valueList = items;
65    }
66
67
68    /**
69     * Returns the checked value of the query.
70     */
71    public ValueExp getCheckedValue()  {
72        return val;
73    }
74
75    /**
76     * Returns the array of values of the query.
77     */
78    public ValueExp[] getExplicitValues()  {
79        return valueList;
80    }
81
82    /**
83     * Applies the InQueryExp on a MBean.
84     *
85     * @param name The name of the MBean on which the InQueryExp will be applied.
86     *
87     * @return  True if the query was successfully applied to the MBean, false otherwise.
88     *
89     * @exception BadStringOperationException
90     * @exception BadBinaryOpValueExpException
91     * @exception BadAttributeValueExpException
92     * @exception InvalidApplicationException
93     */
94    public boolean apply(ObjectName name)
95    throws BadStringOperationException, BadBinaryOpValueExpException,
96        BadAttributeValueExpException, InvalidApplicationException  {
97        if (valueList != null) {
98            ValueExp v      = val.apply(name);
99            boolean numeric = v instanceof NumericValueExp;
100
101            for (ValueExp element : valueList) {
102                element = element.apply(name);
103                if (numeric) {
104                    if (((NumericValueExp) element).doubleValue() ==
105                        ((NumericValueExp) v).doubleValue()) {
106                        return true;
107                    }
108                } else {
109                    if (((StringValueExp) element).getValue().equals(
110                        ((StringValueExp) v).getValue())) {
111                        return true;
112                    }
113                }
114            }
115        }
116        return false;
117    }
118
119    /**
120     * Returns the string representing the object.
121     */
122    public String toString()  {
123        return val + " in (" + generateValueList() + ")";
124    }
125
126
127    private String generateValueList() {
128        if (valueList == null || valueList.length == 0) {
129            return "";
130        }
131
132        final StringBuilder result =
133                new StringBuilder(valueList[0].toString());
134
135        for (int i = 1; i < valueList.length; i++) {
136            result.append(", ");
137            result.append(valueList[i]);
138        }
139
140        return result.toString();
141    }
142
143 }
144