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