Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.cedarsoftware.util;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
/**
* An abstract thread-safe implementation of the {@link ConcurrentMap} interface that allows {@code null} keys
* and {@code null} values. Internally, {@code AbstractConcurrentNullSafeMap} uses sentinel objects to
* represent {@code null} keys and values, enabling safe handling of {@code null} while maintaining
* compatibility with {@link ConcurrentMap} behavior.
*
*
Key Features
*
*
Thread-Safe: Implements {@link ConcurrentMap} with thread-safe operations.
*
Null Handling: Supports {@code null} keys and {@code null} values using sentinel objects
* ({@link NullSentinel}).
*
Customizable: Allows customization of the underlying {@link ConcurrentMap} through its
* constructor.
*
Standard Map Behavior: Adheres to the {@link Map} and {@link ConcurrentMap} contract,
* supporting operations like {@link #putIfAbsent}, {@link #computeIfAbsent}, {@link #merge}, and more.
*
*
*
Null Key and Value Handling
*
* The {@code AbstractConcurrentNullSafeMap} uses internal sentinel objects ({@link NullSentinel}) to distinguish
* {@code null} keys and values from actual entries. This ensures that {@code null} keys and values can coexist
* with regular entries without ambiguity.
*
*
*
Customization
*
* This abstract class requires a concrete implementation of the backing {@link ConcurrentMap}.
* To customize the behavior, subclasses can provide a specific implementation of the internal map.
*
*
*
Usage Example
*
{@code
* // Example subclass using ConcurrentHashMap as the backing map
* public class MyConcurrentNullSafeMap extends AbstractConcurrentNullSafeMap {
* public MyConcurrentNullSafeMap() {
* super(new ConcurrentHashMap<>());
* }
* }
*
* // Using the map
* MyConcurrentNullSafeMap map = new MyConcurrentNullSafeMap<>();
* map.put(null, "nullKey");
* map.put("key", null);
* System.out.println(map.get(null)); // Outputs: nullKey
* System.out.println(map.get("key")); // Outputs: null
* }
*
*
Additional Notes
*
*
Equality and HashCode: Ensures consistent behavior for equality and hash code computation
* in compliance with the {@link Map} contract.
*
Thread Safety: The thread safety of this class is determined by the thread safety of the
* underlying {@link ConcurrentMap} implementation.
*
Sentinel Objects: The {@link NullSentinel#NULL_KEY} and {@link NullSentinel#NULL_VALUE} are used
* internally to mask {@code null} keys and values.
*
*
* @param the type of keys maintained by this map
* @param the type of mapped values
*
* @author John DeRegnaucourt ([email protected])
*
* Copyright (c) Cedar Software LLC
*
* 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
*
* 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.
* @see ConcurrentMap
* @see java.util.concurrent.ConcurrentHashMap
*/
public abstract class AbstractConcurrentNullSafeMap implements ConcurrentMap {
// Sentinel objects to represent null keys and values
protected enum NullSentinel {
NULL_KEY, NULL_VALUE
}
// Internal ConcurrentMap storing Objects
protected final ConcurrentMap