KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > LRUMap


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.util.Map JavaDoc;
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 JavaDoc<K, V> {
28     private static final float DEFAULT_LOAD_FACTOR = 0.75f;
29     private int size;
30
31     public LRUMap(int size) {
32         super(size, DEFAULT_LOAD_FACTOR, true);
33         this.size = size;
34     }
35
36     protected boolean removeEldestEntry(Map.Entry JavaDoc<K, V> eldest) {
37         boolean remove = size() > size;
38         if (remove) {
39             onEldestEntryRemove(eldest);
40         }
41         return remove;
42     }
43
44     /**
45      * Invoked when eldest entry is about to be removed.
46      *
47      * @param eldest eldest entry.
48      */

49     protected void onEldestEntryRemove(Map.Entry JavaDoc<K, V> eldest) {
50     }
51
52 }
53
Popular Tags