Class AbstractConcurrentNullSafeMap<K,V>

java.lang.Object
com.cedarsoftware.util.AbstractConcurrentNullSafeMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
ConcurrentMap<K,V>, Map<K,V>
Direct Known Subclasses:
ConcurrentHashMapNullSafe, ConcurrentNavigableMapNullSafe

public abstract class AbstractConcurrentNullSafeMap<K,V> extends Object implements ConcurrentMap<K,V>
An abstract thread-safe implementation of the ConcurrentMap interface that allows null keys and null values. Internally, AbstractConcurrentNullSafeMap uses sentinel objects to represent null keys and values, enabling safe handling of null while maintaining compatibility with ConcurrentMap behavior.

Key Features

Null Key and Value Handling

The AbstractConcurrentNullSafeMap uses internal sentinel objects (AbstractConcurrentNullSafeMap.NullSentinel) to distinguish null keys and values from actual entries. This ensures that null keys and values can coexist with regular entries without ambiguity.

Customization

This abstract class requires a concrete implementation of the backing ConcurrentMap. To customize the behavior, subclasses can provide a specific implementation of the internal map.

Usage Example


 // Example subclass using ConcurrentHashMap as the backing map
 public class MyConcurrentNullSafeMap<K, V> extends AbstractConcurrentNullSafeMap<K, V> {
     public MyConcurrentNullSafeMap() {
         super(new ConcurrentHashMap<>());
     }
 }

 // Using the map
 MyConcurrentNullSafeMap<String, String> 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

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

License

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 Also:
  • Field Details

  • Constructor Details

    • AbstractConcurrentNullSafeMap

      protected AbstractConcurrentNullSafeMap(ConcurrentMap<Object,Object> internalMap)
      Constructs a new AbstractConcurrentNullSafeMap with the provided internal map.
      Parameters:
      internalMap - the internal ConcurrentMap to use
  • Method Details

    • maskNullKey

      protected Object maskNullKey(K key)
    • unmaskNullKey

      protected K unmaskNullKey(Object key)
    • maskNullValue

      protected Object maskNullValue(V value)
    • unmaskNullValue

      protected V unmaskNullValue(Object value)
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Specified by:
      getOrDefault in interface ConcurrentMap<K,V>
      Specified by:
      getOrDefault in interface Map<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface ConcurrentMap<K,V>
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface ConcurrentMap<K,V>
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
      Specified by:
      computeIfAbsent in interface ConcurrentMap<K,V>
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • compute

      public V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      compute in interface ConcurrentMap<K,V>
      Specified by:
      compute in interface Map<K,V>
    • merge

      public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
      Specified by:
      merge in interface ConcurrentMap<K,V>
      Specified by:
      merge in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • equals

      public boolean equals(Object o)
      Overrides the equals method to ensure proper comparison between two maps. Two maps are considered equal if they contain the same key-value mappings.
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
      Parameters:
      o - the object to be compared for equality with this map
      Returns:
      true if the specified object is equal to this map
    • hashCode

      public int hashCode()
      Overrides the hashCode method to ensure consistency with equals. The hash code of a map is defined to be the sum of the hash codes of each entry in the map.
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this map
    • toString

      public String toString()
      Overrides the toString method to provide a string representation of the map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space).
      Overrides:
      toString in class Object
      Returns:
      a string representation of this map