Class Context

java.lang.Object
io.smallrye.mutiny.Context

public final class Context extends Object
A context allows sharing key / value entries along with a subscriber in a Mutiny pipeline, so all operators can share implicit data for a given subscription.

A context is provided by a UniSubscriber or Flow.Subscriber that implements ContextSupport.

Context keys and values can be from heterogeneous types. Keys must have proper Object.equals(Object) and Object.hashCode() implementations.

Context instances are thread-safe. Internal storage is not allocated until the first entry is being added.

Contexts shall be primarily used to share transient data used for networked I/O processing such as correlation identifiers, tokens, etc. They should not be used as general-purpose data structures that are frequently updated and that hold large amounts of data.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks whether the context has an entry for a given key.
    boolean
    Deprecated, for removal: This API element is subject to removal in a future version.
    Delete an entry for a given key, if present.
    Deprecated, for removal: This API element is subject to removal in a future version.
    static Context
    Creates a new empty context.
    boolean
    equals(Object other)
     
    static Context
    from(Map<?,?> entries)
    Creates a context by copying the entries from a Map.
    <T> T
    get(Object key)
    Get a value for a key.
    <T> T
    get(String key)
    Deprecated, for removal: This API element is subject to removal in a future version.
    <T> T
    getOrElse(Object key, Supplier<? extends T> alternativeSupplier)
    Get a value for a key, or provide an alternative value when not present.
    <T> T
    getOrElse(String key, Supplier<? extends T> alternativeSupplier)
    Deprecated, for removal: This API element is subject to removal in a future version.
    int
     
    boolean
    Test whether the context is empty.
    Gives the set of keys present in the context at the time the method is being called.
    static Context
    of(Object... entries)
    Creates a context from key / value pairs.
    put(Object key, Object value)
    Stores a value for a given key.
    put(String key, Object value)
    Deprecated, for removal: This API element is subject to removal in a future version.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • empty

      public static Context empty()
      Creates a new empty context.
      Returns:
      the context
    • of

      public static Context of(Object... entries)
      Creates a context from key / value pairs.
      Parameters:
      entries - a sequence of key / value pairs, as in key1, value1, key2, value2
      Returns:
      the new context
      Throws:
      IllegalArgumentException - when entries is not balanced
      NullPointerException - when entries is null
    • from

      public static Context from(Map<?,?> entries)
      Creates a context by copying the entries from a Map.
      Parameters:
      entries - the map, cannot be null
      Returns:
      the new context
      Throws:
      NullPointerException - when entries is null
    • contains

      public boolean contains(Object key)
      Checks whether the context has an entry for a given key.
      Parameters:
      key - the key
      Returns:
      true when there is an entry for key, false otherwise
    • contains

      @Deprecated(forRemoval=true) public boolean contains(String key)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • get

      public <T> T get(Object key) throws NoSuchElementException
      Get a value for a key.
      Type Parameters:
      T - the value type
      Parameters:
      key - the key
      Returns:
      the value
      Throws:
      NoSuchElementException - when there is no entry for key
    • get

      @Deprecated(forRemoval=true) public <T> T get(String key) throws NoSuchElementException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Throws:
      NoSuchElementException
    • getOrElse

      public <T> T getOrElse(Object key, Supplier<? extends T> alternativeSupplier)
      Get a value for a key, or provide an alternative value when not present.
      Type Parameters:
      T - the value type
      Parameters:
      key - the key
      alternativeSupplier - the alternative value supplier when there is no entry for key
      Returns:
      the value
    • getOrElse

      @Deprecated(forRemoval=true) public <T> T getOrElse(String key, Supplier<? extends T> alternativeSupplier)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • put

      public Context put(Object key, Object value)
      Stores a value for a given key.
      Parameters:
      key - the key
      value - the value, cannot be null
      Returns:
      this context
    • put

      @Deprecated(forRemoval=true) public Context put(String key, Object value)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • delete

      public Context delete(Object key)
      Delete an entry for a given key, if present.
      Parameters:
      key - the key
      Returns:
      this context
    • delete

      @Deprecated(forRemoval=true) public Context delete(String key)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • isEmpty

      public boolean isEmpty()
      Test whether the context is empty.
      Returns:
      true if the context is empty, false otherwise
    • keys

      public Set<Object> keys()
      Gives the set of keys present in the context at the time the method is being called.

      Note that each call to this method produces a copy.

      Returns:
      the set of keys
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object