01: /*
02: * Copyright 2002 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: WeakValueMap.java,v 1.4 2002/11/08 05:06:27 jackknifebarber Exp $
09: */
10:
11: package com.triactive.jdo.util;
12:
13: import java.lang.ref.ReferenceQueue;
14: import java.lang.ref.WeakReference;
15: import java.util.Map;
16:
17: /**
18: * A <code>java.util.Map</code> implementation using weak reference values.
19: *
20: * <p>The values are stored in the map as weak references. If the garbage
21: * collector clears the reference, the corresponding key is automatically
22: * removed from the map.
23: *
24: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
25: * @version $Revision: 1.4 $
26: *
27: * @see WeakReference
28: */
29:
30: public class WeakValueMap extends ReferenceValueMap {
31: public WeakValueMap() {
32: super ();
33: }
34:
35: public WeakValueMap(int initialCapacity) {
36: super (initialCapacity);
37: }
38:
39: public WeakValueMap(int initialCapacity, float loadFactor) {
40: super (initialCapacity, loadFactor);
41: }
42:
43: public WeakValueMap(Map m) {
44: super (m);
45: }
46:
47: private static class WeakValueReference extends WeakReference
48: implements ReferenceValueMap.ValueReference {
49: private final Object key;
50:
51: WeakValueReference(Object key, Object value, ReferenceQueue q) {
52: super (value, q);
53: this .key = key;
54: }
55:
56: public Object getKey() {
57: return key;
58: }
59: }
60:
61: protected ReferenceValueMap.ValueReference newValueReference(
62: Object key, Object value, ReferenceQueue queue) {
63: return new WeakValueReference(key, value, queue);
64: }
65: }
|