KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > identicator > IdWeakHashMap


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.identicator;
25
26 import java.lang.ref.*;
27 import java.util.*;
28 import com.mchange.v1.util.WrapperIterator;
29
30 /**
31  * IdWeakHashMap is NOT null-accepting!
32  */

33 public final class IdWeakHashMap extends IdMap implements Map
34 {
35     ReferenceQueue rq;
36
37     public IdWeakHashMap(Identicator id)
38     {
39     super ( new HashMap(), id );
40     this.rq = new ReferenceQueue();
41     }
42
43     //all methods from Map interface
44
public int size()
45     {
46     // doing cleanCleared() afterwards, as with other methods
47
// would be just as "correct", as weak collections
48
// make no guarantees about when things disappear,
49
// but for size(), it feels a little more accurate
50
// this way.
51
cleanCleared();
52     return super.size();
53     }
54
55     public boolean isEmpty()
56     {
57     try
58         { return super.isEmpty(); }
59     finally
60         { cleanCleared(); }
61     }
62
63     public boolean containsKey(Object JavaDoc o)
64     {
65     try
66         { return super.containsKey( o ); }
67     finally
68         { cleanCleared(); }
69     }
70
71     public boolean containsValue(Object JavaDoc o)
72     {
73     try
74         { return super.containsValue( o ); }
75     finally
76         { cleanCleared(); }
77     }
78
79     public Object JavaDoc get(Object JavaDoc o)
80     {
81     try
82         { return super.get( o ); }
83     finally
84         { cleanCleared(); }
85     }
86
87     public Object JavaDoc put(Object JavaDoc k, Object JavaDoc v)
88     {
89     try
90         { return super.put( k , v ); }
91     finally
92         { cleanCleared(); }
93     }
94
95     public Object JavaDoc remove(Object JavaDoc o)
96     {
97     try
98         { return super.remove( o ); }
99     finally
100         { cleanCleared(); }
101     }
102
103     public void putAll(Map m)
104     {
105     try
106         { super.putAll( m ); }
107     finally
108         { cleanCleared(); }
109     }
110
111     public void clear()
112     {
113     try
114         { super.clear(); }
115     finally
116         { cleanCleared(); }
117     }
118
119     public Set keySet()
120     {
121     try
122         { return super.keySet(); }
123     finally
124         { cleanCleared(); }
125     }
126
127     public Collection values()
128     {
129     try
130         { return super.values(); }
131     finally
132         { cleanCleared(); }
133     }
134
135     /*
136      * entrySet() is the basis of the implementation of the other
137      * Collection returning methods. Get this right and the rest
138      * follow.
139      */

140     public Set entrySet()
141     {
142     try
143         { return new WeakUserEntrySet(); }
144     finally
145         { cleanCleared(); }
146     }
147
148     public boolean equals(Object JavaDoc o)
149     {
150     try
151         { return super.equals( o ); }
152     finally
153         { cleanCleared(); }
154     }
155
156     public int hashCode()
157     {
158     try
159         { return super.hashCode(); }
160     finally
161         { cleanCleared(); }
162     }
163
164     //internal methods
165
protected IdHashKey createIdKey(Object JavaDoc o)
166     { return new WeakIdHashKey( o, id, rq ); }
167
168     private void cleanCleared()
169     {
170     WeakIdHashKey.Ref ref;
171     while ((ref = (WeakIdHashKey.Ref) rq.poll()) != null)
172         this.removeIdHashKey( ref.getKey() );
173     }
174
175     private final class WeakUserEntrySet extends AbstractSet
176     {
177     Set innerEntries = internalEntrySet();
178     
179     public Iterator iterator()
180     {
181         try
182         {
183             return new WrapperIterator(innerEntries.iterator(), true)
184             {
185                 protected Object JavaDoc transformObject(Object JavaDoc o)
186                 {
187                 Entry innerEntry = (Entry) o;
188                 final Object JavaDoc userKey = ((IdHashKey) innerEntry.getKey()).getKeyObj();
189                 if (userKey == null)
190                     return WrapperIterator.SKIP_TOKEN;
191                 else
192                     return new UserEntry( innerEntry )
193                     { Object JavaDoc preventRefClear = userKey; };
194                 }
195             };
196         }
197     finally
198         { cleanCleared(); }
199     }
200     
201     public int size()
202     {
203         // doing cleanCleared() afterwards, as with other methods
204
// would be just as "correct", as weak collections
205
// make no guarantees about when things disappear,
206
// but for size(), it feels a little more accurate
207
// this way.
208
cleanCleared();
209         return innerEntries.size();
210     }
211     
212     public boolean contains(Object JavaDoc o)
213     {
214         try
215         {
216             if (o instanceof Entry)
217             {
218                 Entry entry = (Entry) o;
219                 return innerEntries.contains( createIdEntry( entry ) );
220             }
221             else
222             return false;
223         }
224         finally
225         { cleanCleared(); }
226     }
227     
228     public boolean remove(Object JavaDoc o)
229     {
230         try
231         {
232             if (o instanceof Entry)
233             {
234                 Entry entry = (Entry) o;
235                 return innerEntries.remove( createIdEntry( entry ) );
236             }
237             else
238             return false;
239         }
240         finally
241         { cleanCleared(); }
242     }
243
244     public void clear()
245     {
246         try
247         { inner.clear(); }
248         finally
249         { cleanCleared(); }
250     }
251     }
252 }
253
Popular Tags