KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > lru > LRUCache


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  */

20 package org.enhydra.dods.cache.lru;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Collection JavaDoc;
27 import org.enhydra.dods.util.LRUMap;
28
29 /**
30  * @author Tanja Jovanovic
31  *
32  * LRUCache class implements LRU cache for storing objects.
33  * @version 2.0 07.01.2003.
34  *
35  */

36  
37 public class LRUCache implements Map JavaDoc {
38
39     private Map JavaDoc impl;
40     private static Map JavaDoc createMap(int initialCapacity, float loadFactor) {
41         Map JavaDoc ret = null;
42         try {
43             ret = (Map JavaDoc) Class.forName("java.util.LinkedHashMap")
44                 .getConstructor(new Class JavaDoc[] {int.class, float.class, boolean.class})
45                 .newInstance(new Object JavaDoc[] {new Integer JavaDoc(initialCapacity), new Float JavaDoc(loadFactor), new Boolean JavaDoc(true)});
46         } catch (Throwable JavaDoc t) {
47             ret = new LRUMap(initialCapacity, loadFactor, -1);
48         }
49         return ret;
50     }
51     /**
52      * Maximal number of objects in LRUCache - 1024 is default value of this
53      * variable.
54      */

55     protected int maxEntries = 1024;
56     
57     /**
58      * Constructor (int, float, int).
59      * Constructs an empty access-ordered <tt>LRUCache</tt> instance with the
60      * specified initial capacity, load factor and maximal number of
61      * objects.
62      *
63      * @param initialCapacity The initial capacity.
64      * @param loadFactor The load factor.
65      * @param maxEnt Maximal number of objects in LRUCache.
66      */

67     public LRUCache(int initialCapacity, float loadFactor, int maxEnt) {
68         impl = createMap(initialCapacity, loadFactor);
69         maxEntries = maxEnt;
70     }
71     
72     /**
73      * Constructor (int, float).
74      * Constructs an empty access-ordered <tt>LRUCache</tt> instance with the
75      * specified initial capacity and load factor.
76      *
77      * @param initialCapacity The initial capacity.
78      * @param loadFactor The load factor.
79      */

80     public LRUCache(int initialCapacity, float loadFactor) {
81         impl = createMap(initialCapacity, loadFactor);
82     }
83    
84     /**
85      * Constructor (int).
86      * Constructs an empty access-ordered <tt>LRUCache</tt> instance with a
87      * default capacity (16) and load factor (0.75) and with maximal number of
88      * objects.
89      *
90      * @param maxEnt Maximal number of objects in LRUCache.
91      */

92     public LRUCache(int maxEnt) {
93         impl = createMap(16, (float) 0.75);
94         maxEntries = maxEnt;
95     }
96     
97     /**
98      * Constructor ().
99      * Constructs an empty access-ordered <tt>LRUCache</tt> instance with a
100      * default capacity (16) and load factor (0.75).
101      */

102     public LRUCache() {
103         impl = createMap(16, (float) 0.75);
104     }
105
106     /**
107      * Constructor (Map, int).
108      * Constructs an access-ordered <tt>LRUCache</tt> instance with the same
109      * mappings as the specified map. The <tt>LRUCache</tt> instance is created
110      * with maximal number of objects, default load factor (0.75) and an initial
111      * capacity sufficient to hold the mappings in the specified map.
112      *
113      * @param m The map whose mappings are to be placed in this map.
114      * @param maxEnt Maximal number of objects in LRUCache.
115      */

116     public LRUCache(Map JavaDoc m, int maxEnt) {
117         impl = createMap(16, (float) 0.75);
118         maxEntries = maxEnt;
119         impl.putAll(m);
120     }
121     
122     /**
123      * Constructor (Map).
124      * Constructs an access-ordered <tt>LRUCache</tt> instance with the same
125      * mappings as the specified map. The <tt>LRUCache</tt> instance is created
126      * with default load factor (0.75) and an initial capacity sufficient to
127      * hold the mappings in the specified map.
128      *
129      * @param m The map whose mappings are to be placed in this map.
130      */

131     public LRUCache(Map JavaDoc m) {
132         impl = createMap(16, (float) 0.75);
133         impl.putAll(m);
134     }
135
136     /**
137      * Adds a pair (key, value) to the cache. If key already exists, the method
138      * returns previous value asociated with this key. If maximum number of
139      * objects is achieved, the eldest entry will be removed and returned.
140      *
141      * @param key Key asociated with the value.
142      * @param value Value that will be added to the cache.
143      * @return Previous value asociated with this key, if the key already
144      * exists, or the eldest entry which is removed, if maximum number of
145      * objects is achieved, otherwise null.
146      */

147     public Object JavaDoc add(Object JavaDoc key, Object JavaDoc value) {
148         Object JavaDoc ret = impl.put(key, value);
149
150         if (ret == null) {
151             if (maxEntries < size() && maxEntries > 0) {
152                 Iterator JavaDoc iter = impl.keySet().iterator();
153                 Object JavaDoc rem = iter.next();
154
155                 ret = impl.remove(rem);
156             }
157         }
158         return ret;
159     }
160
161     /**
162      * Returns maximal number of objects in LRUCache.
163      *
164      * @return Maximal number of objects in LRUCache.
165      */

166     public int getMaxEntries() {
167         return maxEntries;
168     }
169     
170     /**
171      * Sets maximal number of objects in LRUCache.
172      *
173      * @param max New value of maximal number of objects in LRUCache.
174      */

175     public void setMaxEntries(int max) {
176         if (max < size() && max > 0) {
177             Iterator JavaDoc iter = (new HashSet JavaDoc(impl.keySet())).iterator();
178             int dif = size() - max;
179
180             for (int i = 0; i < dif; i++) {
181                 impl.remove(iter.next());
182             }
183         }
184         maxEntries = max;
185     }
186
187     /**
188      * For debug reason.
189      */

190     public String JavaDoc toString() {
191         Object JavaDoc[] v = impl.entrySet().toArray();
192         String JavaDoc ret = "LRU content:\n";
193
194         for (int i = 0; i < v.length; i++) {
195             Map.Entry JavaDoc map = (Map.Entry JavaDoc) v[i];
196
197             ret += i + ". key = " + map.getKey() + " value = " + map.getValue()
198                     + "\n";
199         }
200         return ret;
201     }
202
203     public int size() {
204         return impl.size();
205     }
206     public boolean isEmpty() {
207         return impl.isEmpty();
208     }
209     public boolean containsKey(Object JavaDoc key) throws ClassCastException JavaDoc, NullPointerException JavaDoc {
210         return impl.containsKey(key);
211     }
212     public boolean containsValue(Object JavaDoc value) throws ClassCastException JavaDoc, NullPointerException JavaDoc {
213         return impl.containsValue(value);
214     }
215     public synchronized Object JavaDoc get(Object JavaDoc key) throws ClassCastException JavaDoc, NullPointerException JavaDoc {
216         return impl.get(key);
217     }
218     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) throws ClassCastException JavaDoc, NullPointerException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
219         return impl.put(key, value);
220     }
221     public synchronized Object JavaDoc remove(Object JavaDoc key) throws ClassCastException JavaDoc, NullPointerException JavaDoc, UnsupportedOperationException JavaDoc {
222         return impl.remove(key);
223     }
224     public void putAll(Map JavaDoc t) throws ClassCastException JavaDoc, NullPointerException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
225         impl.putAll(t);
226     }
227     public void clear() throws UnsupportedOperationException JavaDoc {
228         impl.clear();
229     }
230     public Set JavaDoc keySet() {
231         return impl.keySet();
232     }
233     public Collection JavaDoc values() {
234         return impl.values();
235     }
236     public Set JavaDoc entrySet() {
237       return impl.entrySet();
238     }
239     public boolean equals(Object JavaDoc o) {
240         return (o instanceof LRUCache) && impl.equals(((LRUCache)o).impl);
241     }
242     public int hashCode() {
243         return impl.hashCode();
244     }
245 }
246
Popular Tags