KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > LRUCache


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import java.util.LinkedHashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 /**
23  * A Simple LRU Cache
24  *
25  * @version $Revision$
26  */

27
28 public class LRUCache<K, V> extends LinkedHashMap JavaDoc<K, V> {
29     private static final long serialVersionUID=-342098639681884413L;
30     protected int maxCacheSize=10000;
31
32     
33     /**
34      * Constructs LRU Cache
35      *
36      */

37     public LRUCache(){
38         super(1000,0.75f,true);
39     }
40     
41     /**
42      * Constructs an empty <tt>LRUCache</tt> instance with the
43      * specified initial capacity, maximumCacheSize,load factor and ordering mode.
44      *
45      * @param initialCapacity the initial capacity.
46      * @param maximumCacheSize
47      * @param loadFactor the load factor.
48      * @param accessOrder the ordering mode - <tt>true</tt> for
49      * access-order, <tt>false</tt> for insertion-order.
50      * @throws IllegalArgumentException if the initial capacity is negative
51      * or the load factor is nonpositive.
52      */

53     
54     public LRUCache(int initialCapacity,int maximumCacheSize,float loadFactor, boolean accessOrder) {
55         super(initialCapacity,loadFactor,accessOrder);
56         this.maxCacheSize = maximumCacheSize;
57     }
58
59     
60
61     /**
62      * @return Returns the maxCacheSize.
63      */

64     public int getMaxCacheSize(){
65         return maxCacheSize;
66     }
67
68     /**
69      * @param maxCacheSize
70      * The maxCacheSize to set.
71      */

72     public void setMaxCacheSize(int maxCacheSize){
73         this.maxCacheSize=maxCacheSize;
74     }
75     
76     protected boolean removeEldestEntry(Map.Entry JavaDoc entry){
77         return size() > maxCacheSize;
78     }
79 }
80
Popular Tags