source: trunk/src/gcc/libjava/java/util/LinkedHashMap.java@ 154

Last change on this file since 154 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 15.4 KB
Line 
1/* LinkedHashMap.java -- a class providing hashtable data structure,
2 mapping Object --> Object, with linked list traversal
3 Copyright (C) 2001 Free Software Foundation, Inc.
4
5This file is part of GNU Classpath.
6
7GNU Classpath is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Classpath is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Classpath; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
2002111-1307 USA.
21
22Linking this library statically or dynamically with other modules is
23making a combined work based on this library. Thus, the terms and
24conditions of the GNU General Public License cover the whole
25combination.
26
27As a special exception, the copyright holders of this library give you
28permission to link this library with independent modules to produce an
29executable, regardless of the license terms of these independent
30modules, and to copy and distribute the resulting executable under
31terms of your choice, provided that you also meet, for each linked
32independent module, the terms and conditions of the license of that
33module. An independent module is a module which is not derived from
34or based on this library. If you modify this library, you may extend
35this exception to your version of the library, but you are not
36obligated to do so. If you do not wish to do so, delete this
37exception statement from your version. */
38
39
40package java.util;
41
42/**
43 * This class provides a hashtable-backed implementation of the
44 * Map interface, with predictable traversal order.
45 * <p>
46 *
47 * It uses a hash-bucket approach; that is, hash collisions are handled
48 * by linking the new node off of the pre-existing node (or list of
49 * nodes). In this manner, techniques such as linear probing (which
50 * can cause primary clustering) and rehashing (which does not fit very
51 * well with Java's method of precomputing hash codes) are avoided. In
52 * addition, this maintains a doubly-linked list which tracks either
53 * insertion or access order. Note that the insertion order is not
54 * modified if a <code>put</code> simply reinserts a key in the map.
55 * <p>
56 *
57 * One of the nice features of tracking insertion order is that you can
58 * copy a hashtable, and regardless of the implementation of the original,
59 * produce the same results when iterating over the copy. This is possible
60 * without needing the overhead of <code>TreeMap</code>.
61 * <p>
62 *
63 * When using this {@link #LinkedHashMap(int, float, boolean) constructor},
64 * you build an access-order mapping. This can be used to implement LRU
65 * caches, for example. In this case, every invocation of <code>put</code>,
66 * <code>putAll</code>, or <code>get</code> moves the accessed entry to
67 * the end of the iteration list. By overriding
68 * {@link #removeEldestEntry(Map.Entry)}, you can also control the
69 * removal of the oldest entry, and thereby do things like keep the map
70 * at a fixed size.
71 * <p>
72 *
73 * Under ideal circumstances (no collisions), LinkedHashMap offers O(1)
74 * performance on most operations (<pre>containsValue()</pre> is,
75 * of course, O(n)). In the worst case (all keys map to the same
76 * hash code -- very unlikely), most operations are O(n).
77 * <p>
78 *
79 * LinkedHashMap accepts the null key and null values. It is not
80 * synchronized, so if you need multi-threaded access, consider using:<br>
81 * <code>Map m = Collections.synchronizedMap(new LinkedHashMap(...));</code>
82 * <p>
83 *
84 * The iterators are <i>fail-fast</i>, meaning that any structural
85 * modification, except for <code>remove()</code> called on the iterator
86 * itself, cause the iterator to throw a
87 * {@link ConcurrentModificationException} rather than exhibit
88 * non-deterministic behavior.
89 *
90 * @author Eric Blake <[email protected]>
91 * @see Object#hashCode()
92 * @see Collection
93 * @see Map
94 * @see HashMap
95 * @see TreeMap
96 * @see Hashtable
97 * @since 1.4
98 * @status updated to 1.4
99 */
100public class LinkedHashMap extends HashMap
101{
102 /**
103 * Compatible with JDK 1.4.
104 */
105 private static final long serialVersionUID = 3801124242820219131L;
106
107 /**
108 * The first Entry to iterate over.
109 */
110 transient LinkedHashEntry head;
111
112 /**
113 * The last Entry to iterate over.
114 */
115 transient LinkedHashEntry tail;
116
117 /**
118 * The iteration order of this linked hash map: <code>true</code> for
119 * access-order, <code>false</code> for insertion-order.
120 * @serial
121 */
122 final boolean accessOrder;
123
124 /**
125 * Class to represent an entry in the hash table. Holds a single key-value
126 * pair and the doubly-linked insertion order list.
127 */
128 class LinkedHashEntry extends HashEntry
129 {
130 /** The predecessor in the iteration list, null if this is the eldest. */
131 LinkedHashEntry pred;
132 /** The successor in the iteration list, null if this is the newest. */
133 LinkedHashEntry succ;
134
135 /**
136 * Simple constructor.
137 * @param key the key
138 * @param value the value
139 */
140 LinkedHashEntry(Object key, Object value)
141 {
142 super(key, value);
143 if (head == null)
144 head = this;
145 pred = tail;
146 tail = this;
147 if (pred != null)
148 pred.succ = this;
149 }
150
151 /**
152 * Sets the value of this entry, and shuffles it to the end of
153 * the list if this is in access-order.
154 * @param value the new value
155 * @return the prior value
156 */
157 public Object setValue(Object value)
158 {