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