Class DefaultContextMap

java.lang.Object
io.servicetalk.concurrent.internal.DefaultContextMap
All Implemented Interfaces:
ContextMap

public final class DefaultContextMap extends Object implements ContextMap
Default implementation of ContextMap.

Note: it's not thread-safe!

  • Constructor Details

    • DefaultContextMap

      public DefaultContextMap()
      Creates a new instance.
  • Method Details

    • size

      public int size()
      Description copied from interface: ContextMap
      Determine the number of ContextMap.Key-value pairs in this ContextMap.
      Specified by:
      size in interface ContextMap
      Returns:
      the number of ContextMap.Key-value pairs in this ContextMap.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: ContextMap
      Determine if there are no entries in this ContextMap.
      Specified by:
      isEmpty in interface ContextMap
      Returns:
      true if there are no entries in this ContextMap.
    • containsKey

      public boolean containsKey(ContextMap.Key<?> key)
      Description copied from interface: ContextMap
      Determine if this ContextMap contains an entry corresponding to the key.
      Specified by:
      containsKey in interface ContextMap
      Parameters:
      key - The ContextMap.Key to lookup.
      Returns:
      true if this ContextMap contains an entry corresponding to the key, false otherwise.
    • containsValue

      public boolean containsValue(@Nullable Object value)
      Description copied from interface: ContextMap
      Determine if this ContextMap contains an entry with the specified value.
      Specified by:
      containsValue in interface ContextMap
      Parameters:
      value - The value to lookup.
      Returns:
      true if this ContextMap contains one or more entries with the specified value, false otherwise.
    • get

      @Nullable public <T> T get(ContextMap.Key<T> key)
      Description copied from interface: ContextMap
      Get the value associated with the key, or null if no value is associated.
      Specified by:
      get in interface ContextMap
      Type Parameters:
      T - The anticipated type of object associated with the key.
      Parameters:
      key - The ContextMap.Key to lookup.
      Returns:
      The value associated with the key, or null if no value is associated. null can also indicate the value associated with the key is null (if null values are supported by the implementation).
    • getOrDefault

      @Nullable public <T> T getOrDefault(ContextMap.Key<T> key, T defaultValue)
      Description copied from interface: ContextMap
      Get the value associated with the key, or defaultValue if no value is associated.
      Specified by:
      getOrDefault in interface ContextMap
      Type Parameters:
      T - The anticipated type of object associated with the key.
      Parameters:
      key - The ContextMap.Key to lookup.
      defaultValue - The value to return if no value is associated with the key.
      Returns:
      The value associated with the key (can return null if null values are supported by the implementation), or defaultValue if no value is associated.
    • put

      @Nullable public <T> T put(ContextMap.Key<T> key, @Nullable T value)
      Description copied from interface: ContextMap
      Put a new entry into this ContextMap.
      Specified by:
      put in interface ContextMap
      Type Parameters:
      T - The type of object associated with the key.
      Parameters:
      key - The ContextMap.Key used to index the value.
      value - The value to put.
      Returns:
      The previous value associated with the key, or null if there was none. null can also indicate the value associated with the key was null (if null values are supported by the implementation).
    • putIfAbsent

      @Nullable public <T> T putIfAbsent(ContextMap.Key<T> key, @Nullable T value)
      Description copied from interface: ContextMap
      Put a new entry into this ContextMap if this map does not already contain this key or is mapped to null.
      Specified by:
      putIfAbsent in interface ContextMap
      Type Parameters:
      T - The type of object associated with the key.
      Parameters:
      key - The ContextMap.Key used to index the value.
      value - The value to put.
      Returns:
      The previous value associated with the key, or null if there was none. null can also indicate the value associated with the key was null (if null values are supported by the implementation).
    • computeIfAbsent

      @Nullable public <T> T computeIfAbsent(ContextMap.Key<T> key, Function<ContextMap.Key<T>,T> computeFunction)
      Description copied from interface: ContextMap
      Computes a new entry for this ContextMap if this map does not already contain this key or is mapped to null.
      Specified by:
      computeIfAbsent in interface ContextMap
      Type Parameters:
      T - The type of object associated with the key.
      Parameters:
      key - The ContextMap.Key used to index a new value.
      computeFunction - The function to compute a new value. Implementation may invoke this function multiple times if concurrent threads attempt modifying this context map, result is expected to be idempotent.
      Returns:
      The current (existing or computed) value associated with the key, or null if the computed value is null.
    • putAll

      public void putAll(ContextMap map)
      Description copied from interface: ContextMap
      Put all the entries into this ContextMap.
      Specified by:
      putAll in interface ContextMap
      Parameters:
      map - The entries to insert into this ContextMap.
    • putAll

      public void putAll(Map<ContextMap.Key<?>,Object> map)
      Description copied from interface: ContextMap
      Put all the entries into this ContextMap.
      Specified by:
      putAll in interface ContextMap
      Parameters:
      map - The entries to insert into this ContextMap.
    • remove

      @Nullable public <T> T remove(ContextMap.Key<T> key)
      Description copied from interface: ContextMap
      Remove an entry from this ContextMap, and get the previous value (if one exists).
      Specified by:
      remove in interface ContextMap
      Type Parameters:
      T - The type of object associated with the key.
      Parameters:
      key - The ContextMap.Key which identifies an entry for removal.
      Returns:
      The previous value associated with the key, or null if there was none. null can also indicate the value associated with the key was null (if null values are supported by the implementation). If the ContextMap implementation is immutable this may be a new object.
    • clear

      public void clear()
      Description copied from interface: ContextMap
      Clear the contents of this ContextMap.
      Specified by:
      clear in interface ContextMap
    • forEach

      @Nullable public ContextMap.Key<?> forEach(BiPredicate<ContextMap.Key<?>,Object> consumer)
      Description copied from interface: ContextMap
      Iterate over the entries contained in this ContextMap.
      Specified by:
      forEach in interface ContextMap
      Parameters:
      consumer - Each entry will be passed as key and value arguments to this BiPredicate. A consumer predicate should return true if it wants to keep iterating or false to stop iteration at the current entry.
      Returns:
      null if consumer iterated through all entries or the ContextMap.Key at which the iteration stopped.
    • copy

      public ContextMap copy()
      Description copied from interface: ContextMap
      Create an isolated copy of the current map. The return value contents are the same as this ContextMap but modifications to this ContextMap are not visible in the return value, and visa-versa.
      Specified by:
      copy in interface ContextMap
      Returns:
      an isolated copy of the current map. The contents are the same as this ContextMap but modifications to this ContextMap are not visible in the return value, and visa-versa.
    • hashCode

      public int hashCode()
      Description copied from interface: ContextMap
      Returns the hash code value for this ContextMap.
      Specified by:
      hashCode in interface ContextMap
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this ContextMap, taking into account the hash codes of each key-value mapping entries this ContextMap contains.
      See Also:
    • equals

      public boolean equals(Object o)
      Description copied from interface: ContextMap
      Determines if the specified object is equal to this ContextMap.
      Specified by:
      equals in interface ContextMap
      Overrides:
      equals in class Object
      Parameters:
      o - object to be compared for equality with this ContextMap.
      Returns:
      true if the passed object is a ContextMap and has the same key-value mapping entries.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object