229 boolean accessOrder) {
230 super(initialCapacity, loadFactor);
231 this.accessOrder = accessOrder;
232 }
233
234 /**
235 * Called by superclass constructors and pseudoconstructors (clone,
236 * readObject) before any entries are inserted into the map. Initializes
237 * the chain.
238 */
239 void init() {
240 header = new Entry<>(-1, null, null, null);
241 header.before = header.after = header;
242 }
243
244 /**
245 * Transfers all entries to new table array. This method is called
246 * by superclass resize. It is overridden for performance, as it is
247 * faster to iterate using our linked list.
248 */
249 void transfer(HashMap.Entry[] newTable) {
250 int newCapacity = newTable.length;
251 for (Entry<K,V> e = header.after; e != header; e = e.after) {
252 int index = indexFor(e.hash, newCapacity);
253 e.next = newTable[index];
254 newTable[index] = e;
255 }
256 }
257
258
259 /**
260 * Returns <tt>true</tt> if this map maps one or more keys to the
261 * specified value.
262 *
263 * @param value value whose presence in this map is to be tested
264 * @return <tt>true</tt> if this map maps one or more keys to the
265 * specified value
266 */
267 public boolean containsValue(Object value) {
268 // Overridden to take advantage of faster iterator
269 if (value==null) {
270 for (Entry e = header.after; e != header; e = e.after)
271 if (e.value==null)
272 return true;
273 } else {
274 for (Entry e = header.after; e != header; e = e.after)
275 if (value.equals(e.value))
276 return true;
277 }
278 return false;
279 }
280
281 /**
282 * Returns the value to which the specified key is mapped,
283 * or {@code null} if this map contains no mapping for the key.
284 *
285 * <p>More formally, if this map contains a mapping from a key
286 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
287 * key.equals(k))}, then this method returns {@code v}; otherwise
288 * it returns {@code null}. (There can be at most one such mapping.)
289 *
290 * <p>A return value of {@code null} does not <i>necessarily</i>
291 * indicate that the map contains no mapping for the key; it's also
292 * possible that the map explicitly maps the key to {@code null}.
293 * The {@link #containsKey containsKey} operation may be used to
294 * distinguish these two cases.
420 * removes the eldest entry if appropriate.
421 */
422 void addEntry(int hash, K key, V value, int bucketIndex) {
423 createEntry(hash, key, value, bucketIndex);
424
425 // Remove eldest entry if instructed, else grow capacity if appropriate
426 Entry<K,V> eldest = header.after;
427 if (removeEldestEntry(eldest)) {
428 removeEntryForKey(eldest.key);
429 } else {
430 if (size >= threshold)
431 resize(2 * table.length);
432 }
433 }
434
435 /**
436 * This override differs from addEntry in that it doesn't resize the
437 * table or remove the eldest entry.
438 */
439 void createEntry(int hash, K key, V value, int bucketIndex) {
440 HashMap.Entry<K,V> old = table[bucketIndex];
441 Entry<K,V> e = new Entry<>(hash, key, value, old);
442 table[bucketIndex] = e;
443 e.addBefore(header);
444 size++;
445 }
446
447 /**
448 * Returns <tt>true</tt> if this map should remove its eldest entry.
449 * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
450 * inserting a new entry into the map. It provides the implementor
451 * with the opportunity to remove the eldest entry each time a new one
452 * is added. This is useful if the map represents a cache: it allows
453 * the map to reduce memory consumption by deleting stale entries.
454 *
455 * <p>Sample use: this override will allow the map to grow up to 100
456 * entries and then delete the eldest entry each time a new entry is
457 * added, maintaining a steady state of 100 entries.
458 * <pre>
459 * private static final int MAX_ENTRIES = 100;
460 *
|
229 boolean accessOrder) {
230 super(initialCapacity, loadFactor);
231 this.accessOrder = accessOrder;
232 }
233
234 /**
235 * Called by superclass constructors and pseudoconstructors (clone,
236 * readObject) before any entries are inserted into the map. Initializes
237 * the chain.
238 */
239 void init() {
240 header = new Entry<>(-1, null, null, null);
241 header.before = header.after = header;
242 }
243
244 /**
245 * Transfers all entries to new table array. This method is called
246 * by superclass resize. It is overridden for performance, as it is
247 * faster to iterate using our linked list.
248 */
249 @SuppressWarnings("unchecked")
250 void transfer(HashMap.Entry[] newTable) {
251 int newCapacity = newTable.length;
252 for (Entry<K,V> e = header.after; e != header; e = e.after) {
253 int index = indexFor(e.hash, newCapacity);
254 e.next = (HashMap.Entry<K,V>)newTable[index];
255 newTable[index] = e;
256 }
257 }
258
259
260 /**
261 * Returns <tt>true</tt> if this map maps one or more keys to the
262 * specified value.
263 *
264 * @param value value whose presence in this map is to be tested
265 * @return <tt>true</tt> if this map maps one or more keys to the
266 * specified value
267 */
268 public boolean containsValue(Object value) {
269 // Overridden to take advantage of faster iterator
270 if (value==null) {
271 for (Entry<?,?> e = header.after; e != header; e = e.after)
272 if (e.value==null)
273 return true;
274 } else {
275 for (Entry<?,?> e = header.after; e != header; e = e.after)
276 if (value.equals(e.value))
277 return true;
278 }
279 return false;
280 }
281
282 /**
283 * Returns the value to which the specified key is mapped,
284 * or {@code null} if this map contains no mapping for the key.
285 *
286 * <p>More formally, if this map contains a mapping from a key
287 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
288 * key.equals(k))}, then this method returns {@code v}; otherwise
289 * it returns {@code null}. (There can be at most one such mapping.)
290 *
291 * <p>A return value of {@code null} does not <i>necessarily</i>
292 * indicate that the map contains no mapping for the key; it's also
293 * possible that the map explicitly maps the key to {@code null}.
294 * The {@link #containsKey containsKey} operation may be used to
295 * distinguish these two cases.
421 * removes the eldest entry if appropriate.
422 */
423 void addEntry(int hash, K key, V value, int bucketIndex) {
424 createEntry(hash, key, value, bucketIndex);
425
426 // Remove eldest entry if instructed, else grow capacity if appropriate
427 Entry<K,V> eldest = header.after;
428 if (removeEldestEntry(eldest)) {
429 removeEntryForKey(eldest.key);
430 } else {
431 if (size >= threshold)
432 resize(2 * table.length);
433 }
434 }
435
436 /**
437 * This override differs from addEntry in that it doesn't resize the
438 * table or remove the eldest entry.
439 */
440 void createEntry(int hash, K key, V value, int bucketIndex) {
441 @SuppressWarnings("unchecked")
442 HashMap.Entry<K,V> old = (HashMap.Entry<K,V>)table[bucketIndex];
443 Entry<K,V> e = new Entry<>(hash, key, value, old);
444 table[bucketIndex] = e;
445 e.addBefore(header);
446 size++;
447 }
448
449 /**
450 * Returns <tt>true</tt> if this map should remove its eldest entry.
451 * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
452 * inserting a new entry into the map. It provides the implementor
453 * with the opportunity to remove the eldest entry each time a new one
454 * is added. This is useful if the map represents a cache: it allows
455 * the map to reduce memory consumption by deleting stale entries.
456 *
457 * <p>Sample use: this override will allow the map to grow up to 100
458 * entries and then delete the eldest entry each time a new entry is
459 * added, maintaining a steady state of 100 entries.
460 * <pre>
461 * private static final int MAX_ENTRIES = 100;
462 *
|