src/java.base/share/classes/java/util/AbstractMap.java
author darcy
Wed, 09 Oct 2019 10:17:50 -0700
changeset 58520 e036ee8bae56
parent 57956 e0b8b019d2f5
child 58679 9c3209ff7550
permissions -rw-r--r--
8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes Reviewed-by: rriggs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 49433
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
/**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    30
 * This class provides a skeletal implementation of the {@code Map}
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    34
 * class and provide an implementation for the {@code entrySet} method, which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * returns a set-view of the map's mappings.  Typically, the returned set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    36
 * will, in turn, be implemented atop {@code AbstractSet}.  This set should
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    37
 * not support the {@code add} or {@code remove} methods, and its iterator
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    38
 * should not support the {@code remove} method.
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    41
 * this class's {@code put} method (which otherwise throws an
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    42
 * {@code UnsupportedOperationException}), and the iterator returned by
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    43
 * {@code entrySet().iterator()} must additionally implement its
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    44
 * {@code remove} method.
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    47
 * constructor, as per the recommendation in the {@code Map} interface
2
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
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47216
diff changeset
    55
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    82
     * This implementation returns {@code entrySet().size()}.
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    92
     * This implementation returns {@code size() == 0}.
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   102
     * This implementation iterates over {@code entrySet()} searching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * for an entry with the specified value.  If such an entry is found,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   104
     * {@code true} is returned.  If the iteration terminates without
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   105
     * finding such an entry, {@code false} is returned.  Note that this
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   133
     * This implementation iterates over {@code entrySet()} searching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * for an entry with the specified key.  If such an entry is found,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   135
     * {@code true} is returned.  If the iteration terminates without
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   136
     * finding such an entry, {@code false} is returned.  Note that this
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   165
     * This implementation iterates over {@code entrySet()} 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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   168
     * finding such an entry, {@code null} is returned.  Note that this
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   201
     * {@code UnsupportedOperationException}.
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   216
     * This implementation iterates over {@code entrySet()} 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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   218
     * obtained with its {@code getValue} operation, the entry is removed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * from the collection (and the backing map) with the iterator's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   220
     * {@code remove} operation, and the saved value is returned.  If the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   221
     * iteration terminates without finding such an entry, {@code null} is
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   226
     * {@code UnsupportedOperationException} if the {@code entrySet}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   227
     * iterator does not support the {@code remove} method and this map
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   267
     * {@code entrySet()} collection, and calls this map's {@code put}
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   271
     * {@code UnsupportedOperationException} if this map does not support
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   272
     * the {@code put} operation and the specified map is nonempty.
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   288
     * This implementation calls {@code entrySet().clear()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>Note that this implementation throws an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   291
     * {@code UnsupportedOperationException} if the {@code entrySet}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   292
     * does not support the {@code clear} operation.
2
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.
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   307
     *
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   308
     * <p>Since there is no synchronization performed while accessing these fields,
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   309
     * it is expected that java.util.Map view classes using these fields have
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   310
     * no non-final fields (or any fields at all except for outer-this). Adhering
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   311
     * to this rule would make the races on these fields benign.
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   312
     *
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   313
     * <p>It is also imperative that implementations read the field only once,
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   314
     * as in:
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   315
     *
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   316
     * <pre> {@code
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   317
     * public Set<K> keySet() {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   318
     *   Set<K> ks = keySet;  // single racy read
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   319
     *   if (ks == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   320
     *     ks = new KeySet();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   321
     *     keySet = ks;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   322
     *   }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   323
     *   return ks;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   324
     * }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   325
     *}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   327
    transient Set<K>        keySet;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   328
    transient Collection<V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   333
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   334
     * This implementation returns a set that subclasses {@link AbstractSet}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * The subclass's iterator method returns a "wrapper object" over this
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   336
     * map's {@code entrySet()} iterator.  The {@code size} method
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   337
     * delegates to this map's {@code size} method and the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   338
     * {@code contains} method delegates to this map's
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   339
     * {@code containsKey} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p>The set is created the first time this method is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * and returned in response to all subsequent calls.  No synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * is performed, so there is a slight chance that multiple calls to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * method will not all return the same set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public Set<K> keySet() {
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   347
        Set<K> ks = keySet;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   348
        if (ks == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   349
            ks = new AbstractSet<K>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return new Iterator<K>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        private Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            return i.next().getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    return AbstractMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    return AbstractMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    AbstractMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                public boolean contains(Object k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    return AbstractMap.this.containsKey(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            };
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   384
            keySet = ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   386
        return ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   392
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   393
     * This implementation returns a collection that subclasses {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * AbstractCollection}.  The subclass's iterator method returns a
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   395
     * "wrapper object" over this map's {@code entrySet()} iterator.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   396
     * The {@code size} method delegates to this map's {@code size}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   397
     * method and the {@code contains} method delegates to this map's
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   398
     * {@code containsValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <p>The collection is created the first time this method is called, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * returned in response to all subsequent calls.  No synchronization is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * performed, so there is a slight chance that multiple calls to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * method will not all return the same collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public Collection<V> values() {
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   406
        Collection<V> vals = values;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   407
        if (vals == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   408
            vals = new AbstractCollection<V>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    return new Iterator<V>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        private Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            return i.hasNext();
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 V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                            return i.next().getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    return AbstractMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    return AbstractMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    AbstractMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                public boolean contains(Object v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    return AbstractMap.this.containsValue(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            };
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   443
            values = vals;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   445
        return vals;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public abstract Set<Entry<K,V>> entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Compares the specified object with this map for equality.  Returns
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   455
     * {@code true} if the given object is also a map and the two maps
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   456
     * represent the same mappings.  More formally, two maps {@code m1} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   457
     * {@code m2} represent the same mappings if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   458
     * {@code m1.entrySet().equals(m2.entrySet())}.  This ensures that the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   459
     * {@code equals} method works properly across different implementations
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   460
     * of the {@code Map} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   462
     * @implSpec
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   463
     * This implementation first checks if the specified object is this map;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   464
     * if so it returns {@code true}.  Then, it checks if the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * object is a map whose size is identical to the size of this map; if
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   466
     * not, it returns {@code false}.  If so, it iterates over this map's
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   467
     * {@code entrySet} collection, and checks that the specified map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * contains each mapping that this map contains.  If the specified map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   469
     * fails to contain such a mapping, {@code false} is returned.  If the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   470
     * iteration completes, {@code true} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param o object to be compared for equality with this map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   473
     * @return {@code true} if the specified object is equal to this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (!(o instanceof Map))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   481
        Map<?,?> m = (Map<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (m.size() != size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        try {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   486
            for (Entry<K, V> e : entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                if (value == null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   490
                    if (!(m.get(key) == null && m.containsKey(key)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    if (!value.equals(m.get(key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        } catch (ClassCastException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        } catch (NullPointerException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * defined to be the sum of the hash codes of each entry in the map's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   509
     * {@code entrySet()} view.  This ensures that {@code m1.equals(m2)}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   510
     * implies that {@code m1.hashCode()==m2.hashCode()} for any two maps
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   511
     * {@code m1} and {@code m2}, as required by the general contract of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
22096
ca113030b2b3 8031133: AbstractMap should specify its default implementation using @implSpec
dl
parents: 18818
diff changeset
   514
     * @implSpec
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   515
     * This implementation iterates over {@code entrySet()}, calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * set, and adding up the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @see Map.Entry#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see Set#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        int h = 0;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   526
        for (Entry<K, V> entry : entrySet())
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18818
diff changeset
   527
            h += entry.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Returns a string representation of this map.  The string representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * consists of a list of key-value mappings in the order returned by the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   534
     * map's {@code entrySet} view's iterator, enclosed in braces
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   535
     * ({@code "{}"}).  Adjacent mappings are separated by the characters
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   536
     * {@code ", "} (comma and space).  Each key-value mapping is rendered as
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   537
     * the key followed by an equals sign ({@code "="}) followed by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * associated value.  Keys and values are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return a string representation of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        Iterator<Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (! i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            return "{}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        sb.append('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            sb.append(key   == this ? "(this Map)" : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            sb.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            sb.append(value == this ? "(this Map)" : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (! i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                return sb.append('}').toString();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   559
            sb.append(',').append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   564
     * Returns a shallow copy of this {@code AbstractMap} instance: the keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * and values themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    protected Object clone() throws CloneNotSupportedException {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   570
        AbstractMap<?,?> result = (AbstractMap<?,?>)super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        result.keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        result.values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Utility method for SimpleEntry and SimpleImmutableEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Test for equality, checking for nulls.
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 14342
diff changeset
   579
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 14342
diff changeset
   580
     * NB: Do not replace with Object.equals until JDK-8015417 is resolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private static boolean eq(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return o1 == null ? o2 == null : o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    // Implementation Note: SimpleEntry and SimpleImmutableEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    // are distinct unrelated classes, even though they share
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    // some code. Since you can't add or subtract final-ness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    // of a field in a subclass, they can't share representations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    // and the amount of duplicated code is too small to warrant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    // exposing a common abstract class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * An Entry maintaining a key and a value.  The value may be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   596
     * changed using the {@code setValue} method.  This class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * facilitates the process of building custom map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * implementations. For example, it may be convenient to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   599
     * arrays of {@code SimpleEntry} instances in method
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   600
     * {@code Map.entrySet().toArray}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public static class SimpleEntry<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        implements Entry<K,V>, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 49433
diff changeset
   607
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        private static final long serialVersionUID = -8499721149061103585L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
   610
        @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        private final K key;
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
   612
        @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        private V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * Creates an entry representing a mapping from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * key to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         * @param key the key represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * @param value the value represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public SimpleEntry(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            this.key   = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * Creates an entry representing the same mapping as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * @param entry the entry to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        public SimpleEntry(Entry<? extends K, ? extends V> entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            this.key   = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            this.value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * Returns the key corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * @return the key corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * Returns the value corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * @return the value corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * Replaces the value corresponding to this entry with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * @param value new value to be stored in this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * @return the old value corresponding to the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            V oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * Compares the specified object with this entry for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * Returns {@code true} if the given object is also a map entry and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * the two entries represent the same mapping.  More formally, two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * entries {@code e1} and {@code e2} represent the same mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * if<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         *   (e1.getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         *    e2.getKey()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         *    e1.getKey().equals(e2.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         *   &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         *   (e1.getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         *    e2.getValue()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         *    e1.getValue().equals(e2.getValue()))</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * This ensures that the {@code equals} method works properly across
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         * different implementations of the {@code Map.Entry} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * @param o object to be compared for equality with this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         * @return {@code true} if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         *         entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         * @see    #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   693
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return eq(key, e.getKey()) && eq(value, e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * Returns the hash code value for this map entry.  The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * of a map entry {@code e} is defined to be: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * This ensures that {@code e1.equals(e2)} implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * {@code e1.hashCode()==e2.hashCode()} for any two Entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * {@code e1} and {@code e2}, as required by the general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         * @return the hash code value for this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         * @see    #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            return (key   == null ? 0 :   key.hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                   (value == null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         * Returns a String representation of this map entry.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * implementation returns the string representation of this
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   718
         * entry's key followed by the equals character ("{@code =}")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         * followed by the string representation of this entry's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         * @return a String representation of this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            return key + "=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * An Entry maintaining an immutable key and value.  This class
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   731
     * does not support method {@code setValue}.  This class may be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * convenient in methods that return thread-safe snapshots of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public static class SimpleImmutableEntry<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        implements Entry<K,V>, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 49433
diff changeset
   740
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        private static final long serialVersionUID = 7138329143949025153L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
   743
        @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        private final K key;
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
   745
        @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        private final V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
         * Creates an entry representing a mapping from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * key to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * @param key the key represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * @param value the value represented by this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        public SimpleImmutableEntry(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            this.key   = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            this.value = 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
         * Creates an entry representing the same mapping as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         * @param entry the entry to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            this.key   = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            this.value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
         * Returns the key corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         * @return the key corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * Returns the value corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * @return the value corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * Replaces the value corresponding to this entry with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         * value (optional operation).  This implementation simply throws
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   792
         * {@code UnsupportedOperationException}, as this class implements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * an <i>immutable</i> map entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * @param value new value to be stored in this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * @return (Does not return)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * @throws UnsupportedOperationException always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * Compares the specified object with this entry for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         * Returns {@code true} if the given object is also a map entry and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         * the two entries represent the same mapping.  More formally, two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         * entries {@code e1} and {@code e2} represent the same mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * if<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         *   (e1.getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
         *    e2.getKey()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         *    e1.getKey().equals(e2.getKey()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         *   &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         *   (e1.getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         *    e2.getValue()==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         *    e1.getValue().equals(e2.getValue()))</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * This ensures that the {@code equals} method works properly across
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * different implementations of the {@code Map.Entry} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * @param o object to be compared for equality with this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         * @return {@code true} if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         *         entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
         * @see    #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7668
diff changeset
   827
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return eq(key, e.getKey()) && eq(value, e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         * Returns the hash code value for this map entry.  The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         * of a map entry {@code e} is defined to be: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         * This ensures that {@code e1.equals(e2)} implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * {@code e1.hashCode()==e2.hashCode()} for any two Entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * {@code e1} and {@code e2}, as required by the general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
         * @return the hash code value for this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
         * @see    #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return (key   == null ? 0 :   key.hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                   (value == null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * Returns a String representation of this map entry.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * implementation returns the string representation of this
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   852
         * entry's key followed by the equals character ("{@code =}")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * followed by the string representation of this entry's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * @return a String representation of this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            return key + "=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
}