KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * *****************************************************************************
3  * Copyright (C) 2006, International Business Machines Corporation and others.
4  * All Rights Reserved.
5  * *****************************************************************************
6  */

7 package com.ibm.icu.impl;
8
9 import java.lang.ref.ReferenceQueue JavaDoc;
10 import java.lang.ref.SoftReference JavaDoc;
11
12 /**
13  * LRU hash map using softly-referenced values
14  */

15 public class SoftCache {
16     private final LRUMap map;
17     private final ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
18
19     /**
20      * Construct a SoftCache with default cache size
21      */

22     public SoftCache() {
23         map = new LRUMap();
24     }
25     
26     /**
27      * Construct a SoftCache with sepcified initial/max size
28      *
29      * @param initialSize the initial cache size
30      * @param maxSize the maximum cache size
31      */

32     public SoftCache(int initialSize, int maxSize) {
33         map = new LRUMap(initialSize, maxSize);
34     }
35
36     /**
37      * Put an object to the cache
38      * @param key key object
39      * @param value value object
40      * @return the value previously put, null when not matching key is found.
41      */

42     public synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
43         if (key == null || value == null) {
44             throw new IllegalArgumentException JavaDoc("Key and value must not be null");
45         }
46         ProcessQueue();
47         Object JavaDoc obj = map.put(key, new SoftMapEntry(key, value, queue));
48         return obj;
49     }
50
51     /**
52      * Get an object from the cache
53      * @param key key object
54      * @return the cached value, null when the value is not found.
55      */

56     public synchronized Object JavaDoc get(Object JavaDoc key) {
57         ProcessQueue();
58         Object JavaDoc obj = null;
59         SoftMapEntry entry = (SoftMapEntry)map.get(key);
60         if (entry != null) {
61             obj = entry.get();
62             if (obj == null) {
63                 // It is unlikely to enter into this block, because
64
// ProcessQueue() should already remove a map entrie
65
// whose value was deleted by the garbage collactor.
66
map.remove(key);
67             }
68         }
69         return obj;
70     }
71
72     /**
73      * Remove a cache entry from the cache
74      * @param key key object
75      * @return the value of cache entry which is removed from this cache,
76      * or null when no entry for the key was no found.
77      */

78     public synchronized Object JavaDoc remove(Object JavaDoc key) {
79         return map.remove(key);
80     }
81
82     /**
83      * Clear the cache contents
84      */

85     public synchronized void clear() {
86         ProcessQueue();
87         map.clear();
88     }
89
90     /**
91      * Remove map entries which no longer have value
92      */

93     private void ProcessQueue() {
94         while (true) {
95             SoftMapEntry entry = (SoftMapEntry)queue.poll();
96             if (entry == null) {
97                 break;
98             }
99             map.remove(entry.getKey());
100         }
101     }
102
103     /**
104      * A class for map entry with soft-referenced value
105      */

106     private static class SoftMapEntry extends SoftReference JavaDoc {
107         private final Object JavaDoc key;
108
109         private SoftMapEntry(Object JavaDoc key, Object JavaDoc value, ReferenceQueue JavaDoc queue) {
110             super(value, queue);
111             this.key = key;
112         }
113
114         private Object JavaDoc getKey() {
115             return key;
116         }
117     }
118 }
Popular Tags