KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > WeakValueHashMap


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Oct 8, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.util.*;
27 import java.lang.ref.*;
28
29 public class WeakValueHashMap implements Map {
30     private final Map map = new HashMap();
31     private final ReferenceQueue queue = new ReferenceQueue();
32     
33     public WeakValueHashMap() {
34         super();
35     }
36
37     public int size() {
38         return this.map.size();
39     }
40
41     public boolean isEmpty() {
42         return this.map.isEmpty();
43     }
44
45     public boolean containsKey(Object JavaDoc key) {
46         WeakReference wf = (WeakReference) this.map.get(key);
47         return wf != null && wf.get() != null;
48     }
49
50     public boolean containsValue(Object JavaDoc value) {
51         throw new UnsupportedOperationException JavaDoc();
52     }
53
54     public Object JavaDoc get(Object JavaDoc key) {
55         this.checkQueue();
56         WeakReference wf = (WeakReference) this.map.get(key);
57         return wf == null ? null : wf.get();
58     }
59
60     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
61         if(value == null) {
62             throw new IllegalArgumentException JavaDoc("null values not accepted");
63         }
64         this.checkQueue();
65         Reference ref = new LocalWeakReference(key, value, this.queue);
66         WeakReference oldWf = (WeakReference) this.map.put(key, ref);
67         return oldWf == null ? null : oldWf.get();
68     }
69
70     public Object JavaDoc remove(Object JavaDoc key) {
71         this.checkQueue();
72         WeakReference wf = (WeakReference) this.map.remove(key);
73         return wf == null ? null : wf.get();
74     }
75
76     public void putAll(Map t) {
77         this.checkQueue();
78         Iterator i = t.entrySet().iterator();
79         while(i.hasNext()) {
80             Map.Entry entry = (Map.Entry) i.next();
81             this.put(entry.getKey(), entry.getValue());
82         }
83     }
84
85     public void clear() {
86         this.checkQueue();
87         this.map.clear();
88     }
89
90     public Set keySet() {
91         return this.map.keySet();
92     }
93
94     private final void checkQueue() {
95         ReferenceQueue queue = this.queue;
96         LocalWeakReference ref;
97         while((ref = (LocalWeakReference) queue.poll()) != null) {
98             this.map.remove(ref.getKey());
99         }
100     }
101
102     public Collection values() {
103         return new FilteredCollection(this.map.values(), new LocalFilter());
104     }
105
106     public Set entrySet() {
107         throw new UnsupportedOperationException JavaDoc();
108     }
109     
110     private class LocalFilter implements ObjectFilter {
111         /* (non-Javadoc)
112          * @see org.xamjwg.util.ObjectFilter#decode(java.lang.Object)
113          */

114         public Object JavaDoc decode(Object JavaDoc source) {
115             WeakReference wf = (WeakReference) source;
116             return wf == null ? null : wf.get();
117         }
118
119         /* (non-Javadoc)
120          * @see org.xamjwg.util.ObjectFilter#encode(java.lang.Object)
121          */

122         public Object JavaDoc encode(Object JavaDoc source) {
123             throw new java.lang.UnsupportedOperationException JavaDoc("Read-only collection.");
124         }
125     }
126     
127     private static class LocalWeakReference extends WeakReference {
128         private final Object JavaDoc key;
129         
130         public LocalWeakReference(Object JavaDoc key, Object JavaDoc target, ReferenceQueue queue) {
131             super(target, queue);
132             this.key = key;
133         }
134         
135         public Object JavaDoc getKey() {
136             return key;
137         }
138
139         public boolean equals(Object JavaDoc other) {
140             Object JavaDoc target1 = this.get();
141             Object JavaDoc target2 = other instanceof LocalWeakReference ? ((LocalWeakReference) other).get() : null;
142             return Objects.equals(target1, target2);
143         }
144         
145         public int hashCode() {
146             Object JavaDoc target = this.get();
147             return target == null ? 0 : target.hashCode();
148         }
149     }
150 }
151
Popular Tags