01: /*
02: * Copyright 2006 Davide Deidda
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: /*
18: * LookupCache.java
19: *
20: * Created on 5 marzo 2005, 15.35
21: */
22:
23: package it.biobytes.ammentos.cache;
24:
25: import it.biobytes.ammentos.*;
26: import java.util.*;
27: import java.util.logging.*;
28:
29: /**
30: *
31: * @author davide
32: */
33: public class LookupCache<T> {
34: private Map<Object, T> m_cache;
35: private Logger m_logger = Logger.getLogger("ammentos");
36:
37: /** Creates a new instance of LookupCache */
38: public LookupCache() {
39: m_cache = new WeakHashMap<Object, T>();
40: }
41:
42: public synchronized void store(T obj) throws PersistenceException {
43: m_cache.put(Ammentos.getPrimaryKeyField(obj.getClass())
44: .get(obj), obj);
45: }
46:
47: public synchronized void removeKey(Object key) {
48: m_logger.info("Lookup Cache in action: removing " + key);
49: T obj = m_cache.remove(key);
50: m_logger.info(" removed key" + key + " from Cache");
51: }
52:
53: public synchronized T lookup(Object key) {
54: return m_cache.get(key);
55: }
56:
57: public synchronized void clear() {
58: m_cache.clear();
59: }
60: }
|