| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import org.jetbrains.annotations.NotNull; |
| 20 |
|
| 21 |
import java.lang.ref.ReferenceQueue; |
| 22 |
import java.lang.ref.SoftReference; |
| 23 |
import java.util.Map; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public final class ConcurrentSoftValueHashMap<K,V> extends ConcurrentRefValueHashMap<K,V> { |
| 31 |
public ConcurrentSoftValueHashMap(@NotNull Map<K, V> map) { |
| 32 |
super(map); |
| 33 |
} |
| 34 |
|
| 35 |
public ConcurrentSoftValueHashMap() { |
| 36 |
} |
| 37 |
|
| 38 |
public ConcurrentSoftValueHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) { |
| 39 |
super(initialCapacity, loadFactor, concurrencyLevel); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private static class MySoftReference<K, V> extends SoftReference<V> implements ValueReference<K, V> { |
| 47 |
private final K key; |
| 48 |
private MySoftReference(@NotNull K key, @NotNull V referent, @NotNull ReferenceQueue<V> q) { |
| 49 |
super(referent, q); |
| 50 |
this.key = key; |
| 51 |
} |
| 52 |
|
| 53 |
@NotNull |
| 54 |
@Override |
| 55 |
public K getKey() { |
| 56 |
return key; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public final boolean equals(final Object o) { |
| 62 |
if (this == o) return true; |
| 63 |
if (o == null || getClass() != o.getClass()) return false; |
| 64 |
|
| 65 |
@SuppressWarnings("unchecked") |
| 66 |
ValueReference<K,V> that = (ValueReference)o; |
| 67 |
|
| 68 |
V v = get(); |
| 69 |
V thatV = that.get(); |
| 70 |
return key.equals(that.getKey()) && v != null && thatV != null && v.equals(thatV); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@NotNull |
| 75 |
@Override |
| 76 |
protected ValueReference<K, V> createValueReference(@NotNull K key, @NotNull V value) { |
| 77 |
return new MySoftReference<K,V>(key, value, myQueue); |
| 78 |
} |
| 79 |
} |