io.opentelemetry.context.internal.shaded.WeakConcurrentMap Maven / Gradle / Ivy
Show all versions of opentelemetry-context Show documentation
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// Includes work from:
/*
* Copyright Rafael Winterhalter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Suppress warnings since this is vendored as-is.
// CHECKSTYLE:OFF
package io.opentelemetry.context.internal.shaded;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
/**
* A thread-safe map with weak keys. Entries are based on a key's system hash code and keys are
* considered equal only by reference equality. This class does not implement the {@link
* java.util.Map} interface because this implementation is incompatible with the map contract. While
* iterating over a map's entries, any key that has not passed iteration is referenced non-weakly.
*
* This class has been copied as is from
* https://github.com/raphw/weak-lock-free/blob/ad0e5e0c04d4a31f9485bf12b89afbc9d75473b3/src/main/java/com/blogspot/mydailyjava/weaklockfree/WeakConcurrentMap.java
*
*
This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
// Suppress warnings since this is copied as-is.
@SuppressWarnings({
"HashCodeToString",
"MissingSummary",
"UngroupedOverloads",
"ThreadPriorityCheck",
"FieldMissingNullable"
})
public class WeakConcurrentMap
extends AbstractWeakConcurrentMap> {
/**
* Lookup keys are cached thread-locally to avoid allocations on lookups. This is beneficial as
* the JIT unfortunately can't reliably replace the {@link LookupKey} allocation with stack
* allocations, even though the {@link LookupKey} does not escape.
*/
private static final ThreadLocal> LOOKUP_KEY_CACHE =
new ThreadLocal>() {
@Override
protected LookupKey> initialValue() {
return new LookupKey