diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-05-11 14:05:29 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-05-11 14:05:29 +0200 |
commit | ae03641a1c2ec122051ddd01f2dd398f87cc0c46 (patch) | |
tree | 96c20134049458798446f32ea27fe68c39c5bae7 /container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/SimpleConcurrentIdentityHashMap.java | |
parent | 4ae244bc86782b3dc36257edcfabc2e38f510cf7 (diff) |
Extract concurrent identity hashmap into separate class
Diffstat (limited to 'container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/SimpleConcurrentIdentityHashMap.java')
-rw-r--r-- | container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/SimpleConcurrentIdentityHashMap.java | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/SimpleConcurrentIdentityHashMap.java b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/SimpleConcurrentIdentityHashMap.java new file mode 100644 index 00000000000..52142e534ba --- /dev/null +++ b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/SimpleConcurrentIdentityHashMap.java @@ -0,0 +1,39 @@ +// Copyright Verizon Media. 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.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * 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<>(); + + V get(K key) { return wrappedMap.get(IdentityKey.of(key)); } + + V remove(K key) { return wrappedMap.remove(IdentityKey.of(key)); } + + void put(K key, V value) { wrappedMap.put(IdentityKey.of(key), value); } + + 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; + } + } +} |