01: /*
02: * @(#)MemoryCache.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.util;
10:
11: import pnuts.lang.Runtime;
12: import java.lang.ref.SoftReference;
13: import java.util.WeakHashMap;
14: import java.util.Map;
15:
16: /**
17: * A Cache implementation that uses SoftReference.
18: */
19: public class MemoryCache implements Cache {
20:
21: private Map map;
22:
23: public MemoryCache() {
24: this (Runtime.createWeakMap());
25: }
26:
27: public MemoryCache(Map map) {
28: this .map = map;
29: }
30:
31: public Object get(Object key) {
32: SoftReference ref = (SoftReference) map.get(key);
33: if (ref == null) {
34: return null;
35: } else {
36: return ref.get();
37: }
38: }
39:
40: public Object put(Object key, Object value) {
41: map.put(key, new SoftReference(value));
42: return null;
43: }
44:
45: public void reset() {
46: this .map = new WeakHashMap();
47: }
48: }
|