jaroslav@557: /* jaroslav@557: * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@557: * jaroslav@557: * This code is free software; you can redistribute it and/or modify it jaroslav@557: * under the terms of the GNU General Public License version 2 only, as jaroslav@557: * published by the Free Software Foundation. Oracle designates this jaroslav@557: * particular file as subject to the "Classpath" exception as provided jaroslav@557: * by Oracle in the LICENSE file that accompanied this code. jaroslav@557: * jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@557: * accompanied this code). jaroslav@557: * jaroslav@557: * You should have received a copy of the GNU General Public License version jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@557: * jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@557: * or visit www.oracle.com if you need additional information or have any jaroslav@557: * questions. jaroslav@557: */ jaroslav@557: jaroslav@557: package java.util; jaroslav@557: import java.io.*; jaroslav@557: jaroslav@557: /** jaroslav@557: *

Hash table and linked list implementation of the Map interface, jaroslav@557: * with predictable iteration order. This implementation differs from jaroslav@557: * HashMap in that it maintains a doubly-linked list running through jaroslav@557: * all of its entries. This linked list defines the iteration ordering, jaroslav@557: * which is normally the order in which keys were inserted into the map jaroslav@557: * (insertion-order). Note that insertion order is not affected jaroslav@557: * if a key is re-inserted into the map. (A key k is jaroslav@557: * reinserted into a map m if m.put(k, v) is invoked when jaroslav@557: * m.containsKey(k) would return true immediately prior to jaroslav@557: * the invocation.) jaroslav@557: * jaroslav@557: *

This implementation spares its clients from the unspecified, generally jaroslav@557: * chaotic ordering provided by {@link HashMap} (and {@link Hashtable}), jaroslav@557: * without incurring the increased cost associated with {@link TreeMap}. It jaroslav@557: * can be used to produce a copy of a map that has the same order as the jaroslav@557: * original, regardless of the original map's implementation: jaroslav@557: *

jaroslav@557:  *     void foo(Map m) {
jaroslav@557:  *         Map copy = new LinkedHashMap(m);
jaroslav@557:  *         ...
jaroslav@557:  *     }
jaroslav@557:  * 
jaroslav@557: * This technique is particularly useful if a module takes a map on input, jaroslav@557: * copies it, and later returns results whose order is determined by that of jaroslav@557: * the copy. (Clients generally appreciate having things returned in the same jaroslav@557: * order they were presented.) jaroslav@557: * jaroslav@557: *

A special {@link #LinkedHashMap(int,float,boolean) constructor} is jaroslav@557: * provided to create a linked hash map whose order of iteration is the order jaroslav@557: * in which its entries were last accessed, from least-recently accessed to jaroslav@557: * most-recently (access-order). This kind of map is well-suited to jaroslav@557: * building LRU caches. Invoking the put or get method jaroslav@557: * results in an access to the corresponding entry (assuming it exists after jaroslav@557: * the invocation completes). The putAll method generates one entry jaroslav@557: * access for each mapping in the specified map, in the order that key-value jaroslav@557: * mappings are provided by the specified map's entry set iterator. No jaroslav@557: * other methods generate entry accesses. In particular, operations on jaroslav@557: * collection-views do not affect the order of iteration of the backing jaroslav@557: * map. jaroslav@557: * jaroslav@557: *

The {@link #removeEldestEntry(Map.Entry)} method may be overridden to jaroslav@557: * impose a policy for removing stale mappings automatically when new mappings jaroslav@557: * are added to the map. jaroslav@557: * jaroslav@557: *

This class provides all of the optional Map operations, and jaroslav@557: * permits null elements. Like HashMap, it provides constant-time jaroslav@557: * performance for the basic operations (add, contains and jaroslav@557: * remove), assuming the hash function disperses elements jaroslav@557: * properly among the buckets. Performance is likely to be just slightly jaroslav@557: * below that of HashMap, due to the added expense of maintaining the jaroslav@557: * linked list, with one exception: Iteration over the collection-views jaroslav@557: * of a LinkedHashMap requires time proportional to the size jaroslav@557: * of the map, regardless of its capacity. Iteration over a HashMap jaroslav@557: * is likely to be more expensive, requiring time proportional to its jaroslav@557: * capacity. jaroslav@557: * jaroslav@557: *

A linked hash map has two parameters that affect its performance: jaroslav@557: * initial capacity and load factor. They are defined precisely jaroslav@557: * as for HashMap. Note, however, that the penalty for choosing an jaroslav@557: * excessively high value for initial capacity is less severe for this class jaroslav@557: * than for HashMap, as iteration times for this class are unaffected jaroslav@557: * by capacity. jaroslav@557: * jaroslav@557: *

Note that this implementation is not synchronized. jaroslav@557: * If multiple threads access a linked hash map concurrently, and at least jaroslav@557: * one of the threads modifies the map structurally, it must be jaroslav@557: * synchronized externally. This is typically accomplished by jaroslav@557: * synchronizing on some object that naturally encapsulates the map. jaroslav@557: * jaroslav@557: * If no such object exists, the map should be "wrapped" using the jaroslav@557: * {@link Collections#synchronizedMap Collections.synchronizedMap} jaroslav@557: * method. This is best done at creation time, to prevent accidental jaroslav@557: * unsynchronized access to the map:

jaroslav@557:  *   Map m = Collections.synchronizedMap(new LinkedHashMap(...));
jaroslav@557: * jaroslav@557: * A structural modification is any operation that adds or deletes one or more jaroslav@557: * mappings or, in the case of access-ordered linked hash maps, affects jaroslav@557: * iteration order. In insertion-ordered linked hash maps, merely changing jaroslav@557: * the value associated with a key that is already contained in the map is not jaroslav@557: * a structural modification. In access-ordered linked hash maps, jaroslav@557: * merely querying the map with get is a structural jaroslav@557: * modification.) jaroslav@557: * jaroslav@557: *

The iterators returned by the iterator method of the collections jaroslav@557: * returned by all of this class's collection view methods are jaroslav@557: * fail-fast: if the map is structurally modified at any time after jaroslav@557: * the iterator is created, in any way except through the iterator's own jaroslav@557: * remove method, the iterator will throw a {@link jaroslav@557: * ConcurrentModificationException}. Thus, in the face of concurrent jaroslav@557: * modification, the iterator fails quickly and cleanly, rather than risking jaroslav@557: * arbitrary, non-deterministic behavior at an undetermined time in the future. jaroslav@557: * jaroslav@557: *

Note that the fail-fast behavior of an iterator cannot be guaranteed jaroslav@557: * as it is, generally speaking, impossible to make any hard guarantees in the jaroslav@557: * presence of unsynchronized concurrent modification. Fail-fast iterators jaroslav@557: * throw ConcurrentModificationException on a best-effort basis. jaroslav@557: * Therefore, it would be wrong to write a program that depended on this jaroslav@557: * exception for its correctness: the fail-fast behavior of iterators jaroslav@557: * should be used only to detect bugs. jaroslav@557: * jaroslav@557: *

This class is a member of the jaroslav@557: * jaroslav@557: * Java Collections Framework. jaroslav@557: * jaroslav@557: * @param the type of keys maintained by this map jaroslav@557: * @param the type of mapped values jaroslav@557: * jaroslav@557: * @author Josh Bloch jaroslav@557: * @see Object#hashCode() jaroslav@557: * @see Collection jaroslav@557: * @see Map jaroslav@557: * @see HashMap jaroslav@557: * @see TreeMap jaroslav@557: * @see Hashtable jaroslav@557: * @since 1.4 jaroslav@557: */ jaroslav@557: jaroslav@557: public class LinkedHashMap jaroslav@557: extends HashMap jaroslav@557: implements Map jaroslav@557: { jaroslav@557: jaroslav@557: private static final long serialVersionUID = 3801124242820219131L; jaroslav@557: jaroslav@557: /** jaroslav@557: * The head of the doubly linked list. jaroslav@557: */ jaroslav@557: private transient Entry header; jaroslav@557: jaroslav@557: /** jaroslav@557: * The iteration ordering method for this linked hash map: true jaroslav@557: * for access-order, false for insertion-order. jaroslav@557: * jaroslav@557: * @serial jaroslav@557: */ jaroslav@557: private final boolean accessOrder; jaroslav@557: jaroslav@557: /** jaroslav@557: * Constructs an empty insertion-ordered LinkedHashMap instance jaroslav@557: * with the specified initial capacity and load factor. jaroslav@557: * jaroslav@557: * @param initialCapacity the initial capacity jaroslav@557: * @param loadFactor the load factor jaroslav@557: * @throws IllegalArgumentException if the initial capacity is negative jaroslav@557: * or the load factor is nonpositive jaroslav@557: */ jaroslav@557: public LinkedHashMap(int initialCapacity, float loadFactor) { jaroslav@557: super(initialCapacity, loadFactor); jaroslav@557: accessOrder = false; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Constructs an empty insertion-ordered LinkedHashMap instance jaroslav@557: * with the specified initial capacity and a default load factor (0.75). jaroslav@557: * jaroslav@557: * @param initialCapacity the initial capacity jaroslav@557: * @throws IllegalArgumentException if the initial capacity is negative jaroslav@557: */ jaroslav@557: public LinkedHashMap(int initialCapacity) { jaroslav@557: super(initialCapacity); jaroslav@557: accessOrder = false; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Constructs an empty insertion-ordered LinkedHashMap instance jaroslav@557: * with the default initial capacity (16) and load factor (0.75). jaroslav@557: */ jaroslav@557: public LinkedHashMap() { jaroslav@557: super(); jaroslav@557: accessOrder = false; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Constructs an insertion-ordered LinkedHashMap instance with jaroslav@557: * the same mappings as the specified map. The LinkedHashMap jaroslav@557: * instance is created with a default load factor (0.75) and an initial jaroslav@557: * capacity sufficient to hold the mappings in the specified map. jaroslav@557: * jaroslav@557: * @param m the map whose mappings are to be placed in this map jaroslav@557: * @throws NullPointerException if the specified map is null jaroslav@557: */ jaroslav@557: public LinkedHashMap(Map m) { jaroslav@557: super(m); jaroslav@557: accessOrder = false; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Constructs an empty LinkedHashMap instance with the jaroslav@557: * specified initial capacity, load factor and ordering mode. jaroslav@557: * jaroslav@557: * @param initialCapacity the initial capacity jaroslav@557: * @param loadFactor the load factor jaroslav@557: * @param accessOrder the ordering mode - true for jaroslav@557: * access-order, false for insertion-order jaroslav@557: * @throws IllegalArgumentException if the initial capacity is negative jaroslav@557: * or the load factor is nonpositive jaroslav@557: */ jaroslav@557: public LinkedHashMap(int initialCapacity, jaroslav@557: float loadFactor, jaroslav@557: boolean accessOrder) { jaroslav@557: super(initialCapacity, loadFactor); jaroslav@557: this.accessOrder = accessOrder; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Called by superclass constructors and pseudoconstructors (clone, jaroslav@557: * readObject) before any entries are inserted into the map. Initializes jaroslav@557: * the chain. jaroslav@557: */ jaroslav@557: void init() { jaroslav@557: header = new Entry<>(-1, null, null, null); jaroslav@557: header.before = header.after = header; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Transfers all entries to new table array. This method is called jaroslav@557: * by superclass resize. It is overridden for performance, as it is jaroslav@557: * faster to iterate using our linked list. jaroslav@557: */ jaroslav@557: void transfer(HashMap.Entry[] newTable) { jaroslav@557: int newCapacity = newTable.length; jaroslav@557: for (Entry e = header.after; e != header; e = e.after) { jaroslav@557: int index = indexFor(e.hash, newCapacity); jaroslav@557: e.next = newTable[index]; jaroslav@557: newTable[index] = e; jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns true if this map maps one or more keys to the jaroslav@557: * specified value. jaroslav@557: * jaroslav@557: * @param value value whose presence in this map is to be tested jaroslav@557: * @return true if this map maps one or more keys to the jaroslav@557: * specified value jaroslav@557: */ jaroslav@557: public boolean containsValue(Object value) { jaroslav@557: // Overridden to take advantage of faster iterator jaroslav@557: if (value==null) { jaroslav@557: for (Entry e = header.after; e != header; e = e.after) jaroslav@557: if (e.value==null) jaroslav@557: return true; jaroslav@557: } else { jaroslav@557: for (Entry e = header.after; e != header; e = e.after) jaroslav@557: if (value.equals(e.value)) jaroslav@557: return true; jaroslav@557: } jaroslav@557: return false; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns the value to which the specified key is mapped, jaroslav@557: * or {@code null} if this map contains no mapping for the key. jaroslav@557: * jaroslav@557: *

More formally, if this map contains a mapping from a key jaroslav@557: * {@code k} to a value {@code v} such that {@code (key==null ? k==null : jaroslav@557: * key.equals(k))}, then this method returns {@code v}; otherwise jaroslav@557: * it returns {@code null}. (There can be at most one such mapping.) jaroslav@557: * jaroslav@557: *

A return value of {@code null} does not necessarily jaroslav@557: * indicate that the map contains no mapping for the key; it's also jaroslav@557: * possible that the map explicitly maps the key to {@code null}. jaroslav@557: * The {@link #containsKey containsKey} operation may be used to jaroslav@557: * distinguish these two cases. jaroslav@557: */ jaroslav@557: public V get(Object key) { jaroslav@557: Entry e = (Entry)getEntry(key); jaroslav@557: if (e == null) jaroslav@557: return null; jaroslav@557: e.recordAccess(this); jaroslav@557: return e.value; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Removes all of the mappings from this map. jaroslav@557: * The map will be empty after this call returns. jaroslav@557: */ jaroslav@557: public void clear() { jaroslav@557: super.clear(); jaroslav@557: header.before = header.after = header; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * LinkedHashMap entry. jaroslav@557: */ jaroslav@557: private static class Entry extends HashMap.Entry { jaroslav@557: // These fields comprise the doubly linked list used for iteration. jaroslav@557: Entry before, after; jaroslav@557: jaroslav@557: Entry(int hash, K key, V value, HashMap.Entry next) { jaroslav@557: super(hash, key, value, next); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Removes this entry from the linked list. jaroslav@557: */ jaroslav@557: private void remove() { jaroslav@557: before.after = after; jaroslav@557: after.before = before; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Inserts this entry before the specified existing entry in the list. jaroslav@557: */ jaroslav@557: private void addBefore(Entry existingEntry) { jaroslav@557: after = existingEntry; jaroslav@557: before = existingEntry.before; jaroslav@557: before.after = this; jaroslav@557: after.before = this; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * This method is invoked by the superclass whenever the value jaroslav@557: * of a pre-existing entry is read by Map.get or modified by Map.set. jaroslav@557: * If the enclosing Map is access-ordered, it moves the entry jaroslav@557: * to the end of the list; otherwise, it does nothing. jaroslav@557: */ jaroslav@557: void recordAccess(HashMap m) { jaroslav@557: LinkedHashMap lm = (LinkedHashMap)m; jaroslav@557: if (lm.accessOrder) { jaroslav@557: lm.modCount++; jaroslav@557: remove(); jaroslav@557: addBefore(lm.header); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: void recordRemoval(HashMap m) { jaroslav@557: remove(); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: private abstract class LinkedHashIterator implements Iterator { jaroslav@557: Entry nextEntry = header.after; jaroslav@557: Entry lastReturned = null; jaroslav@557: jaroslav@557: /** jaroslav@557: * The modCount value that the iterator believes that the backing jaroslav@557: * List should have. If this expectation is violated, the iterator jaroslav@557: * has detected concurrent modification. jaroslav@557: */ jaroslav@557: int expectedModCount = modCount; jaroslav@557: jaroslav@557: public boolean hasNext() { jaroslav@557: return nextEntry != header; jaroslav@557: } jaroslav@557: jaroslav@557: public void remove() { jaroslav@557: if (lastReturned == null) jaroslav@557: throw new IllegalStateException(); jaroslav@557: if (modCount != expectedModCount) jaroslav@557: throw new ConcurrentModificationException(); jaroslav@557: jaroslav@557: LinkedHashMap.this.remove(lastReturned.key); jaroslav@557: lastReturned = null; jaroslav@557: expectedModCount = modCount; jaroslav@557: } jaroslav@557: jaroslav@557: Entry nextEntry() { jaroslav@557: if (modCount != expectedModCount) jaroslav@557: throw new ConcurrentModificationException(); jaroslav@557: if (nextEntry == header) jaroslav@557: throw new NoSuchElementException(); jaroslav@557: jaroslav@557: Entry e = lastReturned = nextEntry; jaroslav@557: nextEntry = e.after; jaroslav@557: return e; jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: private class KeyIterator extends LinkedHashIterator { jaroslav@557: public K next() { return nextEntry().getKey(); } jaroslav@557: } jaroslav@557: jaroslav@557: private class ValueIterator extends LinkedHashIterator { jaroslav@557: public V next() { return nextEntry().value; } jaroslav@557: } jaroslav@557: jaroslav@557: private class EntryIterator extends LinkedHashIterator> { jaroslav@557: public Map.Entry next() { return nextEntry(); } jaroslav@557: } jaroslav@557: jaroslav@557: // These Overrides alter the behavior of superclass view iterator() methods jaroslav@557: Iterator newKeyIterator() { return new KeyIterator(); } jaroslav@557: Iterator newValueIterator() { return new ValueIterator(); } jaroslav@557: Iterator> newEntryIterator() { return new EntryIterator(); } jaroslav@557: jaroslav@557: /** jaroslav@557: * This override alters behavior of superclass put method. It causes newly jaroslav@557: * allocated entry to get inserted at the end of the linked list and jaroslav@557: * removes the eldest entry if appropriate. jaroslav@557: */ jaroslav@557: void addEntry(int hash, K key, V value, int bucketIndex) { jaroslav@557: createEntry(hash, key, value, bucketIndex); jaroslav@557: jaroslav@557: // Remove eldest entry if instructed, else grow capacity if appropriate jaroslav@557: Entry eldest = header.after; jaroslav@557: if (removeEldestEntry(eldest)) { jaroslav@557: removeEntryForKey(eldest.key); jaroslav@557: } else { jaroslav@557: if (size >= threshold) jaroslav@557: resize(2 * table.length); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * This override differs from addEntry in that it doesn't resize the jaroslav@557: * table or remove the eldest entry. jaroslav@557: */ jaroslav@557: void createEntry(int hash, K key, V value, int bucketIndex) { jaroslav@557: HashMap.Entry old = table[bucketIndex]; jaroslav@557: Entry e = new Entry<>(hash, key, value, old); jaroslav@557: table[bucketIndex] = e; jaroslav@557: e.addBefore(header); jaroslav@557: size++; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns true if this map should remove its eldest entry. jaroslav@557: * This method is invoked by put and putAll after jaroslav@557: * inserting a new entry into the map. It provides the implementor jaroslav@557: * with the opportunity to remove the eldest entry each time a new one jaroslav@557: * is added. This is useful if the map represents a cache: it allows jaroslav@557: * the map to reduce memory consumption by deleting stale entries. jaroslav@557: * jaroslav@557: *

Sample use: this override will allow the map to grow up to 100 jaroslav@557: * entries and then delete the eldest entry each time a new entry is jaroslav@557: * added, maintaining a steady state of 100 entries. jaroslav@557: *

jaroslav@557:      *     private static final int MAX_ENTRIES = 100;
jaroslav@557:      *
jaroslav@557:      *     protected boolean removeEldestEntry(Map.Entry eldest) {
jaroslav@557:      *        return size() > MAX_ENTRIES;
jaroslav@557:      *     }
jaroslav@557:      * 
jaroslav@557: * jaroslav@557: *

This method typically does not modify the map in any way, jaroslav@557: * instead allowing the map to modify itself as directed by its jaroslav@557: * return value. It is permitted for this method to modify jaroslav@557: * the map directly, but if it does so, it must return jaroslav@557: * false (indicating that the map should not attempt any jaroslav@557: * further modification). The effects of returning true jaroslav@557: * after modifying the map from within this method are unspecified. jaroslav@557: * jaroslav@557: *

This implementation merely returns false (so that this jaroslav@557: * map acts like a normal map - the eldest element is never removed). jaroslav@557: * jaroslav@557: * @param eldest The least recently inserted entry in the map, or if jaroslav@557: * this is an access-ordered map, the least recently accessed jaroslav@557: * entry. This is the entry that will be removed it this jaroslav@557: * method returns true. If the map was empty prior jaroslav@557: * to the put or putAll invocation resulting jaroslav@557: * in this invocation, this will be the entry that was just jaroslav@557: * inserted; in other words, if the map contains a single jaroslav@557: * entry, the eldest entry is also the newest. jaroslav@557: * @return true if the eldest entry should be removed jaroslav@557: * from the map; false if it should be retained. jaroslav@557: */ jaroslav@557: protected boolean removeEldestEntry(Map.Entry eldest) { jaroslav@557: return false; jaroslav@557: } jaroslav@557: }