jdk/src/share/classes/javax/management/BinaryRelQueryExp.java
author yan
Mon, 12 May 2014 14:33:13 +0400
changeset 24368 2b4801b94265
parent 5506 202f599c92aa
permissions -rw-r--r--
8038795: Tidy warnings cleanup for javax.management Reviewed-by: dfuchs Contributed-by: Alexander Stepanov <alexander.v.stepanov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This class is used by the query-building mechanism to represent binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class BinaryRelQueryExp extends QueryEval implements QueryExp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final long serialVersionUID = -5690656271650491000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * @serial The operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private int relOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @serial The first value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ValueExp exp1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @serial The second value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private ValueExp exp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Basic Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public BinaryRelQueryExp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a new BinaryRelQueryExp with operator op applied on v1 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * v2 values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public BinaryRelQueryExp(int op, ValueExp v1, ValueExp v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        relOp = op;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        exp1  = v1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        exp2  = v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the operator of the query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public int getOperator()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return relOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns the left value of the query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public ValueExp getLeftValue()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return exp1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Returns the right value of the query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public ValueExp getRightValue()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return exp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Applies the BinaryRelQueryExp on an MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param name The name of the MBean on which the BinaryRelQueryExp will be applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return  True if the query was successfully applied to the MBean, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception BadStringOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @exception BadBinaryOpValueExpException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @exception BadAttributeValueExpException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception InvalidApplicationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public boolean apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        BadAttributeValueExpException, InvalidApplicationException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Object val1 = exp1.apply(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Object val2 = exp2.apply(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        boolean numeric = val1 instanceof NumericValueExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        boolean bool = val1 instanceof BooleanValueExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (numeric) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (((NumericValueExp)val1).isLong()) {
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   115
                long lval1 = ((NumericValueExp)val1).longValue();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   116
                long lval2 = ((NumericValueExp)val2).longValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                switch (relOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                case Query.GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    return lval1 > lval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                case Query.LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    return lval1 < lval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                case Query.GE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    return lval1 >= lval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                case Query.LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    return lval1 <= lval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                case Query.EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    return lval1 == lval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   131
                double dval1 = ((NumericValueExp)val1).doubleValue();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   132
                double dval2 = ((NumericValueExp)val2).doubleValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                switch (relOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                case Query.GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    return dval1 > dval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                case Query.LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    return dval1 < dval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                case Query.GE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    return dval1 >= dval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                case Query.LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    return dval1 <= dval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                case Query.EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    return dval1 == dval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else if (bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   150
            boolean bval1 = ((BooleanValueExp)val1).getValue().booleanValue();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   151
            boolean bval2 = ((BooleanValueExp)val2).getValue().booleanValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            switch (relOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            case Query.GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                return bval1 && !bval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            case Query.LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return !bval1 && bval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            case Query.GE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                return bval1 || !bval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            case Query.LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return !bval1 || bval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            case Query.EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                return bval1 == bval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   167
            String sval1 = ((StringValueExp)val1).getValue();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   168
            String sval2 = ((StringValueExp)val2).getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            switch (relOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            case Query.GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return sval1.compareTo(sval2) > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            case Query.LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return sval1.compareTo(sval2) < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            case Query.GE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return sval1.compareTo(sval2) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            case Query.LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return sval1.compareTo(sval2) <= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            case Query.EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                return sval1.compareTo(sval2) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns the string representing the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
471
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents: 34
diff changeset
   190
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public String toString()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return "(" + exp1 + ") " + relOpString() + " (" + exp2 + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private String relOpString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        switch (relOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        case Query.GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        case Query.LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return "<";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        case Query.GE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return ">=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        case Query.LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return "<=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        case Query.EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return "=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return "=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 }