Debian Codegraph @

src/com/bulenkov/iconloader/util/ConcurrentSoftValueHashMap.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2016 JetBrains s.r.o.
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
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 * Concurrent strong key:K -> soft value:V map
27 * Null keys are NOT allowed
28 * Null values are NOT allowed
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 // public ConcurrentSoftValueHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, @NotNull TObjectHashingStrategy<K> hashingStrategy) {
43 // super(initialCapacity, loadFactor, concurrencyLevel, hashingStrategy);
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 // When referent is collected, equality should be identity-based (for the processQueues() remove this very same SoftValue)
60 // otherwise it's just canonical equals on referents for replace(K,V,V) to work
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 }