src/java.management/share/classes/javax/management/ImmutableDescriptor.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
     2
 * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 2
diff changeset
    28
import com.sun.jmx.mbeanserver.Util;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * An immutable descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ImmutableDescriptor implements Descriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final long serialVersionUID = 8853308591080540165L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * The names of the fields in this ImmutableDescriptor with their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * original case.  The names must be in alphabetical order as determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * by {@link String#CASE_INSENSITIVE_ORDER}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * The values of the fields in this ImmutableDescriptor.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * elements in this array match the corresponding elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * {@code names} array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
    55
    @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final Object[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private transient int hashCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * An empty descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final ImmutableDescriptor EMPTY_DESCRIPTOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            new ImmutableDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Construct a descriptor containing the given fields and values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
    69
     * @param fieldNames the field names
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
    70
     * @param fieldValues the field values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @throws IllegalArgumentException if either array is null, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * if the arrays have different sizes, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * if a field name is null or empty, or if the same field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * appears more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public ImmutableDescriptor(String[] fieldNames, Object[] fieldValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this(makeMap(fieldNames, fieldValues));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Construct a descriptor containing the given fields.  Each String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * must be of the form {@code fieldName=fieldValue}.  The field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * ends at the first {@code =} character; for example if the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * is {@code a=b=c} then the field name is {@code a} and its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * is {@code b=c}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
    87
     * @param fields the field names
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @throws IllegalArgumentException if the parameter is null, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * if a field name is empty, or if the same field name appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * more than once, or if one of the strings does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * an {@code =} character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public ImmutableDescriptor(String... fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this(makeMap(fields));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p>Construct a descriptor where the names and values of the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * are the keys and values of the given Map.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
   101
     * @param fields the field names and values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws IllegalArgumentException if the parameter is null, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * if a field name is null or empty, or if the same field name appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * more than once (which can happen because field names are not case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public ImmutableDescriptor(Map<String, ?> fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (fields == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new IllegalArgumentException("Null Map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        SortedMap<String, Object> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (Map.Entry<String, ?> entry : fields.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            String name = entry.getKey();
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   114
            if (name == null || name.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new IllegalArgumentException("Empty or null field name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (map.containsKey(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new IllegalArgumentException("Duplicate name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            map.put(name, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int size = map.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.names = map.keySet().toArray(new String[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.values = map.values().toArray(new Object[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This method can replace a deserialized instance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * class with another instance.  For example, it might replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * a deserialized empty ImmutableDescriptor with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * {@link #EMPTY_DESCRIPTOR}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the replacement object, which may be {@code this}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws InvalidObjectException if the read object has invalid fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private Object readResolve() throws InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        boolean bad = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (names == null || values == null || names.length != values.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            bad = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (!bad) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 900
diff changeset
   141
            if (names.length == 0 && getClass() == ImmutableDescriptor.class)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 900
diff changeset
   142
                return EMPTY_DESCRIPTOR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            final Comparator<String> compare = String.CASE_INSENSITIVE_ORDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            String lastName = ""; // also catches illegal null name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (names[i] == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        compare.compare(lastName, names[i]) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    bad = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                lastName = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (bad)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new InvalidObjectException("Bad names or values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static SortedMap<String, ?> makeMap(String[] fieldNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                                Object[] fieldValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (fieldNames == null || fieldValues == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new IllegalArgumentException("Null array parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (fieldNames.length != fieldValues.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException("Different size arrays");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        SortedMap<String, Object> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (int i = 0; i < fieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            String name = fieldNames[i];
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   170
            if (name == null || name.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                throw new IllegalArgumentException("Empty or null field name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Object old = map.put(name, fieldValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new IllegalArgumentException("Duplicate field name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                   name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static SortedMap<String, ?> makeMap(String[] fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (fields == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IllegalArgumentException("Null fields parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        String[] fieldNames = new String[fields.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String[] fieldValues = new String[fields.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            String field = fields[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            int eq = field.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (eq < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new IllegalArgumentException("Missing = character: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                                   field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            fieldNames[i] = field.substring(0, eq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // makeMap will catch the case where the name is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            fieldValues[i] = field.substring(eq + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return makeMap(fieldNames, fieldValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>Return an {@code ImmutableDescriptor} whose contents are the union of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * the given descriptors.  Every field name that appears in any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the descriptors will appear in the result with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * value that it has when the method is called.  Subsequent changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * to any of the descriptors do not affect the ImmutableDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * returned here.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>In the simplest case, there is only one descriptor and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * returned {@code ImmutableDescriptor} is a copy of its fields at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * time this method is called:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Descriptor d = something();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * ImmutableDescriptor copy = ImmutableDescriptor.union(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param descriptors the descriptors to be combined.  Any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * descriptors can be null, in which case it is skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return an {@code ImmutableDescriptor} that is the union of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * descriptors.  The returned object may be identical to one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * input descriptors if it is an ImmutableDescriptor that contains all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * the required fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws IllegalArgumentException if two Descriptors contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * same field name with different associated values.  Primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * values are considered the same if they are of the same type with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * the same elements.  Object array values are considered the same if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * {@link Arrays#deepEquals(Object[],Object[])} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public static ImmutableDescriptor union(Descriptor... descriptors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Optimize the case where exactly one Descriptor is non-Empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // and it is immutable - we can just return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int index = findNonEmpty(descriptors, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (descriptors[index] instanceof ImmutableDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                && findNonEmpty(descriptors, index + 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return (ImmutableDescriptor) descriptors[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Map<String, Object> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        ImmutableDescriptor biggestImmutable = EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (Descriptor d : descriptors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (d instanceof ImmutableDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    ImmutableDescriptor id = (ImmutableDescriptor) d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    names = id.names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    if (id.getClass() == ImmutableDescriptor.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            && names.length > biggestImmutable.names.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        biggestImmutable = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    names = d.getFieldNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                for (String n : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    Object v = d.getFieldValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    Object old = map.put(n, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        boolean equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        if (old.getClass().isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            equal = Arrays.deepEquals(new Object[] {old},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                                      new Object[] {v});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                            equal = old.equals(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        if (!equal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                "Inconsistent values for descriptor field " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                n + ": " + old + " :: " + v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (biggestImmutable.names.length == map.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return biggestImmutable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return new ImmutableDescriptor(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private static boolean isEmpty(Descriptor d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (d == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        else if (d instanceof ImmutableDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return ((ImmutableDescriptor) d).names.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return (d.getFieldNames().length == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private static int findNonEmpty(Descriptor[] ds, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        for (int i = start; i < ds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (!isEmpty(ds[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private int fieldIndex(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return Arrays.binarySearch(names, name, String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public final Object getFieldValue(String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        checkIllegalFieldName(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int i = fieldIndex(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        Object v = values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (v == null || !v.getClass().isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (v instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return ((Object[]) v).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // clone the primitive array, could use an 8-way if/else here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int len = Array.getLength(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Object a = Array.newInstance(v.getClass().getComponentType(), len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        System.arraycopy(v, 0, a, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public final String[] getFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        String[] result = new String[names.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        for (int i = 0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            Object value = values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            else if (!(value instanceof String))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                value = "(" + value + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            result[i] = names[i] + "=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public final Object[] getFieldValues(String... fieldNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (fieldNames == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return values.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Object[] result = new Object[fieldNames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        for (int i = 0; i < fieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            String name = fieldNames[i];
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   337
            if (name != null && !name.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                result[i] = getFieldValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public final String[] getFieldNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return names.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Compares this descriptor to the given object.  The objects are equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * the given object is also a Descriptor, and if the two Descriptors have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * the same field names (possibly differing in case) and the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * associated values.  The respective values for a field in the two
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   352
     * Descriptors are equal if the following conditions hold:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <li>If one value is null then the other must be too.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <li>If one value is a primitive array then the other must be a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * array of the same type with the same elements.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <li>If one value is an object array then the other must be too and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * {@link Arrays#deepEquals(Object[],Object[])} must return true.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <li>Otherwise {@link Object#equals(Object)} must return true.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param o the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return {@code true} if the objects are the same; {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    // Note: this Javadoc is copied from javax.management.Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    //       due to 6369229.
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 2
diff changeset
   371
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (!(o instanceof Descriptor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        String[] onames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (o instanceof ImmutableDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            onames = ((ImmutableDescriptor) o).names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            onames = ((Descriptor) o).getFieldNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            Arrays.sort(onames, String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (names.length != onames.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (!names[i].equalsIgnoreCase(onames[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        Object[] ovalues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (o instanceof ImmutableDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            ovalues = ((ImmutableDescriptor) o).values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            ovalues = ((Descriptor) o).getFieldValues(onames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return Arrays.deepEquals(values, ovalues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <p>Returns the hash code value for this descriptor.  The hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * code is computed as the sum of the hash codes for each field in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the descriptor.  The hash code of a field with name {@code n}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * and value {@code v} is {@code n.toLowerCase().hashCode() ^ h}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Here {@code h} is the hash code of {@code v}, computed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <li>If {@code v} is null then {@code h} is 0.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <li>If {@code v} is a primitive array then {@code h} is computed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * the appropriate overloading of {@code java.util.Arrays.hashCode}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <li>If {@code v} is an object array then {@code h} is computed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * {@link Arrays#deepHashCode(Object[])}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <li>Otherwise {@code h} is {@code v.hashCode()}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return A hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    // Note: this Javadoc is copied from javax.management.Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    //       due to 6369229.
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 2
diff changeset
   420
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (hashCode == -1) {
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 2
diff changeset
   423
            hashCode = Util.hashCode(names, values);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 2
diff changeset
   428
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        StringBuilder sb = new StringBuilder("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            sb.append(names[i]).append("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            Object v = values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (v != null && v.getClass().isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                String s = Arrays.deepToString(new Object[] {v});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                s = s.substring(1, s.length() - 1); // remove [...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                v = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            sb.append(String.valueOf(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return sb.append("}").toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns true if all of the fields have legal values given their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * names.  This method always returns true, but a subclass can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * override it to return false when appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @return true if the values are legal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception RuntimeOperationsException if the validity checking fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * The method returns false if the descriptor is not valid, but throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * this exception if the attempt to determine validity fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <p>Returns a descriptor which is equal to this descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Changes to the returned descriptor will have no effect on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * descriptor, and vice versa.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p>This method returns the object on which it is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * A subclass can override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * to return another object provided the contract is respected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @exception RuntimeOperationsException for illegal value for field Names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * or field Values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * If the descriptor construction fails for any reason, this exception will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 2
diff changeset
   475
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public Descriptor clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * This operation is unsupported since this class is immutable.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * this call would change a mutable descriptor with the same contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * then a {@link RuntimeOperationsException} wrapping an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * {@link UnsupportedOperationException} is thrown.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * the behavior is the same as it would be for a mutable descriptor:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * either an exception is thrown because of illegal parameters, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * there is no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public final void setFields(String[] fieldNames, Object[] fieldValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (fieldNames == null || fieldValues == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            illegal("Null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (fieldNames.length != fieldValues.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            illegal("Different array sizes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        for (int i = 0; i < fieldNames.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            checkIllegalFieldName(fieldNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        for (int i = 0; i < fieldNames.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            setField(fieldNames[i], fieldValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * This operation is unsupported since this class is immutable.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * this call would change a mutable descriptor with the same contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * then a {@link RuntimeOperationsException} wrapping an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * {@link UnsupportedOperationException} is thrown.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * the behavior is the same as it would be for a mutable descriptor:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * either an exception is thrown because of illegal parameters, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * there is no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public final void setField(String fieldName, Object fieldValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        checkIllegalFieldName(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int i = fieldIndex(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            unsupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        Object value = values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if ((value == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                (fieldValue != null) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                !value.equals(fieldValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            unsupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Removes a field from the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param fieldName String name of the field to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * If the field name is illegal or the field is not found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * no exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @exception RuntimeOperationsException if a field of the given name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * exists and the descriptor is immutable.  The wrapped exception will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * be an {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public final void removeField(String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (fieldName != null && fieldIndex(fieldName) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            unsupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    static Descriptor nonNullDescriptor(Descriptor d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (d == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    private static void checkIllegalFieldName(String name) {
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   547
        if (name == null || name.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            illegal("Null or empty field name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private static void unsupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        UnsupportedOperationException uoe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            new UnsupportedOperationException("Descriptor is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        throw new RuntimeOperationsException(uoe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private static void illegal(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        IllegalArgumentException iae = new IllegalArgumentException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        throw new RuntimeOperationsException(iae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
}