src/java.management/share/classes/javax/management/openmbean/TabularDataSupport.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 52427 3c6aa484536c
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: 52427
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: 1510
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: 1510
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: 1510
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
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
//
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
    32
import com.sun.jmx.mbeanserver.GetPropertyAction;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
    33
import com.sun.jmx.mbeanserver.Util;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.Serializable;
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
    37
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Iterator;
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
    44
import java.util.LinkedHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Set;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49794
diff changeset
    48
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
// jmx import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
    55
 * The {@code TabularDataSupport} class is the <i>open data</i>
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
    56
 * class which implements the {@code TabularData}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
    57
 * and the {@code Map} interfaces, and which is internally based on a hash map data structure.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/* It would make much more sense to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   Map<List<?>,CompositeData> here, but unfortunately we cannot for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   compatibility reasons.  If we did that, then we would have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   define e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   CompositeData remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
   instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   Object remove(Object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
   That would mean that if any existing code subclassed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   TabularDataSupport and overrode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   Object remove(Object),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   it would (a) no longer compile and (b) not actually override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
   CompositeData remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   in binaries compiled before the change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public class TabularDataSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    implements TabularData, Map<Object,Object>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
               Cloneable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final long serialVersionUID = 5720150593236309827L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @serial This tabular data instance's contents: a {@link HashMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
    88
    // field cannot be final because of clone method
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52427
diff changeset
    89
    @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Map<Object,CompositeData> dataMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @serial This tabular data instance's tabular type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
    95
    private final TabularType tabularType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The array of item names that define the index used for rows (convenience field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private transient String[] indexNamesArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* *** Constructors *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   108
     * Creates an empty {@code TabularDataSupport} instance
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   109
     * whose open-type is <var>tabularType</var>,
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   110
     * and whose underlying {@code HashMap} has a default
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   111
     * initial capacity (101) and default load factor (0.75).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   113
     * This constructor simply calls {@code this(tabularType, 101, 0.75f);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   115
     * @param  tabularType the <i>tabular type</i> describing this
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   116
     *         {@code TabularData} instance; cannot be null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws IllegalArgumentException  if the tabular type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public TabularDataSupport(TabularType tabularType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   122
        this(tabularType, 16, 0.75f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   126
     * Creates an empty {@code TabularDataSupport} instance whose open-type is <var>tabularType</var>,
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   127
     * and whose underlying {@code HashMap} has the specified initial capacity and load factor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   129
     * @param  tabularType               the <i>tabular type</i> describing this {@code TabularData} instance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *                           cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param  initialCapacity   the initial capacity of the HashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param  loadFactor        the load factor of the HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws IllegalArgumentException  if the initial capacity is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                                   or the load factor is nonpositive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *                                   or the tabular type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public TabularDataSupport(TabularType tabularType, int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Check tabularType is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (tabularType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException("Argument tabularType cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Initialize this.tabularType (and indexNamesArray for convenience)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.tabularType = tabularType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        List<String> tmpNames = tabularType.getIndexNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.indexNamesArray = tmpNames.toArray(new String[tmpNames.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   154
        // Since LinkedHashMap was introduced in SE 1.4, it's conceivable even
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   155
        // if very unlikely that we might be the server of a 1.3 client.  In
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   156
        // that case you'll need to set this property.  See CR 6334663.
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   157
        String useHashMapProp = AccessController.doPrivileged(
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   158
                new GetPropertyAction("jmx.tabular.data.hash.map"));
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   159
        boolean useHashMap = "true".equalsIgnoreCase(useHashMapProp);
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   160
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Construct the empty contents HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        //
1018
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   163
        this.dataMap = useHashMap ?
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   164
            new HashMap<Object,CompositeData>(initialCapacity, loadFactor) :
9f07e65e9653 6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents: 2
diff changeset
   165
            new LinkedHashMap<Object, CompositeData>(initialCapacity, loadFactor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /* *** TabularData specific information methods *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   175
     * Returns the <i>tabular type</i> describing this {@code TabularData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public TabularType getTabularType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return tabularType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   183
     * Calculates the index that would be used in this {@code TabularData} instance to refer
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   184
     * to the specified composite data <var>value</var> parameter if it were added to this instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * This method checks for the type validity of the specified <var>value</var>,
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   186
     * but does not check if the calculated index is already used
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   187
     * to refer to a value in this {@code TabularData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param  value                      the composite data value whose index in this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   190
     *                                    {@code TabularData} instance is to be calculated;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *                                    must be of the same composite type as this instance's row type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *                                    must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   194
     * @return the index that the specified <var>value</var> would have in this {@code TabularData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   196
     * @throws NullPointerException       if <var>value</var> is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   198
     * @throws InvalidOpenTypeException   if <var>value</var> does not conform to this {@code TabularData} instance's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *                                    row type definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public Object[] calculateIndex(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // Check value is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        checkValueType(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // Return its calculated index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return internalCalculateIndex(value).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /* *** Content information query methods *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   219
     * Returns {@code true} if and only if this {@code TabularData} instance contains a {@code CompositeData} value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * (ie a row) whose index is the specified <var>key</var>. If <var>key</var> cannot be cast to a one dimension array
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   221
     * of Object instances, this method simply returns {@code false}; otherwise it returns the result of the call to
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   222
     * {@code this.containsKey((Object[]) key)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   224
     * @param  key  the index value whose presence in this {@code TabularData} instance is to be tested.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   226
     * @return  {@code true} if this {@code TabularData} indexes a row value with the specified key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // if key is not an array of Object instances, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Object[] k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            k = (Object[]) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return  this.containsKey(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   243
     * Returns {@code true} if and only if this {@code TabularData} instance contains a {@code CompositeData} value
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   244
     * (ie a row) whose index is the specified <var>key</var>. If <var>key</var> is {@code null} or does not conform to
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   245
     * this {@code TabularData} instance's {@code TabularType} definition, this method simply returns {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   247
     * @param  key  the index value whose presence in this {@code TabularData} instance is to be tested.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   249
     * @return  {@code true} if this {@code TabularData} indexes a row value with the specified key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public boolean containsKey(Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return  ( key == null ? false : dataMap.containsKey(Arrays.asList(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   257
     * Returns {@code true} if and only if this {@code TabularData} instance contains the specified
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   258
     * {@code CompositeData} value. If <var>value</var> is {@code null} or does not conform to
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   259
     * this {@code TabularData} instance's row type definition, this method simply returns {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   261
     * @param  value  the row value whose presence in this {@code TabularData} instance is to be tested.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   263
     * @return  {@code true} if this {@code TabularData} instance contains the specified row value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public boolean containsValue(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return dataMap.containsValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   271
     * Returns {@code true} if and only if this {@code TabularData} instance contains the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   274
     * @param  value  the row value whose presence in this {@code TabularData} instance is to be tested.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   276
     * @return  {@code true} if this {@code TabularData} instance contains the specified row value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return dataMap.containsValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   284
     * This method simply calls {@code get((Object[]) key)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   286
     * @throws NullPointerException  if the <var>key</var> is {@code null}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   287
     * @throws ClassCastException    if the <var>key</var> is not of the type {@code Object[]}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   288
     * @throws InvalidKeyException   if the <var>key</var> does not conform
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   289
     *                               to this {@code TabularData} instance's
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   290
     *                               {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return get((Object[]) key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   298
     * Returns the {@code CompositeData} value whose index is
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   299
     * <var>key</var>, or {@code null} if there is no value mapping
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   300
     * to <var>key</var>, in this {@code TabularData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param key the index of the value to get in this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   303
     * {@code TabularData} instance; must be valid with this
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   304
     * {@code TabularData} instance's row type definition; must not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return the value corresponding to <var>key</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   309
     * @throws NullPointerException  if the <var>key</var> is {@code null}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   310
     * @throws InvalidKeyException   if the <var>key</var> does not conform
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   311
     *                               to this {@code TabularData} instance's
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   312
     *                               {@code TabularType} type definition.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public CompositeData get(Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Check key is not null and valid with tabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // (throws NullPointerException, InvalidKeyException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        checkKeyType(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // Return the mapping stored in the parent HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return dataMap.get(Arrays.asList(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /* *** Content modification operations (one element at a time) *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   333
     * This method simply calls {@code put((CompositeData) value)} and
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   334
     * therefore ignores its <var>key</var> parameter which can be {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param key an ignored parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param value the {@link CompositeData} to put.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return the value which is put
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   341
     * @throws NullPointerException  if the <var>value</var> is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws ClassCastException if the <var>value</var> is not of
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   343
     * the type {@code CompositeData}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @throws InvalidOpenTypeException if the <var>value</var> does
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   345
     * not conform to this {@code TabularData} instance's
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   346
     * {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws KeyAlreadyExistsException if the key for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <var>value</var> parameter, calculated according to this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   349
     * {@code TabularData} instance's {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * already maps to an existing value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public Object put(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        internalPut((CompositeData) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return value; // should be return internalPut(...); (5090566)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public void put(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        internalPut(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private CompositeData internalPut(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // Check value is not null, value's type is the same as this instance's row type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // and calculate the value's index according to this instance's tabularType and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // check it is not already used for a mapping in the parent HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        List<?> index = checkValueAndIndex(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // store the (key, value) mapping in the dataMap HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return dataMap.put(index, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   374
     * This method simply calls {@code remove((Object[]) key)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   376
     * @param key an {@code Object[]} representing the key to remove.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   378
     * @return previous value associated with specified key, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *         if there was no mapping for key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   381
     * @throws NullPointerException  if the <var>key</var> is {@code null}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   382
     * @throws ClassCastException    if the <var>key</var> is not of the type {@code Object[]}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   383
     * @throws InvalidKeyException   if the <var>key</var> does not conform to this {@code TabularData} instance's
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   384
     *                               {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public Object remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return remove((Object[]) key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   392
     * Removes the {@code CompositeData} value whose index is <var>key</var> from this {@code TabularData} instance,
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   393
     * and returns the removed value, or returns {@code null} if there is no value whose index is <var>key</var>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   395
     * @param  key  the index of the value to get in this {@code TabularData} instance;
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   396
     *              must be valid with this {@code TabularData} instance's row type definition;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *              must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   399
     * @return previous value associated with specified key, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *         if there was no mapping for key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   402
     * @throws NullPointerException  if the <var>key</var> is {@code null}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   403
     * @throws InvalidKeyException   if the <var>key</var> does not conform to this {@code TabularData} instance's
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   404
     *                               {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public CompositeData remove(Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // Check key is not null and valid with tabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // (throws NullPointerException, InvalidKeyException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        checkKeyType(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Removes the (key, value) mapping in the parent HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return dataMap.remove(Arrays.asList(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /* ***   Content modification bulk operations   *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Add all the values contained in the specified map <var>t</var>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   425
     * to this {@code TabularData} instance.  This method converts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * the collection of values contained in this map into an array of
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   427
     * {@code CompositeData} values, if possible, and then call the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   428
     * method {@code putAll(CompositeData[])}. Note that the keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * used in the specified map <var>t</var> are ignored. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * allows, for example to add the content of another
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   431
     * {@code TabularData} instance with the same row type (but
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * possibly different index names) into this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param t the map whose values are to be added as new rows to
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   435
     * this {@code TabularData} instance; if <var>t</var> is
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   436
     * {@code null} or empty, this method returns without doing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @throws NullPointerException if a value in <var>t</var> is
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   440
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @throws ClassCastException if a value in <var>t</var> is not an
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   442
     * instance of {@code CompositeData}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @throws InvalidOpenTypeException if a value in <var>t</var>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   444
     * does not conform to this {@code TabularData} instance's row
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * type definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @throws KeyAlreadyExistsException if the index for a value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * <var>t</var>, calculated according to this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   448
     * {@code TabularData} instance's {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * already maps to an existing value in this instance, or two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * values in <var>t</var> have the same index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public void putAll(Map<?,?> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // if t is null or empty, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if ( (t == null) || (t.size() == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   460
        // Convert the values in t into an array of {@code CompositeData}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        CompositeData[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            values =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                t.values().toArray(new CompositeData[t.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        } catch (java.lang.ArrayStoreException e) {
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   467
            throw new ClassCastException("Map argument t contains values which are not instances of {@code CompositeData}");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // Add the array of values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        putAll(values);
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
     * Add all the elements in <var>values</var> to this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   477
     * {@code TabularData} instance.  If any element in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <var>values</var> does not satisfy the constraints defined in
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   479
     * {@link #put(CompositeData) put}, or if any two
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * elements in <var>values</var> have the same index calculated
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   481
     * according to this {@code TabularData} instance's
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   482
     * {@code TabularType} definition, then an exception describing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * the failure is thrown and no element of <var>values</var> is
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   484
     * added, thus leaving this {@code TabularData} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param values the array of composite data values to be added as
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   488
     * new rows to this {@code TabularData} instance; if
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   489
     * <var>values</var> is {@code null} or empty, this method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * returns without doing anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @throws NullPointerException if an element of <var>values</var>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   493
     * is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @throws InvalidOpenTypeException if an element of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * <var>values</var> does not conform to this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   496
     * {@code TabularData} instance's row type definition (ie its
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   497
     * {@code TabularType} definition)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @throws KeyAlreadyExistsException if the index for an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * of <var>values</var>, calculated according to this
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   500
     * {@code TabularData} instance's {@code TabularType} definition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * already maps to an existing value in this instance, or two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * elements of <var>values</var> have the same index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public void putAll(CompositeData[] values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        // if values is null or empty, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if ( (values == null) || (values.length == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // create the list of indexes corresponding to each value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        List<List<?>> indexes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            new ArrayList<List<?>>(values.length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Check all elements in values and build index list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        List<?> index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        for (int i=0; i<values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            // check value and calculate index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            index = checkValueAndIndex(values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            // check index is different of those previously calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (indexes.contains(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                throw new KeyAlreadyExistsException("Argument elements values["+ i +"] and values["+ indexes.indexOf(index) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                                    "] have the same indexes, "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                                    "calculated according to this TabularData instance's tabularType.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // add to index list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            indexes.add(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        // store all (index, value) mappings in the dataMap HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        for (int i=0; i<values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            dataMap.put(indexes.get(i), values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   540
     * Removes all rows from this {@code TabularDataSupport} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        dataMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /* ***  Informational methods from java.util.Map  *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   552
     * Returns the number of rows in this {@code TabularDataSupport} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   554
     * @return the number of rows in this {@code TabularDataSupport} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return dataMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   562
     * Returns {@code true} if this {@code TabularDataSupport} instance contains no rows.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   564
     * @return {@code true} if this {@code TabularDataSupport} instance contains no rows.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return (this.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /* ***  Collection views from java.util.Map  *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Returns a set view of the keys contained in the underlying map of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * {@code TabularDataSupport} instance used to index the rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Each key contained in this {@code Set} is an unmodifiable {@code List<?>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * so the returned set view is a {@code Set<List<?>>} but is declared as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * {@code Set<Object>} for compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * The set is backed by the underlying map of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * {@code TabularDataSupport} instance, so changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * {@code TabularDataSupport} instance are reflected in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * set, and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * The set supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * row from this {@code TabularDataSupport} instance, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * {@link Iterator#remove}, {@link Set#remove}, {@link Set#removeAll},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * {@link Set#retainAll}, and {@link Set#clear} operations. It does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *  not support the {@link Set#add} or {@link Set#addAll} operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return a set view ({@code Set<List<?>>}) of the keys used to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * the rows of this {@code TabularDataSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public Set<Object> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return dataMap.keySet() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Returns a collection view of the rows contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * {@code TabularDataSupport} instance. The returned {@code Collection}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * is a {@code Collection<CompositeData>} but is declared as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * {@code Collection<Object>} for compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * The returned collection can be used to iterate over the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * The collection is backed by the underlying map, so changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * {@code TabularDataSupport} instance are reflected in the collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * The collection supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * index to row mapping from this {@code TabularDataSupport} instance, via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * the {@link Iterator#remove}, {@link Collection#remove},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * {@link Collection#removeAll}, {@link Collection#retainAll},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * and {@link Collection#clear} operations. It does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * the {@link Collection#add} or {@link Collection#addAll} operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @return a collection view ({@code Collection<CompositeData>}) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * the values contained in this {@code TabularDataSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    @SuppressWarnings("unchecked")  // historical confusion about the return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public Collection<Object> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   623
        return Util.cast(dataMap.values());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Returns a collection view of the index to row mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * contained in this {@code TabularDataSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Each element in the returned collection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * a {@code Map.Entry<List<?>,CompositeData>} but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * is declared as a {@code Map.Entry<Object,Object>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * for compatibility reasons. Each of the map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * keys is an unmodifiable {@code List<?>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * The collection is backed by the underlying map of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * {@code TabularDataSupport} instance, so changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * {@code TabularDataSupport} instance are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * the collection, and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * The collection supports element removal, which removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * {@link Iterator#remove}, {@link Collection#remove},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * {@link Collection#removeAll}, {@link Collection#retainAll},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * and {@link Collection#clear} operations. It does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * the {@link Collection#add} or {@link Collection#addAll}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <b>IMPORTANT NOTICE</b>: Do not use the {@code setValue} method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * {@code Map.Entry} elements contained in the returned collection view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Doing so would corrupt the index to row mappings contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * {@code TabularDataSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @return a collection view ({@code Set<Map.Entry<List<?>,CompositeData>>})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @see java.util.Map.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    @SuppressWarnings("unchecked")  // historical confusion about the return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public Set<Map.Entry<Object,Object>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   659
        return Util.cast(dataMap.entrySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /* ***  Commodity methods from java.lang.Object  *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   667
     * Returns a clone of this {@code TabularDataSupport} instance:
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   668
     * the clone is obtained by calling {@code super.clone()}, and then cloning the underlying map.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   669
     * Only a shallow clone of the underlying map is made, i.e.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   670
     * no cloning of the indexes and row values is made as they are immutable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /* We cannot use covariance here and return TabularDataSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
       because this would fail with existing code that subclassed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
       TabularDataSupport and overrode Object clone().  It would not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
       override the new clone().  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            TabularDataSupport c = (TabularDataSupport) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            c.dataMap = new HashMap<Object,CompositeData>(c.dataMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   683
            throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   689
     * Compares the specified <var>obj</var> parameter with this {@code TabularDataSupport} instance for equality.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   691
     * Returns {@code true} if and only if all of the following statements are true:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * <li><var>obj</var> is non null,</li>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   694
     * <li><var>obj</var> also implements the {@code TabularData} interface,</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * <li>their tabular types are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * <li>their contents (ie all CompositeData values) are equal.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * </ul>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   698
     * This ensures that this {@code equals} method works properly for <var>obj</var> parameters which are
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   699
     * different implementations of the {@code TabularData} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * <br>&nbsp;
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   701
     * @param  obj  the object to be compared for equality with this {@code TabularDataSupport} instance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   703
     * @return  {@code true} if the specified object is equal to this {@code TabularDataSupport} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // if obj is null, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // if obj is not a TabularData, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        TabularData other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            other = (TabularData) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // Now, really test for equality between this TabularData implementation and the other:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        // their tabularType should be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if ( ! this.getTabularType().equals(other.getTabularType()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // their contents should be equal:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // . same size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        // . values in this instance are in the other (we know there are no duplicate elements possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        // (row values comparison is enough, because keys are calculated according to tabularType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (this.size() != other.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   738
        for (CompositeData value : dataMap.values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if ( ! other.containsValue(value) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // All tests for equality were successfull
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   750
     * Returns the hash code value for this {@code TabularDataSupport} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   752
     * The hash code of a {@code TabularDataSupport} instance is the sum of the hash codes
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   753
     * of all elements of information used in {@code equals} comparisons
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * (ie: its <i>tabular type</i> and its content, where the content is defined as all the CompositeData values).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   756
     * This ensures that {@code t1.equals(t2)} implies that {@code t1.hashCode()==t2.hashCode()}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   757
     * for any two {@code TabularDataSupport} instances {@code t1} and {@code t2},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * as required by the general contract of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * {@link Object#hashCode() Object.hashCode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   761
     * However, note that another instance of a class implementing the {@code TabularData} interface
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   762
     * may be equal to this {@code TabularDataSupport} instance as defined by {@link #equals},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * but may have a different hash code if it is calculated differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   765
     * @return  the hash code value for this {@code TabularDataSupport} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
   public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        result += this.tabularType.hashCode();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   772
        for (Object value : values())
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   773
            result += value.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   780
     * Returns a string representation of this {@code TabularDataSupport} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <p>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   782
     * The string representation consists of the name of this class
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   783
     * (ie {@code javax.management.openmbean.TabularDataSupport}),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * the string representation of the tabular type of this instance, and the string representation of the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * (ie list the key=value mappings as returned by a call to
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   786
     * {@code dataMap.}{@link java.util.HashMap#toString() toString()}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   788
     * @return  a string representation of this {@code TabularDataSupport} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            .append(this.getClass().getName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            .append("(tabularType=")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            .append(tabularType.toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            .append(",contents=")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            .append(dataMap.toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            .append(")")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            .toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /* *** TabularDataSupport internal utility methods *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   809
     * Returns the index for value, assuming value is valid for this {@code TabularData} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * (ie value is not null, and its composite type is equal to row type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   812
     * The index is a List, and not an array, so that an
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   813
     * index.equals(otherIndex) call will actually compare contents,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * not just the objects references as is done for an array object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   816
     * The returned List is unmodifiable so that once a row has been put
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   817
     * into the dataMap, its index cannot be modified,
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   818
     * for example by a user that would attempt to modify an
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   819
     * index contained in the Set returned by keySet().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    private List<?> internalCalculateIndex(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return Collections.unmodifiableList(Arrays.asList(value.getAll(this.indexNamesArray)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   827
     * Checks if the specified key is valid for this {@code TabularData} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @throws  InvalidOpenTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    private void checkKeyType(Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // Check key is neither null nor empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if ( (key == null) || (key.length == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            throw new NullPointerException("Argument key cannot be null or empty.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        /* Now check key is valid with tabularType index and row type definitions: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // key[] should have the size expected for an index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (key.length != this.indexNamesArray.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            throw new InvalidKeyException("Argument key's length="+ key.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                                          " is different from the number of item values, which is "+ indexNamesArray.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                          ", specified for the indexing rows in this TabularData instance.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        // each element in key[] should be a value for its corresponding open type specified in rowType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        OpenType<?> keyElementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        for (int i=0; i<key.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            keyElementType = tabularType.getRowType().getType(this.indexNamesArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if ( (key[i] != null) && (! keyElementType.isValue(key[i])) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                throw new InvalidKeyException("Argument element key["+ i +"] is not a value for the open type expected for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                                              "this element of the index, whose name is \""+ indexNamesArray[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                              "\" and whose open type is "+ keyElementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   864
     * Checks the specified value's type is valid for this {@code TabularData} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * (ie value is not null, and its composite type is equal to row type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @throws  InvalidOpenTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private void checkValueType(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        // Check value is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            throw new NullPointerException("Argument value cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        // if value's type is not the same as this instance's row type, throw InvalidOpenTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (!tabularType.getRowType().isValue(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            throw new InvalidOpenTypeException("Argument value's composite type ["+ value.getCompositeType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                               "] is not assignable to "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                               "this TabularData instance's row type ["+ tabularType.getRowType() +"].");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 28059
diff changeset
   888
     * Checks if the specified value can be put (ie added) in this {@code TabularData} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * (ie value is not null, its composite type is equal to row type, and its index is not already used),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * and returns the index calculated for this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * The index is a List, and not an array, so that an index.equals(otherIndex) call will actually compare contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * not just the objects references as is done for an array object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @throws  InvalidOpenTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @throws  KeyAlreadyExistsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    private List<?> checkValueAndIndex(CompositeData value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        // Check value is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        checkValueType(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // Calculate value's index according to this instance's tabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        // and check it is not already used for a mapping in the parent HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        List<?> index = internalCalculateIndex(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (dataMap.containsKey(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            throw new KeyAlreadyExistsException("Argument value's index, calculated according to this TabularData "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                                "instance's tabularType, already refers to a value in this table.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // The check is OK, so return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Deserializes a {@link TabularDataSupport} from an {@link ObjectInputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
      in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
      List<String> tmpNames = tabularType.getIndexNames();
49794
274be83ad4b7 8189985: Improve tabular data portability
asapre
parents: 47216
diff changeset
   927
      int size = tmpNames.size();
274be83ad4b7 8189985: Improve tabular data portability
asapre
parents: 47216
diff changeset
   928
      SharedSecrets.getJavaObjectInputStreamAccess().checkArray(in, String[].class, size);
274be83ad4b7 8189985: Improve tabular data portability
asapre
parents: 47216
diff changeset
   929
      indexNamesArray = tmpNames.toArray(new String[size]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
}