jdk/src/java.base/share/classes/java/util/AbstractMap.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
child 32108 aa5490a167ee
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22097
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Map.Entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This class provides a skeletal implementation of the <tt>Map</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * interface, to minimize the effort required to implement this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>To implement an unmodifiable map, the programmer needs only to extend this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * class and provide an implementation for the <tt>entrySet</tt> method, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * returns a set-view of the map's mappings.  Typically, the returned set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * will, in turn, be implemented atop <tt>AbstractSet</tt>.  This set should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * should not support the <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>To implement a modifiable map, the programmer must additionally override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * this class's <tt>put</tt> method (which otherwise throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <tt>UnsupportedOperationException</tt>), and the iterator returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <tt>entrySet().iterator()</tt> must additionally implement its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>The programmer should generally provide a void (no argument) and map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * constructor, as per the recommendation in the <tt>Map</tt> interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>The documentation for each non-abstract method in this class describes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * implementation in detail.  Each of these methods may be overridden if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * map being implemented admits a more efficient implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public abstract class AbstractMap<K,V> implements Map<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected AbstractMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
    81
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
    82
     * This implementation returns <tt>entrySet().size()</tt>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return entrySet().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
    91
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
    92
     * This implementation returns <tt>size() == 0</tt>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   101
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   102
     * This implementation iterates over <tt>entrySet()</tt> searching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * for an entry with the specified value.  If such an entry is found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <tt>true</tt> is returned.  If the iteration terminates without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * finding such an entry, <tt>false</tt> is returned.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * implementation requires linear time in the size of the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (value==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (e.getValue()==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (value.equals(e.getValue()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   132
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   133
     * This implementation iterates over <tt>entrySet()</tt> searching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * for an entry with the specified key.  If such an entry is found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <tt>true</tt> is returned.  If the iteration terminates without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * finding such an entry, <tt>false</tt> is returned.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * implementation requires linear time in the size of the map; many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * implementations will override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Iterator<Map.Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (e.getKey()==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (key.equals(e.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   164
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   165
     * This implementation iterates over <tt>entrySet()</tt> searching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * for an entry with the specified key.  If such an entry is found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * the entry's value is returned.  If the iteration terminates without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * finding such an entry, <tt>null</tt> is returned.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * implementation requires linear time in the size of the map; many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * implementations will override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (e.getKey()==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    return e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (key.equals(e.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    return e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   199
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   200
     * This implementation always throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <tt>UnsupportedOperationException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throw new UnsupportedOperationException();
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
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   215
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   216
     * This implementation iterates over <tt>entrySet()</tt> searching for an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * entry with the specified key.  If such an entry is found, its value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * obtained with its <tt>getValue</tt> operation, the entry is removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * from the collection (and the backing map) with the iterator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <tt>remove</tt> operation, and the saved value is returned.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * iteration terminates without finding such an entry, <tt>null</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * returned.  Note that this implementation requires linear time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * size of the map; many implementations will override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * iterator does not support the <tt>remove</tt> method and this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * contains a mapping for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Entry<K,V> correctEntry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            while (correctEntry==null && i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (e.getKey()==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    correctEntry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            while (correctEntry==null && i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (key.equals(e.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    correctEntry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        V oldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (correctEntry !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            oldValue = correctEntry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   265
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   266
     * This implementation iterates over the specified map's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * operation once for each entry returned by the iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <tt>UnsupportedOperationException</tt> if this map does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * the <tt>put</tt> operation and the specified map is nonempty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   287
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   288
     * This implementation calls <tt>entrySet().clear()</tt>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * does not support the <tt>clear</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        entrySet().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Each of these fields are initialized to contain an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * appropriate view the first time this view is requested.  The views are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * stateless, so there's no reason to create more than one of each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 23010
diff changeset
   308
    transient volatile Set<K>        keySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 23010
diff changeset
   309
    transient volatile Collection<V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   314
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   315
     * This implementation returns a set that subclasses {@link AbstractSet}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * The subclass's iterator method returns a "wrapper object" over this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * map's <tt>entrySet()</tt> iterator.  The <tt>size</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * delegates to this map's <tt>size</tt> method and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <tt>contains</tt> method delegates to this map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <tt>containsKey</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p>The set is created the first time this method is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * and returned in response to all subsequent calls.  No synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * is performed, so there is a slight chance that multiple calls to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * method will not all return the same set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (keySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            keySet = new AbstractSet<K>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    return new Iterator<K>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        private Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                            return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                            return i.next().getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return AbstractMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    return AbstractMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    AbstractMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                public boolean contains(Object k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    return AbstractMap.this.containsKey(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   371
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   372
     * This implementation returns a collection that subclasses {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * AbstractCollection}.  The subclass's iterator method returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * The <tt>size</tt> method delegates to this map's <tt>size</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * method and the <tt>contains</tt> method delegates to this map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <tt>containsValue</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <p>The collection is created the first time this method is called, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * returned in response to all subsequent calls.  No synchronization is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * performed, so there is a slight chance that multiple calls to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * method will not all return the same collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            values = new AbstractCollection<V>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    return new Iterator<V>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        private Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                            return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            return i.next().getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return AbstractMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    return AbstractMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    AbstractMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                public boolean contains(Object v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    return AbstractMap.this.containsValue(v);
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
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public abstract Set<Entry<K,V>> entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Compares the specified object with this map for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <tt>true</tt> if the given object is also a map and the two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * represent the same mappings.  More formally, two maps <tt>m1</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <tt>m2</tt> represent the same mappings if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <tt>equals</tt> method works properly across different implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * of the <tt>Map</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   439
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   440
     * This implementation first checks if the specified object is this map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * if so it returns <tt>true</tt>.  Then, it checks if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * object is a map whose size is identical to the size of this map; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * not, it returns <tt>false</tt>.  If so, it iterates over this map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <tt>entrySet</tt> collection, and checks that the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * contains each mapping that this map contains.  If the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * fails to contain such a mapping, <tt>false</tt> is returned.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * iteration completes, <tt>true</tt> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param o object to be compared for equality with this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (!(o instanceof Map))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   458
        Map<?,?> m = (Map<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (m.size() != size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   463
            for (Entry<K, V> e : entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (value == null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   467
                    if (!(m.get(key) == null && m.containsKey(key)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    if (!value.equals(m.get(key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        } catch (ClassCastException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        } catch (NullPointerException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * defined to be the sum of the hash codes of each entry in the map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   491
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   492
     * This implementation iterates over <tt>entrySet()</tt>, calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * set, and adding up the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @see Map.Entry#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @see Set#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        int h = 0;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   503
        for (Entry<K, V> entry : entrySet())
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   504
            h += entry.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Returns a string representation of this map.  The string representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * consists of a list of key-value mappings in the order returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * map's <tt>entrySet</tt> view's iterator, enclosed in braces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * (<tt>"{}"</tt>).  Adjacent mappings are separated by the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <tt>", "</tt> (comma and space).  Each key-value mapping is rendered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * the key followed by an equals sign (<tt>"="</tt>) followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * associated value.  Keys and values are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return a string representation of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (! i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            return "{}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        sb.append('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            sb.append(key   == this ? "(this Map)" : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            sb.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            sb.append(value == this ? "(this Map)" : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (! i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                return sb.append('}').toString();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   536
            sb.append(',').append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * and values themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    protected Object clone() throws CloneNotSupportedException {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   547
        AbstractMap<?,?> result = (AbstractMap<?,?>)super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        result.keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        result.values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Utility method for SimpleEntry and SimpleImmutableEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Test for equality, checking for nulls.
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 14342
diff changeset
   556
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 14342
diff changeset
   557
     * NB: Do not replace with Object.equals until JDK-8015417 is resolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private static boolean eq(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return o1 == null ? o2 == null : o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    // Implementation Note: SimpleEntry and SimpleImmutableEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    // are distinct unrelated classes, even though they share
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    // some code. Since you can't add or subtract final-ness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    // of a field in a subclass, they can't share representations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    // and the amount of duplicated code is too small to warrant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    // exposing a common abstract class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * An Entry maintaining a key and a value.  The value may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * changed using the <tt>setValue</tt> method.  This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * facilitates the process of building custom map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * implementations. For example, it may be convenient to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * arrays of <tt>SimpleEntry</tt> instances in method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * <tt>Map.entrySet().toArray</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public static class SimpleEntry<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        implements Entry<K,V>, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        private static final long serialVersionUID = -8499721149061103585L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        private final K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        private V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * Creates an entry representing a mapping from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * key to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * @param key the key represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * @param value the value represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        public SimpleEntry(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            this.key   = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * Creates an entry representing the same mapping as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * @param entry the entry to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        public SimpleEntry(Entry<? extends K, ? extends V> entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            this.key   = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            this.value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * Returns the key corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * @return the key corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
         * Returns the value corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         * @return the value corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * Replaces the value corresponding to this entry with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * @param value new value to be stored in this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * @return the old value corresponding to the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            V oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * Compares the specified object with this entry for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         * Returns {@code true} if the given object is also a map entry and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         * the two entries represent the same mapping.  More formally, two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * entries {@code e1} and {@code e2} represent the same mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * if<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         *   (e1.getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         *    e2.getKey()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         *    e1.getKey().equals(e2.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         *   &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         *   (e1.getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         *    e2.getValue()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         *    e1.getValue().equals(e2.getValue()))</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * This ensures that the {@code equals} method works properly across
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * different implementations of the {@code Map.Entry} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         * @param o object to be compared for equality with this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * @return {@code true} if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         *         entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * @see    #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   667
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return eq(key, e.getKey()) && eq(value, e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * Returns the hash code value for this map entry.  The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * of a map entry {@code e} is defined to be: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * This ensures that {@code e1.equals(e2)} implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * {@code e1.hashCode()==e2.hashCode()} for any two Entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * {@code e1} and {@code e2}, as required by the general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         * contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * @return the hash code value for this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * @see    #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return (key   == null ? 0 :   key.hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                   (value == null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * Returns a String representation of this map entry.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * implementation returns the string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * entry's key followed by the equals character ("<tt>=</tt>")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         * followed by the string representation of this entry's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         * @return a String representation of this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return key + "=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * An Entry maintaining an immutable key and value.  This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * does not support method <tt>setValue</tt>.  This class may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * convenient in methods that return thread-safe snapshots of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public static class SimpleImmutableEntry<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        implements Entry<K,V>, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        private static final long serialVersionUID = 7138329143949025153L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        private final K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        private final V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         * Creates an entry representing a mapping from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         * key to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * @param key the key represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * @param value the value represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        public SimpleImmutableEntry(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            this.key   = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         * Creates an entry representing the same mapping as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
         * @param entry the entry to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            this.key   = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            this.value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * Returns the key corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         * @return the key corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * Returns the value corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * @return the value corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * Replaces the value corresponding to this entry with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * value (optional operation).  This implementation simply throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         * <tt>UnsupportedOperationException</tt>, as this class implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         * an <i>immutable</i> map entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * @param value new value to be stored in this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * @return (Does not return)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * @throws UnsupportedOperationException always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * Compares the specified object with this entry for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         * Returns {@code true} if the given object is also a map entry and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         * the two entries represent the same mapping.  More formally, two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         * entries {@code e1} and {@code e2} represent the same mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * if<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         *   (e1.getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         *    e2.getKey()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         *    e1.getKey().equals(e2.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         *   &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         *   (e1.getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         *    e2.getValue()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         *    e1.getValue().equals(e2.getValue()))</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * This ensures that the {@code equals} method works properly across
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * different implementations of the {@code Map.Entry} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * @param o object to be compared for equality with this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         * @return {@code true} if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
         *         entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * @see    #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   798
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            return eq(key, e.getKey()) && eq(value, e.getValue());
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
         * Returns the hash code value for this map entry.  The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * of a map entry {@code e} is defined to be: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         * This ensures that {@code e1.equals(e2)} implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * {@code e1.hashCode()==e2.hashCode()} for any two Entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         * {@code e1} and {@code e2}, as required by the general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
         * contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * @return the hash code value for this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * @see    #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return (key   == null ? 0 :   key.hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                   (value == null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         * Returns a String representation of this map entry.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
         * implementation returns the string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * entry's key followed by the equals character ("<tt>=</tt>")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         * followed by the string representation of this entry's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * @return a String representation of this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            return key + "=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
}