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