J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package 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 | */ |
| 36 | class 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) throws BadStringOperationException, BadBinaryOpValueExpException, |
| 95 | BadAttributeValueExpException, InvalidApplicationException { |
| 96 | if (valueList != null) { |
| 97 | ValueExp v = val.apply(name); |
| 98 | boolean numeric = v instanceof NumericValueExp; |
| 99 | |
| 100 | for (int i = 0; i < valueList.length; i++) { |
| 101 | if (numeric) { |
| 102 | if (((NumericValueExp)valueList[i]).doubleValue() == |
| 103 | ((NumericValueExp)v).doubleValue()) { |
| 104 | return true; |
| 105 | } |
| 106 | } else { |
| 107 | if (((StringValueExp)valueList[i]).getValue().equals( |
| 108 | ((StringValueExp)v).getValue())) { |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the string representing the object. |
| 119 | */ |
| 120 | public String toString() { |
| 121 | return val + " in (" + generateValueList() + ")"; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | private String generateValueList() { |
| 126 | if (valueList == null || valueList.length == 0) { |
| 127 | return ""; |
| 128 | } |
| 129 | |
| 130 | final StringBuilder result = |
| 131 | new StringBuilder(valueList[0].toString()); |
| 132 | |
| 133 | for (int i = 1; i < valueList.length; i++) { |
| 134 | result.append(", "); |
| 135 | result.append(valueList[i]); |
| 136 | } |
| 137 | |
| 138 | return result.toString(); |
| 139 | } |
| 140 | |
| 141 | } |