blob: 5d53933c36758ee0ad97dd342c5dd6ec2af2165e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* A simplified {@link ConcurrentMap} using reference-equality to compare keys (similarly to {@link java.util.IdentityHashMap})
*
* @author bjorncs
*/
class SimpleConcurrentIdentityHashMap<K, V> {
private final ConcurrentMap<IdentityKey<K>, V> wrappedMap = new ConcurrentHashMap<>();
Optional<V> get(K key) { return Optional.ofNullable(wrappedMap.get(identityKey(key))); }
Optional<V> remove(K key) { return Optional.ofNullable(wrappedMap.remove(identityKey(key))); }
Optional<V> put(K key, V value) { return Optional.ofNullable(wrappedMap.put(identityKey(key), value)); }
V computeIfAbsent(K key, Supplier<V> supplier) {
return wrappedMap.computeIfAbsent(identityKey(key), ignored -> supplier.get());
}
V computeIfAbsent(K key, Function<K, V> factory) {
return wrappedMap.computeIfAbsent(identityKey(key), k -> factory.apply(k.instance));
}
private static <K> IdentityKey<K> identityKey(K key) { return IdentityKey.of(key); }
private static class IdentityKey<K> {
final K instance;
IdentityKey(K instance) { this.instance = instance; }
static <K> IdentityKey<K> of(K instance) { return new IdentityKey<>(instance); }
@Override public int hashCode() { return System.identityHashCode(instance); }
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof IdentityKey<?>)) return false;
IdentityKey<?> other = (IdentityKey<?>) obj;
return this.instance == other.instance;
}
}
}
|