01: /*
02: * Copyright 2006-2007 The Scriptella Project Team.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package scriptella.util;
17:
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20:
21: /**
22: * Represents LRU Map implementation based on {@link java.util.LinkedHashMap}.
23: *
24: * @author Fyodor Kupolov
25: * @version 1.0
26: */
27: public class LRUMap<K, V> extends LinkedHashMap<K, V> {
28: private static final float DEFAULT_LOAD_FACTOR = 0.75f;
29: private static final long serialVersionUID = 1;
30: private int size;
31:
32: public LRUMap(int size) {
33: super (size, DEFAULT_LOAD_FACTOR, true);
34: this .size = size;
35: }
36:
37: protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
38: boolean remove = size() > size;
39: if (remove) {
40: onEldestEntryRemove(eldest);
41: }
42: return remove;
43: }
44:
45: /**
46: * Invoked when eldest entry is about to be removed.
47: *
48: * @param eldest eldest entry.
49: */
50: protected void onEldestEntryRemove(Map.Entry<K, V> eldest) {
51: }
52:
53: }
|