jdk/src/share/classes/com/sun/tools/jdi/LinkedHashMap.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2000 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Hash table based implementation of the Map interface.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * provides all of the optional Map operations, and permits null values and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the null key.  (HashMap is roughly equivalent to Hashtable, except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is unsynchronized and permits nulls.) In addition, elements in the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ordered and doubly linked together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This implementation provides constant-time performance for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * operations (get and put), assuming the the hash function disperses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * elements properly among the buckets.  Iteration over Collection views
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * requires time proportional to its size (the number of key-value mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and returns elements in the order they are linked. In a HashMap the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * iteration would require time  proportional to the capacity of the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * plus the map size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * An instance of LinkedHashMap has two parameters that affect its efficiency:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * its <i>capacity</i> and its <i>load factor</i>. The load factor should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * between 0.0 and 1.0. When the number of mappings in the LinkedHashMap exceeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the product of the load factor and the current capacity, the capacity is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * increased by calling the rehash method which requires time proportional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * to the number of key-value mappings in the map. Larger load factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * use memory more efficiently, at the expense of larger expected time per
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * If many mappings are to be stored in a LinkedHashMap, creating it with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * sufficiently large capacity will allow the mappings to be stored more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * efficiently than letting it perform automatic rehashing as needed to grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <strong>Note that this implementation is not synchronized.</strong> If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * multiple threads access a LinkedHashMap concurrently, and at least one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * threads modifies the LinkedHashMap structurally, it <em>must</em> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * synchronized externally.  (A structural modification is any operation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * adds or deletes one or more mappings; merely changing the value associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * with a key that is already contained in the Table is not a structural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * modification.)  This is typically accomplished by synchronizing on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * object that naturally encapsulates the LinkedHashMap.  If no such object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * exists, the LinkedHashMap should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Collections.synchronizedSet method.  This is best done at creation time, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * prevent accidental unsynchronized access to the LinkedHashMap:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      Map m = Collections.synchronizedMap(new LinkedHashMap(...));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The Iterators returned by the iterator methods of the Collections returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * by all of LinkedHashMap's "collection view methods" are <em>fail-fast</em>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * if the LinkedHashMap is structurally modified at any time after the Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * is created, in any way except through the Iterator's own remove or add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * methods, the Iterator will throw a ConcurrentModificationException.  Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * in the face of concurrent modification, the Iterator fails quickly and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * cleanly, rather than risking arbitrary, non-deterministic behavior at an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @author  Zhenghua Li
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see     java.util.Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @see     java.util.Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @see     java.util.TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see     java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @see     java.util.HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public class LinkedHashMap extends AbstractMap implements Map, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The hash table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private transient Entry table[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * The head of the double linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private transient Entry header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The total number of mappings in the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private transient int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Rehashes the table when count exceeds this threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The load factor for the LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The number of times this LinkedHashMap has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Structural modifications are those that change the number of mappings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the LinkedHashMap or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the LinkedHashMap fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Constructs a new, empty LinkedHashMap with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * capacity and the specified load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param      initialCapacity   the initial capacity of the LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param      loadFactor        a number between 0.0 and 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception  IllegalArgumentException  if the initial capacity is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *               than or equal to zero, or if the load factor is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *               or equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public LinkedHashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new IllegalArgumentException("Illegal Initial Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if ((loadFactor > 1) || (loadFactor <= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IllegalArgumentException("Illegal Load factor: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                               loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (initialCapacity==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            initialCapacity = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.loadFactor = loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        table = new Entry[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        threshold = (int)(initialCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        header = new Entry(-1, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        header.before = header.after = header;
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
     * Constructs a new, empty LinkedHashMap with the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * and default load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param   initialCapacity   the initial capacity of the LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public LinkedHashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this(initialCapacity, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Constructs a new, empty LinkedHashMap with a default capacity and load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public LinkedHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this(101, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Constructs a new LinkedHashMap with the same mappings as the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Map.  The LinkedHashMap is created with a capacity of thrice the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * of mappings in the given Map or 11 (whichever is greater), and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * default load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public LinkedHashMap(Map t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        this(Math.max(3*t.size(), 11), 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        putAll(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the number of key-value mappings in this Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return count;
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
     * Returns true if this Map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return count == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Returns true if this LinkedHashMap maps one or more keys to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param value value whose presence in this Map is to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (value==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            for (Entry e = header.after; e != header; e = e.after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (e.value==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            for (Entry e = header.after; e != header; e = e.after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (value.equals(e.value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns true if this LinkedHashMap contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param key key whose presence in this Map is to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            for (Entry e = tab[index]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (e.hash==hash && e.key.equals(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            for (Entry e = tab[0]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (e.key==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns the value to which this LinkedHashMap maps the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Returns null if the LinkedHashMap contains no mapping for this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * A return value of null does not <em>necessarily</em> indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * LinkedHashMap contains no mapping for the key; it's also possible that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the LinkedHashMap explicitly maps the key to null.  The containsKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * operation may be used to distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param key key whose associated value is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Entry e = getEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return e==null ? null : e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns the entry associated with the specified key in the LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Returns null if the LinkedHashMap contains no mapping for this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private Entry getEntry(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            for (Entry e = tab[index]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if ((e.hash == hash) && e.key.equals(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            for (Entry e = tab[0]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (e.key==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Rehashes the contents of the LinkedHashMap into a LinkedHashMap with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * larger capacity. This method is called automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * number of keys in the LinkedHashMap exceeds this LinkedHashMap's capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private void rehash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        int oldCapacity = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        Entry oldMap[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int newCapacity = oldCapacity * 2 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Entry newMap[] = new Entry[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        threshold = (int)(newCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        table = newMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (Entry e = header.after; e != header; e = e.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            int index = (e.hash & 0x7FFFFFFF) % newCapacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            e.next = newMap[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            newMap[index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Remove an entry from the linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private void listRemove(Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        entry.before.after = entry.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        entry.after.before = entry.before;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    * Add the specified entry before the specified existing entry to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    * the linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private void listAddBefore(Entry entry, Entry existEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        entry.after = existEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        entry.before = existEntry.before;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        entry.before.after = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        entry.after.before = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Returns the position of the mapping for the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * in the ordered map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param key the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return index of the key mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public int indexOf(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            for (Entry e = header.after; e != header; e = e.after, i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (e.key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            for (Entry e = header.after; e != header; e = e.after, i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if(key.equals(e.key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Associates the specified value with the specified key in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * LinkedHashMap. If the LinkedHashMap previously contained a mapping for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * this key, the old value is replaced and the position of this mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * entry in the double linked list remains the same. Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * mapping entry is created and inserted into the list before the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * existing mapping entry. The method returns the previous value associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * with the specified key, or null if there was no mapping for key.  A null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * return can also indicate that the LinkedHashMap previously associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * null with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private Object putAhead(Object key, Object value, Entry existEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // Makes sure the key is not already in the LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            for (Entry e = tab[index] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    Object old = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            for (Entry e = tab[0] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (e.key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    Object old = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (count >= threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // Rehash the table if the threshold is exceeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            rehash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // Creates the new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        Entry e = new Entry(hash, key, value, tab[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        tab[index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        listAddBefore(e, existEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Associates the specified value with the specified key in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * LinkedHashMap and position the mapping at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * If the LinkedHashMap previously contained a mapping for this key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * the old value is replaced and the position of this mapping entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * in the double linked list remains the same. Otherwise, a new mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * entry is created and inserted into the list at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param index     the position to put the key-value mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param key       key with which the specified value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param value     value to be associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return previous value associated with specified key, or null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *         was no mapping for key.  A null return can also indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         the LinkedHashMap previously associated null with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *         key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public Object put(int index, Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (index < 0 || index > count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        Entry e = header.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (index == count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return putAhead(key, value, header); //fast approach for append
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            for (int i = 0; i < index; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                e = e.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return putAhead(key, value, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Associates the specified value with the specified key in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * LinkedHashMap. If the LinkedHashMap previously contained a mapping for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * this key, the old value is replaced. The mapping entry is also appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * to the end of the ordered linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param key key with which the specified value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param value value to be associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @return previous value associated with specified key, or null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *         was no mapping for key.  A null return can also indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         the LinkedHashMap previously associated null with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public Object put(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return putAhead(key, value, header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Removes the mapping for this key from this LinkedHashMap if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * The mapping would also be removed from the double linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param key key whose mapping is to be removed from the Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @return previous value associated with specified key, or null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         was no mapping for key.  A null return can also indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *         the LinkedHashMap previously associated null with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *         key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public Object remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            for (Entry e = tab[index], prev = null; e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                 prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    if (prev != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    Object oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    listRemove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            for (Entry e = tab[0], prev = null; e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                 prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (e.key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    if (prev != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        tab[0] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    Object oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    listRemove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Copies all of the mappings from the specified Map to this LinkedHashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * These mappings will replace any mappings that this LinkedHashMap had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * any of the keys currently in the specified Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
      * @param t Mappings to be stored in this Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public void putAll(Map t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        Iterator i = t.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            Map.Entry e = (Map.Entry) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Removes all mappings from this LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        for (int index = tab.length; --index >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            tab[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        header.before = header.after = header;
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 shallow copy of this LinkedHashMap. The keys and values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return new LinkedHashMap(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    private transient Set keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    private transient Set entries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private transient Collection values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Returns a Set view of the keys contained in this LinkedHashMap.  The Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * is backed by the LinkedHashMap, so changes to the LinkedHashMap are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * reflected in the Set, and vice-versa.  The Set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * which removes the corresponding mapping from the LinkedHashMap, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Iterator.remove, Set.remove, removeAll retainAll, and clear operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * It does not support the add or addAll operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public Set keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (keySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            keySet = new AbstractSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                public Iterator iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    return new HashIterator(KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    return LinkedHashMap.this.remove(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    LinkedHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return keySet;
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
     * Returns a Collection view of the values contained in this LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * The Collection is backed by the LinkedHashMap, so changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * LinkedHashMap are reflected in the Collection, and vice-versa.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Collection supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * mapping from the LinkedHashMap, via the Iterator.remove,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Collection.remove, removeAll, retainAll and clear operations.  It does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * not support the add or addAll operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public Collection values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (values==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            values = new AbstractCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                public Iterator iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    return new HashIterator(VALUES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    LinkedHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Returns a Collection view of the mappings contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * LinkedHashMap. Each element in the returned collection is a Map.Entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * The Collection is backed by the LinkedHashMap, so changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * LinkedHashMap are reflected in the Collection, and vice-versa.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Collection supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * mapping from the LinkedHashMap, via the Iterator.remove,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Collection.remove, removeAll, retainAll and clear operations.  It does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * not support the add or addAll operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see   java.util.Map.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public Set entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (entries==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            entries = new AbstractSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                public Iterator iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    return new HashIterator(ENTRIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    Object key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    int hash = (key==null ? 0 : key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    for (Entry e = tab[index]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                        if (e.hash==hash && e.equals(entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    Object key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    int hash = (key==null ? 0 : key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    for (Entry e = tab[index], prev = null; e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                         prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        if (e.hash==hash && e.equals(entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                            modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            if (prev != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                            count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                            e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                            listRemove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    LinkedHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Compares the specified Object with this Map for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Returns true if the given object is also a LinkedHashMap and the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Maps represent the same mappings in the same order.  More formally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * two Maps <code>t1</code> and <code>t2</code> represent the same mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * if <code>t1.keySet().equals(t2.keySet())</code> and for every
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * key <code>k</code> in <code>t1.keySet()</code>, <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * </code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * This implementation first checks if the specified Object is this Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * if so it returns true.  Then, it checks if the specified Object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * a Map whose size is identical to the size of this Set; if not, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * it returns false.  If so, it iterates over this Map and the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Map's entrySet() Collection, and checks that the specified Map contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * each mapping that this Map contains at the same position.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * specified Map fails to contain such a mapping in the right order, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * is returned.  If the iteration completes, true is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @param o Object to be compared for equality with this Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @return true if the specified Object is equal to this Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (!(o instanceof LinkedHashMap))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        LinkedHashMap t = (LinkedHashMap) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (t.size() != size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        Iterator i1 = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        Iterator i2 = t.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        while (i1.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            Entry e1 = (Entry) i1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            Entry e2 = (Entry) i2.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            Object key1 = e1.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            Object value1 = e1.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            Object key2 = e2.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            Object value2 = e2.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if ((key1 == null ? key2 == null : key1.equals(key2)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                (value1 == null ? value2 == null : value1.equals(value2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * LinkedHashMap collision list entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private static class Entry implements Map.Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        Object key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        Entry next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // These fields comprise the doubly linked list that is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        Entry before, after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        Entry(int hash, Object key, Object value, Entry next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            this.hash = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // Map.Entry Ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        public Object getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        public Object setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            Object oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            Map.Entry e = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
               (value==null ? e.getValue()==null : value.equals(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return hash ^ (value==null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return key+"="+value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    // Types of Iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private static final int KEYS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    private static final int VALUES = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    private static final int ENTRIES = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    private class HashIterator implements Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        private Entry[] table = LinkedHashMap.this.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        private Entry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        private Entry lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        private int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * List should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        private int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        HashIterator(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            this.entry = LinkedHashMap.this.header.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            return entry != header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        public Object next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            if (entry == LinkedHashMap.this.header)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            Entry e = lastReturned = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            entry = e.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return type == KEYS ? e.key : (type == VALUES ? e.value : e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            Entry[] tab = LinkedHashMap.this.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (Entry e = tab[index], prev = null; e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                 prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                if (e == lastReturned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    expectedModCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    if (prev == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    listRemove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Save the state of the LinkedHashMap to a stream (i.e., serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * The objects will be written out in the order they are linked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        // Write out the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        // Write out number of buckets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        s.writeInt(table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        s.writeInt(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        // Write out keys and values (alternating)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        for (Entry e = header.after; e != header; e = e.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            s.writeObject(e.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            s.writeObject(e.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Reconstitute the LinkedHashMap from a stream (i.e., deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        // Read in the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        // Read in number of buckets and allocate the bucket array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        int numBuckets = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        table = new Entry[numBuckets];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        header = new Entry(-1, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        header.before = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        header.after = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // Read in size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // Read the keys and values, and put the mappings in the LinkedHashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            Object key = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            Object value = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
}