src/java.management/share/classes/javax/management/openmbean/CompositeDataSupport.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 47216 71c04702a3d5
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: 47216
diff changeset
     2
 * Copyright (c) 2000, 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: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.management.openmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
// java import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Collections;
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    36
import java.util.LinkedHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// jmx import
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    43
import java.util.TreeSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    48
 * The {@code CompositeDataSupport} class is the <i>open data</i> class which
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    49
 * implements the {@code CompositeData} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class CompositeDataSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    implements CompositeData, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final long serialVersionUID = 8003518976613702244L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @serial Internal representation of the mapping of item names to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * respective values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *         A {@link SortedMap} is used for faster retrieval of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 47216
diff changeset
    65
    @SuppressWarnings("serial") // Conditionally serializable
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    66
    private final SortedMap<String, Object> contents;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @serial The <i>composite type </i> of this <i>composite data</i> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    71
    private final CompositeType compositeType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    74
     * <p>Constructs a {@code CompositeDataSupport} instance with the specified
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    75
     * {@code compositeType}, whose item values
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    76
     * are specified by {@code itemValues[]}, in the same order as in
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    77
     * {@code itemNames[]}.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    78
     * As a {@code CompositeType} does not specify any order on its items,
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    79
     * the {@code itemNames[]} parameter is used
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    80
     * to specify the order in which the values are given in {@code itemValues[]}.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    81
     * The items contained in this {@code CompositeDataSupport} instance are
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    82
     * internally stored in a {@code TreeMap},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * thus sorted in ascending lexicographic order of their names, for faster
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    84
     * retrieval of individual item values.</p>
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    85
     *
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    86
     * <p>The constructor checks that all the constraints listed below for each
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * parameter are satisfied,
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    88
     * and throws the appropriate exception if they are not.</p>
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    89
     *
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    90
     * @param compositeType the <i>composite type </i> of this <i>composite
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    91
     * data</i> instance; must not be null.
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    92
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    93
     * @param itemNames {@code itemNames} must list, in any order, all the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    94
     * item names defined in {@code compositeType}; the order in which the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    95
     * names are listed, is used to match values in {@code itemValues[]}; must
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
    96
     * not be null or empty.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    97
     *
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
    98
     * @param itemValues the values of the items, listed in the same order as
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    99
     * their respective names in {@code itemNames}; each item value can be
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   100
     * null, but if it is non-null it must be a valid value for the open type
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   101
     * defined in {@code compositeType} for the corresponding item; must be of
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   102
     * the same size as {@code itemNames}; must not be null or empty.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   103
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   104
     * @throws IllegalArgumentException {@code compositeType} is null, or
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   105
     * {@code itemNames[]} or {@code itemValues[]} is null or empty, or one
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   106
     * of the elements in {@code itemNames[]} is a null or empty string, or
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   107
     * {@code itemNames[]} and {@code itemValues[]} are not of the same size.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   108
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   109
     * @throws OpenDataException {@code itemNames[]} or
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   110
     * {@code itemValues[]}'s size differs from the number of items defined in
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   111
     * {@code compositeType}, or one of the elements in {@code itemNames[]}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   112
     * does not exist as an item name defined in {@code compositeType}, or one
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   113
     * of the elements in {@code itemValues[]} is not a valid value for the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   114
     * corresponding item as defined in {@code compositeType}.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   115
     */
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   116
    public CompositeDataSupport(
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   117
            CompositeType compositeType, String[] itemNames, Object[] itemValues)
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   118
            throws OpenDataException {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   119
        this(makeMap(itemNames, itemValues), compositeType);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   120
    }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   121
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   122
    private static SortedMap<String, Object> makeMap(
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   123
            String[] itemNames, Object[] itemValues)
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   124
            throws OpenDataException {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   125
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   126
        if (itemNames == null || itemValues == null)
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   127
            throw new IllegalArgumentException("Null itemNames or itemValues");
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   128
        if (itemNames.length == 0 || itemValues.length == 0)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   129
            throw new IllegalArgumentException("Empty itemNames or itemValues");
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   130
        if (itemNames.length != itemValues.length) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   131
            throw new IllegalArgumentException(
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   132
                    "Different lengths: itemNames[" + itemNames.length +
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   133
                    "], itemValues[" + itemValues.length + "]");
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   134
        }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   135
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   136
        SortedMap<String, Object> map = new TreeMap<String, Object>();
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   137
        for (int i = 0; i < itemNames.length; i++) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   138
            String name = itemNames[i];
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   139
            if (name == null || name.equals(""))
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   140
                throw new IllegalArgumentException("Null or empty item name");
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   141
            if (map.containsKey(name))
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   142
                throw new OpenDataException("Duplicate item name " + name);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   143
            map.put(itemNames[i], itemValues[i]);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   144
        }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   145
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   146
        return map;
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   147
    }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   148
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   149
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   151
     * Constructs a {@code CompositeDataSupport} instance with the specified {@code compositeType},
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   152
     * whose item names and corresponding values
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   153
     * are given by the mappings in the map {@code items}.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   154
     * This constructor converts the keys to a string array and the values to an object array and calls
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   155
     * {@code CompositeDataSupport(javax.management.openmbean.CompositeType, java.lang.String[], java.lang.Object[])}.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   156
     *
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   157
     * @param  compositeType  the <i>composite type </i> of this <i>composite data</i> instance;
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   158
     *                        must not be null.
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   159
     * @param  items  the mappings of all the item names to their values;
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   160
     *                {@code items} must contain all the item names defined in {@code compositeType};
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   161
     *                must not be null or empty.
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   162
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   163
     * @throws IllegalArgumentException {@code compositeType} is null, or
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   164
     * {@code items} is null or empty, or one of the keys in {@code items} is a null
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   165
     * or empty string.
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   166
     * @throws OpenDataException {@code items}' size differs from the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   167
     * number of items defined in {@code compositeType}, or one of the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   168
     * keys in {@code items} does not exist as an item name defined in
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   169
     * {@code compositeType}, or one of the values in {@code items}
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   170
     * is not a valid value for the corresponding item as defined in
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   171
     * {@code compositeType}.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   172
     * @throws ArrayStoreException one or more keys in {@code items} is not of
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   173
     * the class {@code java.lang.String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   175
    public CompositeDataSupport(CompositeType compositeType,
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   176
                                Map<String,?> items)
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   177
            throws OpenDataException {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   178
        this(makeMap(items), compositeType);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   179
    }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   180
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   181
    private static SortedMap<String, Object> makeMap(Map<String, ?> items) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   182
        if (items == null || items.isEmpty())
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   183
            throw new IllegalArgumentException("Null or empty items map");
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   184
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   185
        SortedMap<String, Object> map = new TreeMap<String, Object>();
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   186
        for (Object key : items.keySet()) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   187
            if (key == null || key.equals(""))
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1627
diff changeset
   188
                throw new IllegalArgumentException("Null or empty item name");
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   189
            if (!(key instanceof String)) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   190
                throw new ArrayStoreException("Item name is not string: " + key);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   191
                // This can happen because of erasure.  The particular
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   192
                // exception is a historical artifact - an implementation
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   193
                // detail that leaked into the API.
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   194
            }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   195
            map.put((String) key, items.get(key));
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   196
        }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   197
        return map;
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   198
    }
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   199
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   200
    private CompositeDataSupport(
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   201
            SortedMap<String, Object> items, CompositeType compositeType)
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   202
            throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Check compositeType is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (compositeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new IllegalArgumentException("Argument compositeType cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // item names defined in compositeType:
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   211
        Set<String> namesFromType = compositeType.keySet();
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   212
        Set<String> namesFromItems = items.keySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   214
        // This is just a comparison, but we do it this way for a better
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   215
        // exception message.
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   216
        if (!namesFromType.equals(namesFromItems)) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   217
            Set<String> extraFromType = new TreeSet<String>(namesFromType);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   218
            extraFromType.removeAll(namesFromItems);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   219
            Set<String> extraFromItems = new TreeSet<String>(namesFromItems);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   220
            extraFromItems.removeAll(namesFromType);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   221
            if (!extraFromType.isEmpty() || !extraFromItems.isEmpty()) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   222
                throw new OpenDataException(
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   223
                        "Item names do not match CompositeType: " +
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   224
                        "names in items but not in CompositeType: " + extraFromItems +
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   225
                        "; names in CompositeType but not in items: " + extraFromType);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   226
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   229
        // Check each value, if not null, is of the open type defined for the
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   230
        // corresponding item
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   231
        for (String name : namesFromType) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   232
            Object value = items.get(name);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   233
            if (value != null) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   234
                OpenType<?> itemType = compositeType.getType(name);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   235
                if (!itemType.isValue(value)) {
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   236
                    throw new OpenDataException(
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   237
                            "Argument value of wrong type for item " + name +
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   238
                            ": value " + value + ", type " + itemType);
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   239
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // Initialize internal fields: compositeType and contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        this.compositeType = compositeType;
1571
421ef5172dd3 6336968: Methods to convert AttributeList to/from Map
emcmanus
parents: 1247
diff changeset
   246
        this.contents = items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Returns the <i>composite type </i> of this <i>composite data</i> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public CompositeType getCompositeType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return compositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   258
     * Returns the value of the item whose name is {@code key}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   260
     * @throws IllegalArgumentException  if {@code key} is a null or empty String.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   262
     * @throws InvalidKeyException  if {@code key} is not an existing item name for
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   263
     * this {@code CompositeData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public Object get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if ( (key == null) || (key.trim().equals("")) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new IllegalArgumentException("Argument key cannot be a null or empty String.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if ( ! contents.containsKey(key.trim())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new InvalidKeyException("Argument key=\""+ key.trim() +"\" is not an existing item name for this CompositeData instance.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return contents.get(key.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns an array of the values of the items whose names are specified by
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   278
     * {@code keys}, in the same order as {@code keys}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   280
     * @throws IllegalArgumentException  if an element in {@code keys} is a null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * or empty String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   283
     * @throws InvalidKeyException  if an element in {@code keys} is not an existing
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   284
     * item name for this {@code CompositeData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public Object[] getAll(String[] keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if ( (keys == null) || (keys.length == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return new Object[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        Object[] results = new Object[keys.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        for (int i=0; i<keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            results[i] = this.get(keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   299
     * Returns {@code true} if and only if this {@code CompositeData} instance contains
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   300
     * an item whose name is {@code key}.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   301
     * If {@code key} is a null or empty String, this method simply returns false.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public boolean containsKey(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if ( (key == null) || (key.trim().equals("")) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return contents.containsKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   312
     * Returns {@code true} if and only if this {@code CompositeData} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * contains an item
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   314
     * whose value is {@code value}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return contents.containsValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns an unmodifiable Collection view of the item values contained in this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   323
     * {@code CompositeData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * The returned collection's iterator will return the values in the ascending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * lexicographic order of the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * item names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public Collection<?> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return Collections.unmodifiableCollection(contents.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Compares the specified <var>obj</var> parameter with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <code>CompositeDataSupport</code> instance for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   337
     * Returns {@code true} if and only if all of the following statements are true:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <li><var>obj</var> is non null,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <li>their composite types are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <li>their contents, i.e. (name, value) pairs are equal. If a value contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * the content is an array, the value comparison is done as if by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * the {@link java.util.Arrays#deepEquals(Object[], Object[]) deepEquals} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * for arrays of object reference types or the appropriate overloading of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * {@code Arrays.equals(e1,e2)} for arrays of primitive types</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   349
     * This ensures that this {@code equals} method works properly for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <var>obj</var> parameters which are different implementations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <code>CompositeData</code> interface, with the restrictions mentioned in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * {@link java.util.Collection#equals(Object) equals}
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   353
     * method of the {@code java.util.Collection} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param  obj  the object to be compared for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <code>CompositeDataSupport</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return  <code>true</code> if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <code>CompositeDataSupport</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
1020
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   360
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // if obj is not a CompositeData, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (!(obj instanceof CompositeData)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        CompositeData other = (CompositeData) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // their compositeType should be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (!this.getCompositeType().equals(other.getCompositeType()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (contents.size() != other.values().size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        for (Map.Entry<String,Object> entry : contents.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            Object e1 = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            Object e2 = other.get(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            boolean eq = e1.getClass().isArray() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                Arrays.deepEquals(new Object[] {e1}, new Object[] {e2}) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                e1.equals(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // All tests for equality were successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Returns the hash code value for this <code>CompositeDataSupport</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * of all elements of information used in <code>equals</code> comparisons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * (ie: its <i>composite type</i> and all the item values).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * for any two <code>CompositeDataSupport</code> instances <code>t1</code> and <code>t2</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * as required by the general contract of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * {@link Object#hashCode() Object.hashCode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Each item value's hash code is added to the returned hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * If an item value is an array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * its hash code is obtained as if by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * {@link java.util.Arrays#deepHashCode(Object[]) deepHashCode} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * for arrays of object reference types or the appropriate overloading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * of {@code Arrays.hashCode(e)} for arrays of primitive types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return the hash code value for this <code>CompositeDataSupport</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
1020
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   425
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        int hashcode = compositeType.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        for (Object o : contents.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (o instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                hashcode += Arrays.deepHashCode((Object[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            else if (o instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                hashcode += Arrays.hashCode((byte[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            else if (o instanceof short[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                hashcode += Arrays.hashCode((short[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            else if (o instanceof int[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                hashcode += Arrays.hashCode((int[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            else if (o instanceof long[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                hashcode += Arrays.hashCode((long[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            else if (o instanceof char[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                hashcode += Arrays.hashCode((char[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            else if (o instanceof float[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                hashcode += Arrays.hashCode((float[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            else if (o instanceof double[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                hashcode += Arrays.hashCode((double[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            else if (o instanceof boolean[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                hashcode += Arrays.hashCode((boolean[]) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            else if (o != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                hashcode += o.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return hashcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns a string representation of this <code>CompositeDataSupport</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * The string representation consists of the name of this class (ie <code>javax.management.openmbean.CompositeDataSupport</code>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * the string representation of the composite type of this instance, and the string representation of the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * (ie list the itemName=itemValue mappings).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @return  a string representation of this <code>CompositeDataSupport</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
1020
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   464
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            .append(this.getClass().getName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            .append("(compositeType=")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            .append(compositeType.toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            .append(",contents=")
1020
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   471
            .append(contentString())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            .append(")")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            .toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
1020
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   476
    private String contentString() {
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   477
        StringBuilder sb = new StringBuilder("{");
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   478
        String sep = "";
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   479
        for (Map.Entry<String, Object> entry : contents.entrySet()) {
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   480
            sb.append(sep).append(entry.getKey()).append("=");
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   481
            String s = Arrays.deepToString(new Object[] {entry.getValue()});
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   482
            sb.append(s.substring(1, s.length() - 1));
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   483
            sep = ", ";
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   484
        }
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   485
        sb.append("}");
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   486
        return sb.toString();
e7a021898dff 6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents: 2
diff changeset
   487
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
}