KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > hash > LinkedHashCache


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.hash;
21
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 /**
29  * @author Tanja Jovanovic
30  *
31  * LinkedHashCache class implements LRU cache for storing objects.
32  * @version 2.0 07.01.2003.
33  *
34  */

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

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

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

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

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

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

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

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

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

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

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

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