KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > util > WeakValueMap


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: WeakValueMap.java,v 1.4 2002/11/08 05:06:27 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.util;
12
13 import java.lang.ref.ReferenceQueue JavaDoc;
14 import java.lang.ref.WeakReference JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * A <code>java.util.Map</code> implementation using weak reference values.
20  *
21  * <p>The values are stored in the map as weak references. If the garbage
22  * collector clears the reference, the corresponding key is automatically
23  * removed from the map.
24  *
25  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
26  * @version $Revision: 1.4 $
27  *
28  * @see WeakReference
29  */

30
31 public class WeakValueMap extends ReferenceValueMap
32 {
33     public WeakValueMap()
34     {
35         super();
36     }
37
38
39     public WeakValueMap(int initialCapacity)
40     {
41         super(initialCapacity);
42     }
43
44
45     public WeakValueMap(int initialCapacity, float loadFactor)
46     {
47         super(initialCapacity, loadFactor);
48     }
49
50
51     public WeakValueMap(Map JavaDoc m)
52     {
53         super(m);
54     }
55
56
57     private static class WeakValueReference extends WeakReference JavaDoc implements ReferenceValueMap.ValueReference
58     {
59         private final Object JavaDoc key;
60
61         WeakValueReference(Object JavaDoc key, Object JavaDoc value, ReferenceQueue JavaDoc q)
62         {
63             super(value, q);
64             this.key = key;
65         }
66
67         public Object JavaDoc getKey()
68         {
69             return key;
70         }
71     }
72
73     protected ReferenceValueMap.ValueReference newValueReference(Object JavaDoc key, Object JavaDoc value, ReferenceQueue JavaDoc queue)
74     {
75         return new WeakValueReference(key, value, queue);
76     }
77 }
78
Popular Tags