01: /**********************************************************************
02: Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: 2002 Kelly Grizzle (TJDO)
18: 2003 Andy Jefferson - coding standards
19: ...
20: **********************************************************************/package org.jpox.util;
21:
22: import java.lang.ref.ReferenceQueue;
23: import java.lang.ref.WeakReference;
24: import java.util.Map;
25:
26: /**
27: * A <code>java.util.Map</code> implementation using weak reference values.
28: *
29: * <p>The values are stored in the map as weak references. If the garbage
30: * collector clears the reference, the corresponding key is automatically
31: * removed from the map.
32: *
33: * @see WeakReference
34: * @version $Revision: 1.2 $
35: */
36: public class WeakValueMap extends ReferenceValueMap {
37: /**
38: * Default Constructor
39: **/
40: public WeakValueMap() {
41: super ();
42: }
43:
44: /**
45: * Constructor taking the initial capacity.
46: * @param initialCapacity The Initial capacity of the collection
47: **/
48: public WeakValueMap(int initialCapacity) {
49: super (initialCapacity);
50: }
51:
52: /**
53: * Constructor taking the initial capacity and load factor.
54: * @param initialCapacity The Initial capacity of the collection
55: * @param loadFactor The Load Factor of the collection
56: **/
57: public WeakValueMap(int initialCapacity, float loadFactor) {
58: super (initialCapacity, loadFactor);
59: }
60:
61: /**
62: * Constructor taking a Map for definition.
63: * @param m The Map
64: **/
65: public WeakValueMap(Map m) {
66: super (m);
67: }
68:
69: /**
70: * Inner class to represent a weak reference - one that can be garbage
71: * collected.
72: **/
73: private static class WeakValueReference extends WeakReference
74: implements ReferenceValueMap.ValueReference {
75: private final Object key;
76:
77: WeakValueReference(Object key, Object value, ReferenceQueue q) {
78: super (value, q);
79: this .key = key;
80: }
81:
82: public Object getKey() {
83: return key;
84: }
85: }
86:
87: protected ReferenceValueMap.ValueReference newValueReference(
88: Object key, Object value, ReferenceQueue queue) {
89: return new WeakValueReference(key, value, queue);
90: }
91: }
|