Path: blob/master/src/java.management/share/classes/javax/management/BinaryRelQueryExp.java
41155 views
/*1* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;262728/**29* This class is used by the query-building mechanism to represent binary30* operations.31* @serial include32*33* @since 1.534*/35class BinaryRelQueryExp extends QueryEval implements QueryExp {3637/* Serial version */38private static final long serialVersionUID = -5690656271650491000L;3940/**41* @serial The operator42*/43private int relOp;4445/**46* @serial The first value47*/48private ValueExp exp1;4950/**51* @serial The second value52*/53private ValueExp exp2;545556/**57* Basic Constructor.58*/59public BinaryRelQueryExp() {60}6162/**63* Creates a new BinaryRelQueryExp with operator op applied on v1 and64* v2 values.65*/66public BinaryRelQueryExp(int op, ValueExp v1, ValueExp v2) {67relOp = op;68exp1 = v1;69exp2 = v2;70}717273/**74* Returns the operator of the query.75*/76public int getOperator() {77return relOp;78}7980/**81* Returns the left value of the query.82*/83public ValueExp getLeftValue() {84return exp1;85}8687/**88* Returns the right value of the query.89*/90public ValueExp getRightValue() {91return exp2;92}9394/**95* Applies the BinaryRelQueryExp on an MBean.96*97* @param name The name of the MBean on which the BinaryRelQueryExp will be applied.98*99* @return True if the query was successfully applied to the MBean, false otherwise.100*101* @exception BadStringOperationException102* @exception BadBinaryOpValueExpException103* @exception BadAttributeValueExpException104* @exception InvalidApplicationException105*/106public boolean apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,107BadAttributeValueExpException, InvalidApplicationException {108Object val1 = exp1.apply(name);109Object val2 = exp2.apply(name);110boolean numeric = val1 instanceof NumericValueExp;111boolean bool = val1 instanceof BooleanValueExp;112if (numeric) {113if (((NumericValueExp)val1).isLong()) {114long lval1 = ((NumericValueExp)val1).longValue();115long lval2 = ((NumericValueExp)val2).longValue();116117switch (relOp) {118case Query.GT:119return lval1 > lval2;120case Query.LT:121return lval1 < lval2;122case Query.GE:123return lval1 >= lval2;124case Query.LE:125return lval1 <= lval2;126case Query.EQ:127return lval1 == lval2;128}129} else {130double dval1 = ((NumericValueExp)val1).doubleValue();131double dval2 = ((NumericValueExp)val2).doubleValue();132133switch (relOp) {134case Query.GT:135return dval1 > dval2;136case Query.LT:137return dval1 < dval2;138case Query.GE:139return dval1 >= dval2;140case Query.LE:141return dval1 <= dval2;142case Query.EQ:143return dval1 == dval2;144}145}146147} else if (bool) {148149boolean bval1 = ((BooleanValueExp)val1).getValue().booleanValue();150boolean bval2 = ((BooleanValueExp)val2).getValue().booleanValue();151152switch (relOp) {153case Query.GT:154return bval1 && !bval2;155case Query.LT:156return !bval1 && bval2;157case Query.GE:158return bval1 || !bval2;159case Query.LE:160return !bval1 || bval2;161case Query.EQ:162return bval1 == bval2;163}164165} else {166String sval1 = ((StringValueExp)val1).getValue();167String sval2 = ((StringValueExp)val2).getValue();168169switch (relOp) {170case Query.GT:171return sval1.compareTo(sval2) > 0;172case Query.LT:173return sval1.compareTo(sval2) < 0;174case Query.GE:175return sval1.compareTo(sval2) >= 0;176case Query.LE:177return sval1.compareTo(sval2) <= 0;178case Query.EQ:179return sval1.compareTo(sval2) == 0;180}181}182183return false;184}185186/**187* Returns the string representing the object.188*/189@Override190public String toString() {191return "(" + exp1 + ") " + relOpString() + " (" + exp2 + ")";192}193194private String relOpString() {195switch (relOp) {196case Query.GT:197return ">";198case Query.LT:199return "<";200case Query.GE:201return ">=";202case Query.LE:203return "<=";204case Query.EQ:205return "=";206}207208return "=";209}210211}212213214