KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > SimpleCache


1 package com.ibm.icu.impl;
2
3 import java.lang.ref.SoftReference JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 public class SimpleCache implements ICUCache {
9
10     public Object JavaDoc get(Object JavaDoc key) {
11         SoftReference JavaDoc ref = cacheRef;
12         if (ref != null) {
13             Map JavaDoc map = (Map JavaDoc)ref.get();
14             if (map != null) {
15                 return map.get(key);
16             }
17         }
18         return null;
19     }
20
21     public void put(Object JavaDoc key, Object JavaDoc value) {
22         SoftReference JavaDoc ref = cacheRef;
23         Map JavaDoc map = null;
24         if (ref != null) {
25             map = (Map JavaDoc)ref.get();
26         }
27         if (map == null) {
28             map = Collections.synchronizedMap(new HashMap JavaDoc());
29             ref = new SoftReference JavaDoc(map);
30             cacheRef = ref;
31         }
32         map.put(key, value);
33     }
34
35     public void clear() {
36         cacheRef = null;
37     }
38
39     private SoftReference JavaDoc cacheRef = null;
40 }
41
Popular Tags