KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > InstanceMap


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.lang.ref.ReferenceQueue JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  *
31  * @author Martin Matula
32  */

33 public class InstanceMap extends HashMap JavaDoc {
34     private final ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
35
36     private class InstanceReference extends WeakReference JavaDoc {
37         private Object JavaDoc key;
38
39         public InstanceReference(Object JavaDoc key, Object JavaDoc instance) {
40             super(instance, queue);
41             this.key = key;
42         }
43
44         public Object JavaDoc getKey() {
45             return key;
46         }
47     }
48
49     private void cleanUp() {
50         InstanceReference reference;
51
52         while ((reference = (InstanceReference) queue.poll()) != null) {
53             Object JavaDoc key = reference.getKey();
54             Reference JavaDoc currentRef = (Reference JavaDoc) super.remove(key);
55             if (currentRef != null && currentRef != reference && currentRef.get() != null) {
56                 super.put(key, currentRef);
57             }
58         }
59     }
60
61     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
62         cleanUp();
63         Object JavaDoc result = super.put(key, new InstanceReference(key, value));
64         if (result != null) {
65             return ((InstanceReference) result).get();
66         } else {
67             return result;
68         }
69     }
70
71     public Object JavaDoc get(Object JavaDoc key) {
72         cleanUp();
73         Object JavaDoc result = super.get(key);
74         if (result != null) {
75             return ((InstanceReference) result).get();
76         } else {
77             return result;
78         }
79     }
80     
81     public Collection JavaDoc values() {
82         ArrayList JavaDoc result = new ArrayList JavaDoc();
83         cleanUp();
84         for (Iterator JavaDoc it = super.values().iterator(); it.hasNext();) {
85             result.add(((InstanceReference) it.next()).get());
86         }
87         return result;
88     }
89 }
90
Popular Tags