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