jdk/src/java.management/share/classes/javax/management/openmbean/OpenMBeanAttributeInfoSupport.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23010 jdk/src/share/classes/javax/management/openmbean/OpenMBeanAttributeInfoSupport.java@6dadb192ad81
child 32034 05676cfd40b5
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21656
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 3446
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: 3446
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: 3446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.management.openmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
// java import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.DescriptorRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.jmx.remote.util.EnvHelp;
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
    48
import sun.reflect.misc.ConstructorUtil;
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
    49
import sun.reflect.misc.MethodUtil;
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
    50
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Describes an attribute of an open MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class OpenMBeanAttributeInfoSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    extends MBeanAttributeInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    implements OpenMBeanAttributeInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final long serialVersionUID = -4867215622149721849L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @serial The open mbean attribute's <i>open type</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private OpenType<?> openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @serial The open mbean attribute's default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final Object defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @serial The open mbean attribute's legal values. This {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Set} is unmodifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final Set<?> legalValues;  // to be constructed unmodifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @serial The open mbean attribute's min value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    84
    private final Comparable<?> minValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @serial The open mbean attribute's max value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    89
    private final Comparable<?> maxValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // As this instance is immutable, these two values need only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // be calculated once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private transient Integer myHashCode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private transient String  myToString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * specified {@code name}, {@code openType} and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * description}, and the specified read/write access properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param isIs {@code true} if the attribute's getter is of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * form <tt>is<i>XXX</i></tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public OpenMBeanAttributeInfoSupport(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                         String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                         OpenType<?> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                         boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                         boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                         boolean isIs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             (Descriptor) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * specified {@code name}, {@code openType}, {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * description}, read/write access properties, and {@code Descriptor}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>The {@code descriptor} can contain entries that will define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * the values returned by certain methods of this class, as
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 18220
diff changeset
   141
     * explained in the <a href="package-summary.html#constraints">
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 18220
diff changeset
   142
     * package description</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param isIs {@code true} if the attribute's getter is of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * form <tt>is<i>XXX</i></tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param descriptor The descriptor for the attribute.  This may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * which is equivalent to an empty descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * null, or the descriptor entries are invalid as described in the
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 18220
diff changeset
   165
     * <a href="package-summary.html#constraints">package description</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public OpenMBeanAttributeInfoSupport(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                         String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                         OpenType<?> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                         boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                         boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                         boolean isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                         Descriptor descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // Construct parent's state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        super(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
              (openType==null) ? null : openType.getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
              description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
              isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
              isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
              isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
              ImmutableDescriptor.union(descriptor, (openType==null)?null:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                openType.getDescriptor()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // Initialize this instance's specific state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.openType = openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        descriptor = getDescriptor();  // replace null by empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this.defaultValue = valueFrom(descriptor, "defaultValue", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.legalValues = valuesFrom(descriptor, "legalValues", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this.minValue = comparableValueFrom(descriptor, "minValue", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.maxValue = comparableValueFrom(descriptor, "maxValue", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            check(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new IllegalArgumentException(e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * specified {@code name}, {@code openType}, {@code description}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * and {@code defaultValue}, and the specified read/write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param isIs {@code true} if the attribute's getter is of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * form <tt>is<i>XXX</i></tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param defaultValue must be a valid value for the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * openType} specified for this attribute; default value not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * supported for {@code ArrayType} and {@code TabularType}; can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * be null, in which case it means that no default value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param <T> allows the compiler to check that the {@code defaultValue},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * if non-null, has the correct Java type for the given {@code openType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws OpenDataException if {@code defaultValue} is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * valid value for the specified {@code openType}, or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * defaultValue} is non null and {@code openType} is an {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * ArrayType} or a {@code TabularType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public <T> OpenMBeanAttributeInfoSupport(String   name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                             String   description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                             OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                             boolean  isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                             boolean  isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                             boolean  isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                             T        defaultValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             defaultValue, (T[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * specified {@code name}, {@code openType}, {@code description},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * {@code defaultValue} and {@code legalValues}, and the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * read/write access properties.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p>The contents of {@code legalValues} are copied, so subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * modifications of the array referenced by {@code legalValues}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * have no impact on this {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * instance.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param isIs {@code true} if the attribute's getter is of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * form <tt>is<i>XXX</i></tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param defaultValue must be a valid value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * for the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * openType} specified for this attribute; default value not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * supported for {@code ArrayType} and {@code TabularType}; can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * be null, in which case it means that no default value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param legalValues each contained value must be valid for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * {@code openType} specified for this attribute; legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * not supported for {@code ArrayType} and {@code TabularType};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * can be null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param <T> allows the compiler to check that the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * defaultValue} and {@code legalValues}, if non-null, have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * correct Java type for the given {@code openType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @throws OpenDataException if {@code defaultValue} is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * valid value for the specified {@code openType}, or one value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@code legalValues} is not valid for the specified {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * openType}, or {@code defaultValue} is non null and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * openType} is an {@code ArrayType} or a {@code TabularType}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * {@code legalValues} is non null and non empty and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * openType} is an {@code ArrayType} or a {@code TabularType}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * {@code legalValues} is non null and non empty and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * defaultValue} is not contained in {@code legalValues}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public <T> OpenMBeanAttributeInfoSupport(String   name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                             String   description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                             OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                             boolean  isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                             boolean  isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                             boolean  isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                             T        defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                             T[]      legalValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
             defaultValue, legalValues, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * which describes the attribute of an open MBean, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * specified {@code name}, {@code openType}, {@code description},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * {@code defaultValue}, {@code minValue} and {@code maxValue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * It is possible to specify minimal and maximal values only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * an open type whose values are {@code Comparable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param isIs {@code true} if the attribute's getter is of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * form <tt>is<i>XXX</i></tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param defaultValue must be a valid value for the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * openType} specified for this attribute; default value not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * supported for {@code ArrayType} and {@code TabularType}; can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * null, in which case it means that no default value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param minValue must be valid for the {@code openType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * specified for this attribute; can be null, in which case it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * means that no minimal value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param maxValue must be valid for the {@code openType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * specified for this attribute; can be null, in which case it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * means that no maximal value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param <T> allows the compiler to check that the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * defaultValue}, {@code minValue}, and {@code maxValue}, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * non-null, have the correct Java type for the given {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * openType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @throws OpenDataException if {@code defaultValue}, {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * minValue} or {@code maxValue} is not a valid value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * specified {@code openType}, or {@code defaultValue} is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * and {@code openType} is an {@code ArrayType} or a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * TabularType}, or both {@code minValue} and {@code maxValue} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * non-null and {@code minValue.compareTo(maxValue) > 0} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * true}, or both {@code defaultValue} and {@code minValue} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * non-null and {@code minValue.compareTo(defaultValue) > 0} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * {@code true}, or both {@code defaultValue} and {@code maxValue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * are non-null and {@code defaultValue.compareTo(maxValue) > 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public <T> OpenMBeanAttributeInfoSupport(String     name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                             String     description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                             OpenType<T>   openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                             boolean    isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                             boolean    isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                             boolean    isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                             T          defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                             Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                             Comparable<T> maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
             defaultValue, null, minValue, maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private <T> OpenMBeanAttributeInfoSupport(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                              String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                              OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                              boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                              boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                              boolean isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                              T defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                              T[] legalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                              Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                              Comparable<T> maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        super(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
              (openType==null) ? null : openType.getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
              description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
              isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
              isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
              isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
              makeDescriptor(openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                             defaultValue, legalValues, minValue, maxValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        this.openType = openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        Descriptor d = getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        this.defaultValue = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        this.minValue = minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        this.maxValue = maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // We already converted the array into an unmodifiable Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // in the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        this.legalValues = (Set<?>) d.getFieldValue("legalValues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        check(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * An object serialized in a version of the API before Descriptors were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * added to this class will have an empty or null Descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * For consistency with our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * behavior in this version, we must replace the object with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * where the Descriptors reflect the same values of openType, defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (getDescriptor().getFieldNames().length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            OpenType<Object> xopenType = cast(openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            Set<Object> xlegalValues = cast(legalValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            Comparable<Object> xminValue = cast(minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            Comparable<Object> xmaxValue = cast(maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return new OpenMBeanAttributeInfoSupport(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    name, description, openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    isReadable(), isWritable(), isIs(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    makeDescriptor(xopenType, defaultValue, xlegalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                   xminValue, xmaxValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    static void check(OpenMBeanParameterInfo info) throws OpenDataException {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   455
        OpenType<?> openType = info.getOpenType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (openType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new IllegalArgumentException("OpenType cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (info.getName() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                info.getName().trim().equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throw new IllegalArgumentException("Name cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (info.getDescription() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                info.getDescription().trim().equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            throw new IllegalArgumentException("Description cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // Check and initialize defaultValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (info.hasDefaultValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // Default value not supported for ArrayType and TabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // Cast to Object because "OpenType<T> instanceof" is illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (openType.isArray() || (Object)openType instanceof TabularType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                throw new OpenDataException("Default value not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                            "for ArrayType and TabularType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // Check defaultValue's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (!openType.isValue(info.getDefaultValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    "Argument defaultValue's class [\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    info.getDefaultValue().getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    "\"] does not match the one defined in openType[\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    openType.getClassName() +"\"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // Check that we don't have both legalValues and min or max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (info.hasLegalValues() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                (info.hasMinValue() || info.hasMaxValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            throw new OpenDataException("cannot have both legalValue and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                        "minValue or maxValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // Check minValue and maxValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (info.hasMinValue() && !openType.isValue(info.getMinValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                "Type of minValue [" + info.getMinValue().getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                "] does not match OpenType [" + openType.getClassName() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (info.hasMaxValue() && !openType.isValue(info.getMaxValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                "Type of maxValue [" + info.getMaxValue().getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                "] does not match OpenType [" + openType.getClassName() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // Check that defaultValue is a legal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (info.hasDefaultValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            Object defaultValue = info.getDefaultValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (info.hasLegalValues() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    !info.getLegalValues().contains(defaultValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                throw new OpenDataException("defaultValue is not contained " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                            "in legalValues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            // Check that minValue <= defaultValue <= maxValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (info.hasMinValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (compare(info.getMinValue(), defaultValue) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    throw new OpenDataException("minValue cannot be greater " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                                "than defaultValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (info.hasMaxValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                if (compare(info.getMaxValue(), defaultValue) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    throw new OpenDataException("maxValue cannot be less " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                                "than defaultValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // Check legalValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (info.hasLegalValues()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // legalValues not supported for TabularType and arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if ((Object)openType instanceof TabularType || openType.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                throw new OpenDataException("Legal values not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                            "for TabularType and arrays");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            // Check legalValues are valid with openType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            for (Object v : info.getLegalValues()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (!openType.isValue(v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        "Element of legalValues [" + v +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        "] is not a valid value for the specified openType [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        openType.toString() +"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // Check that, if both specified, minValue <= maxValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (info.hasMinValue() && info.hasMaxValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (compare(info.getMinValue(), info.getMaxValue()) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                throw new OpenDataException("minValue cannot be greater " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                            "than maxValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   567
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    static int compare(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return ((Comparable) x).compareTo(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    static <T> Descriptor makeDescriptor(OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                         T defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                         T[] legalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                         Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                         Comparable<T> maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        Map<String, Object> map = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        if (defaultValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            map.put("defaultValue", defaultValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (legalValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            Set<T> set = new HashSet<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            for (T v : legalValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                set.add(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            set = Collections.unmodifiableSet(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            map.put("legalValues", set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (minValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            map.put("minValue", minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (maxValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            map.put("maxValue", maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (map.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            return openType.getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            map.put("openType", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return new ImmutableDescriptor(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    static <T> Descriptor makeDescriptor(OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                         T defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                         Set<T> legalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                         Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                         Comparable<T> maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        T[] legals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (legalValues == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            legals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            legals = cast(new Object[legalValues.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            legalValues.toArray(legals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return makeDescriptor(openType, defaultValue, legals, minValue, maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    static <T> T valueFrom(Descriptor d, String name, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Object x = d.getFieldValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return convertFrom(x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                "Cannot convert descriptor field " + name + "  to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                openType.getTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            throw EnvHelp.initCause(new IllegalArgumentException(msg), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    static <T> Set<T> valuesFrom(Descriptor d, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                 OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        Object x = d.getFieldValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        Collection<?> coll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (x instanceof Set<?>) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            Set<?> set = (Set<?>) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            boolean asis = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            for (Object element : set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                if (!openType.isValue(element)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    asis = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (asis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                return cast(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            coll = set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        } else if (x instanceof Object[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            coll = Arrays.asList((Object[]) x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                "Descriptor value for " + name + " must be a Set or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                "an array: " + x.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        Set<T> result = new HashSet<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        for (Object element : coll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            result.add(convertFrom(element, openType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   662
    static <T> Comparable<?> comparableValueFrom(Descriptor d, String name,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   663
                                                 OpenType<T> openType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        T t = valueFrom(d, name, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (t == null || t instanceof Comparable<?>)
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   666
            return (Comparable<?>) t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            "Descriptor field " + name + " with value " + t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            " is not Comparable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    private static <T> T convertFrom(Object x, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (openType.isValue(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            T t = OpenMBeanAttributeInfoSupport.<T>cast(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return convertFromStrings(x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    private static <T> T convertFromStrings(Object x, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (openType instanceof ArrayType<?>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return convertFromStringArray(x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        else if (x instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return convertFromString((String) x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            "Cannot convert value " + x + " of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            x.getClass().getName() + " to type " + openType.getTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    private static <T> T convertFromString(String s, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        Class<T> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   695
            ReflectUtil.checkPackageAccess(openType.safeGetClassName());
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   696
            c = cast(Class.forName(openType.safeGetClassName()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            throw new NoClassDefFoundError(e.toString());  // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Look for: public static T valueOf(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        Method valueOf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   704
            // It is safe to call this plain Class.getMethod because the class "c"
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   705
            // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            valueOf = c.getMethod("valueOf", String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if (!Modifier.isStatic(valueOf.getModifiers()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    valueOf.getReturnType() != c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                valueOf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            valueOf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (valueOf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   715
                return c.cast(MethodUtil.invoke(valueOf, null, new Object[] {s}));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    "Could not convert \"" + s + "\" using method: " + valueOf;
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   719
                throw new IllegalArgumentException(msg, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        // Look for: public T(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        Constructor<T> con;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   726
            // It is safe to call this plain Class.getConstructor because the class "c"
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   727
            // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            con = c.getConstructor(String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            con = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (con != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                return con.newInstance(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    "Could not convert \"" + s + "\" using constructor: " + con;
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   738
                throw new IllegalArgumentException(msg, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        throw new IllegalArgumentException("Don't know how to convert " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                                           "string to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                           openType.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /* A Descriptor contained an array value encoded as Strings.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
       Strings must be organized in an array corresponding to the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
       array.  If the desired array has n dimensions, so must the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
       array.  We will convert element by element from String to desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
       component type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    private static <T> T convertFromStringArray(Object x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                                OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        ArrayType<?> arrayType = (ArrayType<?>) openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        OpenType<?> baseType = arrayType.getElementOpenType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        int dim = arrayType.getDimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        String squareBrackets = "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        for (int i = 1; i < dim; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            squareBrackets += "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        Class<?> stringArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        Class<?> targetArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            stringArrayClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                Class.forName(squareBrackets + "Ljava.lang.String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            targetArrayClass =
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   767
                Class.forName(squareBrackets + "L" + baseType.safeGetClassName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                              ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            throw new NoClassDefFoundError(e.toString());  // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (!stringArrayClass.isInstance(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                "Value for " + dim + "-dimensional array of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                baseType.getTypeName() + " must be same type or a String " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                "array with same dimensions";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        OpenType<?> componentOpenType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (dim == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            componentOpenType = baseType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                componentOpenType = new ArrayType<T>(dim - 1, baseType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                throw new IllegalArgumentException(e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        int n = Array.getLength(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        Object[] targetArray = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            Array.newInstance(targetArrayClass.getComponentType(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            Object stringish = Array.get(x, i);  // String or String[] etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            Object converted =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                convertFromStrings(stringish, componentOpenType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            Array.set(targetArray, i, converted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        return OpenMBeanAttributeInfoSupport.<T>cast(targetArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    static <T> T cast(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        return (T) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * Returns the open type for the values of the attribute described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * by this {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public OpenType<?> getOpenType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Returns the default value for the attribute described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * {@code OpenMBeanAttributeInfoSupport} instance, if specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    public Object getDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        // Special case for ArrayType and TabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        // [JF] TODO: clone it so that it cannot be altered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // [JF] TODO: if we decide to support defaultValue as an array itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        // [JF] As of today (oct 2000) it is not supported so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        // defaultValue is null for arrays. Nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Returns an unmodifiable Set of legal values for the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * described by this {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * instance, if specified, or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    public Set<?> getLegalValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        // Special case for ArrayType and TabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // [JF] TODO: clone values so that they cannot be altered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // [JF] TODO: if we decide to support LegalValues as an array itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // [JF] As of today (oct 2000) it is not supported so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // legalValues is null for arrays. Nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        // Returns our legalValues Set (set was constructed unmodifiable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return (legalValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Returns the minimal value for the attribute described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * {@code OpenMBeanAttributeInfoSupport} instance, if specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public Comparable<?> getMinValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        // Note: only comparable values have a minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        // so that's not the case of arrays and tabulars (always null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        return minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * Returns the maximal value for the attribute described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * {@code OpenMBeanAttributeInfoSupport} instance, if specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public Comparable<?> getMaxValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        // Note: only comparable values have a maxValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        // so that's not the case of arrays and tabulars (always null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * default value for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public boolean hasDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        return (defaultValue != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * set of legal values for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    public boolean hasLegalValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        return (legalValues != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * minimal value for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    public boolean hasMinValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        return (minValue != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * maximal value for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public boolean hasMaxValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return (maxValue != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Tests whether {@code obj} is a valid value for the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * described by this {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @param obj the object to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @return {@code true} if {@code obj} is a valid value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * the parameter described by this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * OpenMBeanAttributeInfoSupport} instance, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public boolean isValue(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return isValue(this, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   935
    @SuppressWarnings({"unchecked", "rawtypes"})  // cast to Comparable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    static boolean isValue(OpenMBeanParameterInfo info, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if (info.hasDefaultValue() && obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            info.getOpenType().isValue(obj) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            (!info.hasLegalValues() || info.getLegalValues().contains(obj)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            (!info.hasMinValue() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            ((Comparable) info.getMinValue()).compareTo(obj) <= 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            (!info.hasMaxValue() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            ((Comparable) info.getMaxValue()).compareTo(obj) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /* ***  Commodity methods from java.lang.Object  *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Compares the specified {@code obj} parameter with this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * OpenMBeanAttributeInfoSupport} instance for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * Returns {@code true} if and only if all of the following statements are true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * <li>{@code obj} is non null,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <li>{@code obj} also implements the {@code OpenMBeanAttributeInfo} interface,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * <li>their names are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * <li>their open types are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * <li>their access properties (isReadable, isWritable and isIs) are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * <li>their default, min, max and legal values are equal.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * This ensures that this {@code equals} method works properly for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * {@code obj} parameters which are different implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * the {@code OpenMBeanAttributeInfo} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * <p>If {@code obj} also implements {@link DescriptorRead}, then its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * {@link DescriptorRead#getDescriptor() getDescriptor()} method must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * also return the same value as for this object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @param obj the object to be compared for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @return {@code true} if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        if (!(obj instanceof OpenMBeanAttributeInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            this.isReadable() == other.isReadable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            this.isWritable() == other.isWritable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            this.isIs() == other.isIs() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            equal(this, other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    static boolean equal(OpenMBeanParameterInfo x1, OpenMBeanParameterInfo x2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        if (x1 instanceof DescriptorRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            if (!(x2 instanceof DescriptorRead))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            Descriptor d1 = ((DescriptorRead) x1).getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            Descriptor d2 = ((DescriptorRead) x2).getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            if (!d1.equals(d2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        } else if (x2 instanceof DescriptorRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            x1.getName().equals(x2.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            x1.getOpenType().equals(x2.getOpenType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            (x1.hasDefaultValue() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                x1.getDefaultValue().equals(x2.getDefaultValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                !x2.hasDefaultValue()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            (x1.hasMinValue() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                x1.getMinValue().equals(x2.getMinValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                !x2.hasMinValue()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            (x1.hasMaxValue() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                x1.getMaxValue().equals(x2.getMaxValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                !x2.hasMaxValue()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            (x1.hasLegalValues() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                x1.getLegalValues().equals(x2.getLegalValues()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                !x2.hasLegalValues());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <p>Returns the hash code value for this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * OpenMBeanAttributeInfoSupport} instance.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * <p>The hash code of an {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * instance is the sum of the hash codes of all elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * information used in {@code equals} comparisons (ie: its name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * its <i>open type</i>, its default, min, max and legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * values, and its Descriptor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * <p>This ensures that {@code t1.equals(t2)} implies that {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * t1.hashCode()==t2.hashCode()} for any two {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * OpenMBeanAttributeInfoSupport} instances {@code t1} and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * t2}, as required by the general contract of the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * Object#hashCode() Object.hashCode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * <p>However, note that another instance of a class implementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * the {@code OpenMBeanAttributeInfo} interface may be equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * this {@code OpenMBeanAttributeInfoSupport} instance as defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * by {@link #equals(java.lang.Object)}, but may have a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * hash code if it is calculated differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * <p>As {@code OpenMBeanAttributeInfoSupport} instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * immutable, the hash code for this instance is calculated once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * on the first call to {@code hashCode}, and then the same value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * is returned for subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @return the hash code value for this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * OpenMBeanAttributeInfoSupport} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        // Calculate the hash code value if it has not yet been done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        // (ie 1st call to hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        if (myHashCode == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            myHashCode = hashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        // return always the same hash code for this instance (immutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        return myHashCode.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    static int hashCode(OpenMBeanParameterInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        value += info.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        value += info.getOpenType().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (info.hasDefaultValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            value += info.getDefaultValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        if (info.hasMinValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            value += info.getMinValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if (info.hasMaxValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            value += info.getMaxValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (info.hasLegalValues())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            value += info.getLegalValues().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        if (info instanceof DescriptorRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            value += ((DescriptorRead) info).getDescriptor().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Returns a string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * The string representation consists of the name of this class (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * {@code javax.management.openmbean.OpenMBeanAttributeInfoSupport}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * the string representation of the name and open type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * described parameter, the string representation of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * default, min, max and legal values and the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * representation of its descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * <p>As {@code OpenMBeanAttributeInfoSupport} instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * immutable, the string representation for this instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * calculated once, on the first call to {@code toString}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * then the same value is returned for subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return a string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        // Calculate the string value if it has not yet been done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        // (ie 1st call to toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if (myToString == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            myToString = toString(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        // return always the same string representation for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        // instance (immutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        return myToString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    static String toString(OpenMBeanParameterInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        Descriptor d = (info instanceof DescriptorRead) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            ((DescriptorRead) info).getDescriptor() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            info.getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            "(name=" + info.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            ",openType=" + info.getOpenType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            ",default=" + info.getDefaultValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            ",minValue=" + info.getMinValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            ",maxValue=" + info.getMaxValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            ",legalValues=" + info.getLegalValues() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            ((d == null) ? "" : ",descriptor=" + d) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
}