File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\SmallLRUCache.java.html

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