KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > util > CacheManager


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * CacheManager.java
25  *
26  * Created on 17 septembre 2002, 16:15
27  */

28
29 package org.xquark.mapper.util;
30
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * This cache manager mixes use counters and LRU
36  *
37  */

38 public class CacheManager
39 {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42
43     private CounterMap usedObjects = new CounterMap();
44     private LRU cache;
45     private int cacheSize;
46     
47     /** Creates a new instance of CacheManager */
48     public CacheManager(int cacheSize)
49     {
50         this.cacheSize = cacheSize;
51         cache = new LRU(cacheSize);
52     }
53     
54     public Object JavaDoc checkOut(Object JavaDoc key)
55     {
56         Object JavaDoc o = usedObjects.checkOut(key);
57         
58         // look if it's not in the LRU
59
if (o == null)
60         {
61             o = cache.get(key);
62             // if so, move it from the cache to the used objects
63
if (o != null)
64                 use(key, o);
65         }
66         return o;
67     }
68     
69     /* Object is kept in memory till released */
70     public void use(Object JavaDoc key, Object JavaDoc o)
71     {
72         usedObjects.put(key, o);
73
74         int lruSize = cacheSize - usedObjects.size();
75         if (lruSize >= 0)
76             cache.setMaxSize(lruSize);
77     }
78     
79     /** Object is released and possibly sent to LRU is counter is 0
80      * @return the object that was removed from the cache if no space is left.
81      */

82     public Object JavaDoc release(Object JavaDoc key)
83     {
84         Object JavaDoc removed = usedObjects.remove(key);
85         
86         // put it in LRU if no "strong" use
87
if (removed != null)
88         {
89             cache.setMaxSize(cacheSize - usedObjects.size());
90             removed = cache.put(key, removed);
91         }
92         return removed;
93     }
94     
95     /* puts directly in the LRU (no release expected) */
96     public void put(Object JavaDoc key, Object JavaDoc o)
97     {
98         cache.put(key, o);
99     }
100     
101     /* look 1st in the used objects and then in the cache */
102     public Object JavaDoc get(Object JavaDoc key)
103     {
104         Object JavaDoc o = usedObjects.get(key);
105         
106         if (o == null)
107             o = cache.get(key);
108         return o;
109     }
110     
111     public boolean contains(Object JavaDoc key)
112     {
113         return get(key) != null;
114     }
115     
116     public int size()
117     {
118         return usedObjects.size() + cache.size();
119     }
120     
121     public int getCacheSize()
122     {
123         return cacheSize;
124     }
125     
126     public void clear()
127     {
128         usedObjects.clear();
129         cache.clear();
130     }
131     
132     public boolean used()
133     {
134         return usedObjects.size() > 0;
135     }
136     
137     public boolean used(Object JavaDoc key)
138     {
139         return usedObjects.containsKey(key);
140     }
141     
142     public void setKey(Object JavaDoc oldKey, Object JavaDoc newKey)
143     {
144         usedObjects.setKey(oldKey, newKey);
145         cache.setKey(oldKey, newKey);
146     }
147     
148     public Object JavaDoc remove(Object JavaDoc key)
149     {
150         Object JavaDoc removed = cache.removeObject(key);
151         
152         removed = usedObjects.removeAll(key);
153         if (removed != null)
154             cache.setMaxSize(cacheSize - usedObjects.size());
155         return removed;
156     }
157     
158     public Iterator JavaDoc iterator()
159     {
160         Collection JavaDoc objects = usedObjects.values();
161         objects.addAll(cache.values());
162         return objects.iterator();
163     }
164 }
165
Popular Tags