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 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 BinaryRelQueryExp extends QueryEval implements QueryExp { 37 38 /* Serial version */ 39 private static final long serialVersionUID = -5690656271650491000L; 40 41 /** 42 * @serial The operator 43 */ 44 private int relOp; 45 46 /** 47 * @serial The first value 48 */ 49 private ValueExp exp1; 50 51 /** 52 * @serial The second value 53 */ 54 private ValueExp exp2; 55 56 57 /** 58 * Basic Constructor. 59 */ BinaryRelQueryExp()60 public BinaryRelQueryExp() { 61 } 62 63 /** 64 * Creates a new BinaryRelQueryExp with operator op applied on v1 and 65 * v2 values. 66 */ BinaryRelQueryExp(int op, ValueExp v1, ValueExp v2)67 public BinaryRelQueryExp(int op, ValueExp v1, ValueExp v2) { 68 relOp = op; 69 exp1 = v1; 70 exp2 = v2; 71 } 72 73 74 /** 75 * Returns the operator of the query. 76 */ getOperator()77 public int getOperator() { 78 return relOp; 79 } 80 81 /** 82 * Returns the left value of the query. 83 */ getLeftValue()84 public ValueExp getLeftValue() { 85 return exp1; 86 } 87 88 /** 89 * Returns the right value of the query. 90 */ getRightValue()91 public ValueExp getRightValue() { 92 return exp2; 93 } 94 95 /** 96 * Applies the BinaryRelQueryExp on an MBean. 97 * 98 * @param name The name of the MBean on which the BinaryRelQueryExp will be applied. 99 * 100 * @return True if the query was successfully applied to the MBean, false otherwise. 101 * 102 * @exception BadStringOperationException 103 * @exception BadBinaryOpValueExpException 104 * @exception BadAttributeValueExpException 105 * @exception InvalidApplicationException 106 */ apply(ObjectName name)107 public boolean apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException, 108 BadAttributeValueExpException, InvalidApplicationException { 109 Object val1 = exp1.apply(name); 110 Object val2 = exp2.apply(name); 111 boolean numeric = val1 instanceof NumericValueExp; 112 boolean bool = val1 instanceof BooleanValueExp; 113 if (numeric) { 114 if (((NumericValueExp)val1).isLong()) { 115 long lval1 = ((NumericValueExp)val1).longValue(); 116 long lval2 = ((NumericValueExp)val2).longValue(); 117 118 switch (relOp) { 119 case Query.GT: 120 return lval1 > lval2; 121 case Query.LT: 122 return lval1 < lval2; 123 case Query.GE: 124 return lval1 >= lval2; 125 case Query.LE: 126 return lval1 <= lval2; 127 case Query.EQ: 128 return lval1 == lval2; 129 } 130 } else { 131 double dval1 = ((NumericValueExp)val1).doubleValue(); 132 double dval2 = ((NumericValueExp)val2).doubleValue(); 133 134 switch (relOp) { 135 case Query.GT: 136 return dval1 > dval2; 137 case Query.LT: 138 return dval1 < dval2; 139 case Query.GE: 140 return dval1 >= dval2; 141 case Query.LE: 142 return dval1 <= dval2; 143 case Query.EQ: 144 return dval1 == dval2; 145 } 146 } 147 148 } else if (bool) { 149 150 boolean bval1 = ((BooleanValueExp)val1).getValue().booleanValue(); 151 boolean bval2 = ((BooleanValueExp)val2).getValue().booleanValue(); 152 153 switch (relOp) { 154 case Query.GT: 155 return bval1 && !bval2; 156 case Query.LT: 157 return !bval1 && bval2; 158 case Query.GE: 159 return bval1 || !bval2; 160 case Query.LE: 161 return !bval1 || bval2; 162 case Query.EQ: 163 return bval1 == bval2; 164 } 165 166 } else { 167 String sval1 = ((StringValueExp)val1).getValue(); 168 String sval2 = ((StringValueExp)val2).getValue(); 169 170 switch (relOp) { 171 case Query.GT: 172 return sval1.compareTo(sval2) > 0; 173 case Query.LT: 174 return sval1.compareTo(sval2) < 0; 175 case Query.GE: 176 return sval1.compareTo(sval2) >= 0; 177 case Query.LE: 178 return sval1.compareTo(sval2) <= 0; 179 case Query.EQ: 180 return sval1.compareTo(sval2) == 0; 181 } 182 } 183 184 return false; 185 } 186 187 /** 188 * Returns the string representing the object. 189 */ 190 @Override toString()191 public String toString() { 192 return "(" + exp1 + ") " + relOpString() + " (" + exp2 + ")"; 193 } 194 relOpString()195 private String relOpString() { 196 switch (relOp) { 197 case Query.GT: 198 return ">"; 199 case Query.LT: 200 return "<"; 201 case Query.GE: 202 return ">="; 203 case Query.LE: 204 return "<="; 205 case Query.EQ: 206 return "="; 207 } 208 209 return "="; 210 } 211 212 } 213