crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google Inc. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
kevinb9n | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 17 | package com.google.inject.internal; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 18 | |
kevinb9n | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 19 | import static com.google.inject.internal.ReferenceType.STRONG; |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 20 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.io.ObjectInputStream; |
| 23 | import java.io.ObjectOutputStream; |
| 24 | import java.io.Serializable; |
| 25 | import java.lang.ref.Reference; |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 26 | import java.util.AbstractMap; |
| 27 | import java.util.AbstractSet; |
| 28 | import java.util.Iterator; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 29 | import java.util.Map; |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 30 | import java.util.NoSuchElementException; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | import java.util.concurrent.ConcurrentHashMap; |
| 33 | import java.util.concurrent.ConcurrentMap; |
| 34 | |
| 35 | /** |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 36 | * Concurrent hash map that wraps keys and/or values in soft or weak references. |
| 37 | * Does not support null keys or values. Uses identity equality for weak and |
| 38 | * soft keys. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 39 | * |
| 40 | * <p>The concurrent semantics of {@link ConcurrentHashMap} combined with the |
| 41 | * fact that the garbage collector can asynchronously reclaim and clean up |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 42 | * keys and values at any time can lead to some racy semantics. For example, |
| 43 | * {@link #size()} returns an upper bound on the size; that is, the actual size |
| 44 | * may be smaller in cases where the key or value has been reclaimed but the map |
| 45 | * entry has not been cleaned up yet. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 46 | * |
| 47 | * @author crazybob@google.com (Bob Lee) |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 48 | * @author fry@google.com (Charles Fry) |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 49 | */ |
| 50 | @SuppressWarnings("unchecked") |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 51 | public class ReferenceMap<K, V> extends AbstractMap<K, V> |
| 52 | implements ConcurrentMap<K, V>, Serializable { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 53 | |
| 54 | transient ConcurrentMap<Object, Object> delegate; |
| 55 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 56 | private final ReferenceType keyReferenceType; |
| 57 | private final ReferenceType valueReferenceType; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Concurrent hash map that wraps keys and/or values based on specified |
| 61 | * reference types. |
| 62 | * |
| 63 | * @param keyReferenceType key reference type |
| 64 | * @param valueReferenceType value reference type |
| 65 | */ |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 66 | public ReferenceMap( |
| 67 | ReferenceType keyReferenceType, ReferenceType valueReferenceType) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 68 | ensureNotNull(keyReferenceType, valueReferenceType); |
| 69 | |
| 70 | if (keyReferenceType == ReferenceType.PHANTOM |
| 71 | || valueReferenceType == ReferenceType.PHANTOM) { |
| 72 | throw new IllegalArgumentException("Phantom references not supported."); |
| 73 | } |
| 74 | |
| 75 | this.delegate = new ConcurrentHashMap<Object, Object>(); |
| 76 | this.keyReferenceType = keyReferenceType; |
| 77 | this.valueReferenceType = valueReferenceType; |
| 78 | } |
| 79 | |
| 80 | V internalGet(K key) { |
| 81 | Object valueReference = delegate.get(makeKeyReferenceAware(key)); |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 82 | return dereferenceValue(valueReference); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public V get(final Object key) { |
| 86 | ensureNotNull(key); |
| 87 | return internalGet((K) key); |
| 88 | } |
| 89 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 90 | private V execute(Strategy strategy, K key, V value) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 91 | ensureNotNull(key, value); |
| 92 | Object keyReference = referenceKey(key); |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 93 | return (V) strategy.execute( |
| 94 | this, keyReference, referenceValue(keyReference, value) |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 95 | ); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public V put(K key, V value) { |
| 99 | return execute(putStrategy(), key, value); |
| 100 | } |
| 101 | |
| 102 | public V remove(Object key) { |
| 103 | ensureNotNull(key); |
| 104 | Object referenceAwareKey = makeKeyReferenceAware(key); |
| 105 | Object valueReference = delegate.remove(referenceAwareKey); |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 106 | return dereferenceValue(valueReference); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public int size() { |
| 110 | return delegate.size(); |
| 111 | } |
| 112 | |
| 113 | public boolean isEmpty() { |
| 114 | return delegate.isEmpty(); |
| 115 | } |
| 116 | |
| 117 | public boolean containsKey(Object key) { |
| 118 | ensureNotNull(key); |
| 119 | Object referenceAwareKey = makeKeyReferenceAware(key); |
| 120 | return delegate.containsKey(referenceAwareKey); |
| 121 | } |
| 122 | |
| 123 | public boolean containsValue(Object value) { |
| 124 | ensureNotNull(value); |
| 125 | for (Object valueReference : delegate.values()) { |
| 126 | if (value.equals(dereferenceValue(valueReference))) { |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | public void putAll(Map<? extends K, ? extends V> t) { |
| 134 | for (Map.Entry<? extends K, ? extends V> entry : t.entrySet()) { |
| 135 | put(entry.getKey(), entry.getValue()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public void clear() { |
| 140 | delegate.clear(); |
| 141 | } |
| 142 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 143 | public V putIfAbsent(K key, V value) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 144 | return execute(putIfAbsentStrategy(), key, value); |
| 145 | } |
| 146 | |
| 147 | public boolean remove(Object key, Object value) { |
| 148 | ensureNotNull(key, value); |
| 149 | Object referenceAwareKey = makeKeyReferenceAware(key); |
| 150 | Object referenceAwareValue = makeValueReferenceAware(value); |
| 151 | return delegate.remove(referenceAwareKey, referenceAwareValue); |
| 152 | } |
| 153 | |
| 154 | public boolean replace(K key, V oldValue, V newValue) { |
| 155 | ensureNotNull(key, oldValue, newValue); |
| 156 | Object keyReference = referenceKey(key); |
| 157 | |
| 158 | Object referenceAwareOldValue = makeValueReferenceAware(oldValue); |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 159 | return delegate.replace(keyReference, referenceAwareOldValue, |
| 160 | referenceValue(keyReference, newValue) |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 161 | ); |
| 162 | } |
| 163 | |
| 164 | public V replace(K key, V value) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 165 | return execute(replaceStrategy(), key, value); |
| 166 | } |
| 167 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 168 | private transient volatile Set<Map.Entry<K, V>> entrySet = null; |
| 169 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 170 | public Set<Map.Entry<K, V>> entrySet() { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 171 | if (entrySet == null) { |
| 172 | entrySet = new EntrySet(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 173 | } |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 174 | return entrySet; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 175 | } |
| 176 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 177 | /** Dereferences an entry. Returns null if the key or value has been gc'ed. */ |
| 178 | private Entry dereferenceEntry(Map.Entry<Object, Object> entry) { |
| 179 | K key = dereferenceKey(entry.getKey()); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 180 | V value = dereferenceValue(entry.getValue()); |
| 181 | return (key == null || value == null) |
| 182 | ? null |
| 183 | : new Entry(key, value); |
| 184 | } |
| 185 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 186 | /** Creates a reference for a key. */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 187 | Object referenceKey(K key) { |
| 188 | switch (keyReferenceType) { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 189 | case STRONG: |
| 190 | return key; |
| 191 | case SOFT: |
| 192 | return new SoftKeyReference(key); |
| 193 | case WEAK: |
| 194 | return new WeakKeyReference(key); |
| 195 | default: |
| 196 | throw new AssertionError(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 200 | /** Converts a reference to a key. */ |
| 201 | private K dereferenceKey(Object o) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 202 | return (K) dereference(keyReferenceType, o); |
| 203 | } |
| 204 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 205 | /** Converts a reference to a value. */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 206 | V dereferenceValue(Object o) { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 207 | if (o == null) { |
| 208 | return null; |
| 209 | } |
| 210 | Object value = dereference(valueReferenceType, o); |
| 211 | if (o instanceof InternalReference) { |
| 212 | InternalReference ref = (InternalReference) o; |
| 213 | if (value == null) { |
| 214 | // old value was garbage collected |
| 215 | ref.finalizeReferent(); |
| 216 | } |
| 217 | } |
| 218 | return (V) value; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 219 | } |
| 220 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 221 | /** Returns the refererent for reference given its reference type. */ |
| 222 | private Object dereference(ReferenceType referenceType, Object reference) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 223 | return referenceType == STRONG ? reference : ((Reference) reference).get(); |
| 224 | } |
| 225 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 226 | /** Creates a reference for a value. */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 227 | Object referenceValue(Object keyReference, Object value) { |
| 228 | switch (valueReferenceType) { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 229 | case STRONG: |
| 230 | return value; |
| 231 | case SOFT: |
| 232 | return new SoftValueReference(keyReference, value); |
| 233 | case WEAK: |
| 234 | return new WeakValueReference(keyReference, value); |
| 235 | default: |
| 236 | throw new AssertionError(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 241 | * Wraps key so it can be compared to a referenced key for equality. |
| 242 | */ |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 243 | private Object makeKeyReferenceAware(Object o) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 244 | return keyReferenceType == STRONG ? o : new KeyReferenceAwareWrapper(o); |
| 245 | } |
| 246 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 247 | /** Wraps value so it can be compared to a referenced value for equality. */ |
| 248 | private Object makeValueReferenceAware(Object o) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 249 | return valueReferenceType == STRONG ? o : new ReferenceAwareWrapper(o); |
| 250 | } |
| 251 | |
| 252 | /** |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 253 | * Marker interface to differentiate external and internal references. Also |
| 254 | * duplicates FinalizableReference and Reference.get for internal use. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 255 | */ |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 256 | interface InternalReference { |
| 257 | void finalizeReferent(); |
| 258 | |
| 259 | Object get(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 260 | } |
| 261 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 262 | private static int keyHashCode(Object key) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 263 | return System.identityHashCode(key); |
| 264 | } |
| 265 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 266 | /* |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 267 | * Tests weak and soft references for identity equality. Compares references |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 268 | * to other references and wrappers. If o is a reference, this returns true if |
| 269 | * r == o or if r and o reference the same non-null object. If o is a wrapper, |
| 270 | * this returns true if r's referent is identical to the wrapped object. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 271 | */ |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 272 | private static boolean referenceEquals(Reference r, Object o) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 273 | // compare reference to reference. |
| 274 | if (o instanceof InternalReference) { |
| 275 | // are they the same reference? used in cleanup. |
| 276 | if (o == r) { |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | // do they reference identical values? used in conditional puts. |
| 281 | Object referent = ((Reference) o).get(); |
| 282 | return referent != null && referent == r.get(); |
| 283 | } |
| 284 | |
| 285 | // is the wrapped object identical to the referent? used in lookups. |
| 286 | return ((ReferenceAwareWrapper) o).unwrap() == r.get(); |
| 287 | } |
| 288 | |
| 289 | /** |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 290 | * Returns {@code true} if the specified value reference has been garbage |
| 291 | * collected. The value behind the reference is also passed in, rather than |
| 292 | * queried inside this method, to ensure that the return statement of this |
| 293 | * method will still hold true after it has returned (that is, a value |
| 294 | * reference exists outside of this method which will prevent that value from |
| 295 | * being garbage collected). |
| 296 | * |
| 297 | * @param valueReference the value reference to be tested |
| 298 | * @param value the object referenced by {@code valueReference} |
| 299 | * @return {@code true} if {@code valueReference} is non-null and {@code |
| 300 | * value} is null |
| 301 | */ |
| 302 | private static boolean isExpired(Object valueReference, Object value) { |
| 303 | return (valueReference != null) && (value == null); |
| 304 | } |
| 305 | |
| 306 | /** |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 307 | * Big hack. Used to compare keys and values to referenced keys and values |
| 308 | * without creating more references. |
| 309 | */ |
| 310 | static class ReferenceAwareWrapper { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 311 | final Object wrapped; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 312 | |
| 313 | ReferenceAwareWrapper(Object wrapped) { |
| 314 | this.wrapped = wrapped; |
| 315 | } |
| 316 | |
| 317 | Object unwrap() { |
| 318 | return wrapped; |
| 319 | } |
| 320 | |
| 321 | public int hashCode() { |
| 322 | return wrapped.hashCode(); |
| 323 | } |
| 324 | |
| 325 | public boolean equals(Object obj) { |
| 326 | // defer to reference's equals() logic. |
| 327 | return obj.equals(this); |
| 328 | } |
| 329 | } |
| 330 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 331 | /** Used for keys. Overrides hash code to use identity hash code. */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 332 | static class KeyReferenceAwareWrapper extends ReferenceAwareWrapper { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 333 | public KeyReferenceAwareWrapper(Object wrapped) { |
| 334 | super(wrapped); |
| 335 | } |
| 336 | |
| 337 | public int hashCode() { |
| 338 | return System.identityHashCode(wrapped); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | class SoftKeyReference extends FinalizableSoftReference<Object> |
| 343 | implements InternalReference { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 344 | final int hashCode; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 345 | |
| 346 | public SoftKeyReference(Object key) { |
| 347 | super(key); |
| 348 | this.hashCode = keyHashCode(key); |
| 349 | } |
| 350 | |
| 351 | public void finalizeReferent() { |
| 352 | delegate.remove(this); |
| 353 | } |
| 354 | |
| 355 | @Override public int hashCode() { |
| 356 | return this.hashCode; |
| 357 | } |
| 358 | |
| 359 | @Override public boolean equals(Object o) { |
| 360 | return referenceEquals(this, o); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | class WeakKeyReference extends FinalizableWeakReference<Object> |
| 365 | implements InternalReference { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 366 | final int hashCode; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 367 | |
| 368 | public WeakKeyReference(Object key) { |
| 369 | super(key); |
| 370 | this.hashCode = keyHashCode(key); |
| 371 | } |
| 372 | |
| 373 | public void finalizeReferent() { |
| 374 | delegate.remove(this); |
| 375 | } |
| 376 | |
| 377 | @Override public int hashCode() { |
| 378 | return this.hashCode; |
| 379 | } |
| 380 | |
| 381 | @Override public boolean equals(Object o) { |
| 382 | return referenceEquals(this, o); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | class SoftValueReference extends FinalizableSoftReference<Object> |
| 387 | implements InternalReference { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 388 | final Object keyReference; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 389 | |
| 390 | public SoftValueReference(Object keyReference, Object value) { |
| 391 | super(value); |
| 392 | this.keyReference = keyReference; |
| 393 | } |
| 394 | |
| 395 | public void finalizeReferent() { |
| 396 | delegate.remove(keyReference, this); |
| 397 | } |
| 398 | |
| 399 | @Override public boolean equals(Object obj) { |
| 400 | return referenceEquals(this, obj); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | class WeakValueReference extends FinalizableWeakReference<Object> |
| 405 | implements InternalReference { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 406 | final Object keyReference; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 407 | |
| 408 | public WeakValueReference(Object keyReference, Object value) { |
| 409 | super(value); |
| 410 | this.keyReference = keyReference; |
| 411 | } |
| 412 | |
| 413 | public void finalizeReferent() { |
| 414 | delegate.remove(keyReference, this); |
| 415 | } |
| 416 | |
| 417 | @Override public boolean equals(Object obj) { |
| 418 | return referenceEquals(this, obj); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | protected interface Strategy { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 423 | public Object execute( |
| 424 | ReferenceMap map, Object keyReference, Object valueReference); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 425 | } |
| 426 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 427 | // TODO(crazybob): a getter called put() is probably a bad idea |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 428 | protected Strategy putStrategy() { |
| 429 | return PutStrategy.PUT; |
| 430 | } |
| 431 | |
| 432 | protected Strategy putIfAbsentStrategy() { |
| 433 | return PutStrategy.PUT_IF_ABSENT; |
| 434 | } |
| 435 | |
| 436 | protected Strategy replaceStrategy() { |
| 437 | return PutStrategy.REPLACE; |
| 438 | } |
| 439 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 440 | private enum PutStrategy implements Strategy { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 441 | PUT { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 442 | public Object execute( |
| 443 | ReferenceMap map, Object keyReference, Object valueReference) { |
| 444 | return map.dereferenceValue( |
| 445 | map.delegate.put(keyReference, valueReference)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 446 | } |
| 447 | }, |
| 448 | |
| 449 | REPLACE { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 450 | public Object execute( |
| 451 | ReferenceMap map, Object keyReference, Object valueReference) { |
| 452 | // ensure that the existing value is not collected |
| 453 | do { |
| 454 | Object existingValueReference; |
| 455 | Object existingValue; |
| 456 | do { |
| 457 | existingValueReference = map.delegate.get(keyReference); |
| 458 | existingValue = map.dereferenceValue(existingValueReference); |
| 459 | } while (isExpired(existingValueReference, existingValue)); |
| 460 | |
| 461 | if (existingValueReference == null) { |
| 462 | // nothing to replace |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | if (map.delegate.replace( |
| 467 | keyReference, existingValueReference, valueReference)) { |
| 468 | // existingValue didn't expire since we still have a reference to it |
| 469 | return existingValue; |
| 470 | } |
| 471 | } while (true); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 472 | } |
| 473 | }, |
| 474 | |
| 475 | PUT_IF_ABSENT { |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 476 | public Object execute( |
| 477 | ReferenceMap map, Object keyReference, Object valueReference) { |
| 478 | Object existingValueReference; |
| 479 | Object existingValue; |
| 480 | do { |
| 481 | existingValueReference |
| 482 | = map.delegate.putIfAbsent(keyReference, valueReference); |
| 483 | existingValue = map.dereferenceValue(existingValueReference); |
| 484 | } while (isExpired(existingValueReference, existingValue)); |
| 485 | return existingValue; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 486 | } |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 487 | }, |
| 488 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 489 | |
| 490 | private static PutStrategy defaultPutStrategy; |
| 491 | |
| 492 | protected PutStrategy getPutStrategy() { |
| 493 | return defaultPutStrategy; |
| 494 | } |
| 495 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 496 | class Entry implements Map.Entry<K, V> { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 497 | final K key; |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 498 | V value; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 499 | |
| 500 | public Entry(K key, V value) { |
| 501 | this.key = key; |
| 502 | this.value = value; |
| 503 | } |
| 504 | |
| 505 | public K getKey() { |
| 506 | return this.key; |
| 507 | } |
| 508 | |
| 509 | public V getValue() { |
| 510 | return this.value; |
| 511 | } |
| 512 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 513 | public V setValue(V newValue) { |
| 514 | value = newValue; |
| 515 | return put(key, newValue); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | public int hashCode() { |
| 519 | return key.hashCode() * 31 + value.hashCode(); |
| 520 | } |
| 521 | |
| 522 | public boolean equals(Object o) { |
| 523 | if (!(o instanceof ReferenceMap.Entry)) { |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | Entry entry = (Entry) o; |
| 528 | return key.equals(entry.key) && value.equals(entry.value); |
| 529 | } |
| 530 | |
| 531 | public String toString() { |
| 532 | return key + "=" + value; |
| 533 | } |
| 534 | } |
| 535 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 536 | private class ReferenceIterator implements Iterator<Map.Entry<K, V>> { |
| 537 | private Iterator<Map.Entry<Object, Object>> i = |
| 538 | delegate.entrySet().iterator(); |
| 539 | private Map.Entry<K, V> nextEntry; |
| 540 | private Map.Entry<K, V> lastReturned; |
| 541 | |
| 542 | public ReferenceIterator() { |
| 543 | advance(); |
| 544 | } |
| 545 | |
| 546 | private void advance() { |
| 547 | while (i.hasNext()) { |
| 548 | Map.Entry<K, V> entry = dereferenceEntry(i.next()); |
| 549 | if (entry != null) { |
| 550 | nextEntry = entry; |
| 551 | return; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // nothing left |
| 556 | nextEntry = null; |
| 557 | } |
| 558 | |
| 559 | public boolean hasNext() { |
| 560 | return nextEntry != null; |
| 561 | } |
| 562 | |
| 563 | public Map.Entry<K, V> next() { |
| 564 | if (nextEntry == null) { |
| 565 | throw new NoSuchElementException(); |
| 566 | } |
| 567 | lastReturned = nextEntry; |
| 568 | advance(); |
| 569 | return lastReturned; |
| 570 | } |
| 571 | |
| 572 | public void remove() { |
| 573 | ReferenceMap.this.remove(lastReturned.getKey()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | private class EntrySet extends AbstractSet<Map.Entry<K, V>> { |
| 578 | public Iterator<Map.Entry<K, V>> iterator() { |
| 579 | return new ReferenceIterator(); |
| 580 | } |
| 581 | |
| 582 | public int size() { |
| 583 | return delegate.size(); |
| 584 | } |
| 585 | |
| 586 | public boolean contains(Object o) { |
| 587 | if (!(o instanceof Map.Entry)) { |
| 588 | return false; |
| 589 | } |
| 590 | Map.Entry<K, V> e = (Map.Entry<K, V>) o; |
| 591 | V v = ReferenceMap.this.get(e.getKey()); |
| 592 | return v != null && v.equals(e.getValue()); |
| 593 | } |
| 594 | |
| 595 | public boolean remove(Object o) { |
| 596 | if (!(o instanceof Map.Entry)) { |
| 597 | return false; |
| 598 | } |
| 599 | Map.Entry<K, V> e = (Map.Entry<K, V>) o; |
| 600 | return ReferenceMap.this.remove(e.getKey(), e.getValue()); |
| 601 | } |
| 602 | |
| 603 | public void clear() { |
| 604 | delegate.clear(); |
| 605 | } |
| 606 | }; |
| 607 | |
| 608 | // TODO(kevinb): use preconditions |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 609 | static void ensureNotNull(Object o) { |
| 610 | if (o == null) { |
| 611 | throw new NullPointerException(); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static void ensureNotNull(Object... array) { |
| 616 | for (int i = 0; i < array.length; i++) { |
| 617 | if (array[i] == null) { |
| 618 | throw new NullPointerException("Argument #" + i + " is null."); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 623 | private void writeObject(ObjectOutputStream out) throws IOException { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 624 | out.defaultWriteObject(); |
| 625 | out.writeInt(size()); |
| 626 | for (Map.Entry<Object, Object> entry : delegate.entrySet()) { |
| 627 | Object key = dereferenceKey(entry.getKey()); |
| 628 | Object value = dereferenceValue(entry.getValue()); |
| 629 | |
| 630 | // don't persist gc'ed entries. |
| 631 | if (key != null && value != null) { |
| 632 | out.writeObject(key); |
| 633 | out.writeObject(value); |
| 634 | } |
| 635 | } |
| 636 | out.writeObject(null); |
| 637 | } |
| 638 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 639 | private void readObject(ObjectInputStream in) |
| 640 | throws IOException, ClassNotFoundException { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 641 | in.defaultReadObject(); |
| 642 | int size = in.readInt(); |
| 643 | this.delegate = new ConcurrentHashMap<Object, Object>(size); |
| 644 | while (true) { |
| 645 | K key = (K) in.readObject(); |
| 646 | if (key == null) { |
| 647 | break; |
| 648 | } |
| 649 | V value = (V) in.readObject(); |
| 650 | put(key, value); |
| 651 | } |
| 652 | } |
| 653 | |
crazyboblee | c49e091 | 2007-08-31 23:52:25 +0000 | [diff] [blame] | 654 | private static final long serialVersionUID = 0; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 655 | } |