Debian Codegraph @

src/main/org/h2/util/SmallLRUCache.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import java.util.LinkedHashMap;
9 import java.util.Map;
10
11 /**
12 * This class implements a small LRU object cache.
13 *
14 * @param <K> the key
15 * @param <V> the value
16 */
17 public class SmallLRUCache<K, V> extends LinkedHashMap<K, V> {
18
19 private static final long serialVersionUID = 1L;
20 private int size;
21
22 private SmallLRUCache(int size) {
23 super(size, (float) 0.75, true);
24 this.size = size;
25 }
26
27 /**
28 * Create a new object with all elements of the given collection.
29 *
30 * @param <K> the key type
31 * @param <V> the value type
32 * @param size the number of elements
33 * @return the object
34 */
35 public static <K, V> SmallLRUCache<K, V> newInstance(int size) {
36 return new SmallLRUCache<>(size);
37 }
38
39 public void setMaxSize(int size) {
40 this.size = size;
41 }
42
43 @Override
44 protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
45 return size() > size;
46 }
47
48 }