source: josm/trunk/src/org/openstreetmap/josm/gui/util/LruCache.java

Last change on this file was 19519, checked in by stoecker, 2 months ago

unify eol-style

  • Property svn:eol-style set to native
File size: 1.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import java.util.LinkedHashMap;
5import java.util.Map;
6import java.util.Objects;
7
8/**
9 * LRU cache (least recently used)
10 * @param <K> the type of keys maintained by this map
11 * @param <V> the type of mapped values
12 * @see <a href="http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html">
13 * Java Planet: How to set up a simple LRU cache using LinkedHashMap</a>
14 */
15public final class LruCache<K, V> extends LinkedHashMap<K, V> {
16 private static final long serialVersionUID = 1L;
17 private final int capacity;
18
19 /**
20 * Constructs an empty {@code LruCache} instance with the given capacity
21 * @param capacity the capacity
22 */
23 public LruCache(int capacity) {
24 super(capacity + 1, 1.1f, true);
25 this.capacity = capacity;
26 }
27
28 @Override
29 protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
30 return size() > capacity;
31 }
32
33 @Override
34 public boolean equals(Object o) {
35 if (this == o) return true;
36 if (o == null || getClass() != o.getClass()) return false;
37 if (!super.equals(o)) return false;
38 LruCache<?, ?> lruCache = (LruCache<?, ?>) o;
39 return capacity == lruCache.capacity;
40 }
41
42 @Override
43 public int hashCode() {
44 return Objects.hash(super.hashCode(), capacity);
45 }
46}
Note: See TracBrowser for help on using the repository browser.