KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > cache > GenericCache


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library 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 library 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 library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.cache;
25
26 import org.objectweb.jalisto.se.api.cache.JalistoCache;
27 import org.objectweb.jalisto.se.impl.trace.Trace;
28
29 import java.io.PrintStream JavaDoc;
30 import java.util.*;
31
32 public class GenericCache implements JalistoCache {
33
34     public GenericCache() {
35         table = new HashMap();
36     }
37
38     public void init(int size, String JavaDoc name, double clearPourcent) {
39         this.maxSize = size;
40         this.name = name;
41         this.cacheClearPourcent = clearPourcent;
42     }
43
44     public void setTrace(Trace trace) {
45         this.trace = trace;
46     }
47
48     public boolean isEmpty() {
49         return table.isEmpty();
50     }
51
52     public void clear() {
53         trace.println(Trace.CACHE, "{0} : clear()", name);
54         table.clear();
55         firstCell = null;
56         lastCell = null;
57     }
58
59     public boolean containsKey(Object JavaDoc key) {
60         return table.containsKey(key);
61     }
62
63     public boolean containsValue(Object JavaDoc value) {
64         throw new UnsupportedOperationException JavaDoc("not used in this implementation");
65     }
66
67     public Set entrySet() {
68         throw new UnsupportedOperationException JavaDoc("not used in this implementation");
69     }
70
71     public Object JavaDoc get(Object JavaDoc key) {
72         trace.println(Trace.CACHE, "{0} : get({1})", name, key);
73         Object JavaDoc result = table.get(key);
74         if (result != null) {
75             return ((CacheCell) result).getObject();
76         }
77         return null;
78     }
79
80     public Set keySet() {
81         return table.keySet();
82     }
83
84     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
85         trace.println(Trace.CACHE, "{0} : put({1}, {2})", name, key, value);
86         if (containsKey(key)) {
87             ((CacheCell) table.get(key)).setObject(value);
88         } else {
89             trace.println(Trace.CACHE, "{0} : put -> add");
90             if (table.size() >= maxSize) {
91                 makeSpace();
92             }
93             CacheCell availableCacheCell = CacheCell.newInstance(key, value);
94             if (lastCell != null) {
95                 lastCell.setNextCell(availableCacheCell);
96                 availableCacheCell.setPrecedingCell(lastCell);
97                 lastCell = availableCacheCell;
98             } else {
99                 lastCell = availableCacheCell;
100                 firstCell = availableCacheCell;
101             }
102             table.put(key, availableCacheCell);
103         }
104         return null;
105     }
106
107     public void putAll(Map map) {
108         Iterator keys = map.keySet().iterator();
109         while (keys.hasNext()) {
110             Object JavaDoc key = keys.next();
111             put(key, map.get(key));
112         }
113     }
114
115     public Object JavaDoc remove(Object JavaDoc key) {
116         trace.println(Trace.CACHE, "{0} : remove({1})", name, key);
117         if (containsKey(key)) {
118             synchronized (table) {
119                 CacheCell cell = (CacheCell) table.remove(key);
120                 if (cell.equals(firstCell)) {
121                     firstCell = cell.getNextCell();
122                 }
123                 if (cell.equals(lastCell)) {
124                     lastCell = cell.getPrecedingCell();
125                 }
126                 Object JavaDoc result = cell.getObject();
127                 cell.delink();
128                 if (table.isEmpty()) {
129                     lastCell = null;
130                     firstCell = null;
131                 }
132                 return result;
133             }
134         }
135         return null;
136     }
137
138     public int size() {
139         return table.size();
140     }
141
142     public synchronized void setMaxSize(int size) {
143         maxSize = size;
144         CacheCell myFirst = firstCell;
145         synchronized (table) {
146             while (table.size() > maxSize) {
147                 CacheCell mySecond = myFirst.getNextCell();
148                 table.remove(myFirst.getId());
149                 myFirst.delink();
150                 myFirst = mySecond;
151             }
152             if (table.isEmpty()) {
153                 lastCell = null;
154                 firstCell = null;
155             }
156             firstCell = myFirst;
157         }
158     }
159
160     public int getMaxSize() {
161         return maxSize;
162     }
163
164     public Collection values() {
165         throw new UnsupportedOperationException JavaDoc("not used in this implementation");
166     }
167
168
169     private void makeSpace() {
170         int nbrToRemove = (new Double JavaDoc(maxSize * cacheClearPourcent)).intValue();
171         trace.println(Trace.CACHE, "{0} : makeSpace() : clear {1} objects", name, new Integer JavaDoc(nbrToRemove));
172
173         CacheCell myFirst = firstCell;
174         synchronized (table) {
175             for (int i = 0; i < nbrToRemove; i++) {
176                 CacheCell mySecond = myFirst.getNextCell();
177                 if (mySecond == null) {
178                     throw new NullPointerException JavaDoc();
179                 }
180                 table.remove(myFirst.getId());
181                 myFirst.delink();
182                 myFirst = mySecond;
183             }
184         }
185         firstCell = myFirst;
186     }
187
188     public void print(PrintStream JavaDoc out) {
189         out.println(table.toString());
190     }
191
192     public String JavaDoc toString() {
193         return table.toString();
194     }
195
196
197     private CacheCell firstCell;
198     private CacheCell lastCell;
199     private HashMap table;
200     private String JavaDoc name;
201     private double cacheClearPourcent;
202     private int maxSize;
203     private Trace trace;
204 }
205
Popular Tags