Class Context


  • @Experimental("Context support is a new experimental API introduced in Mutiny 1.3.0")
    public final class Context
    extends java.lang.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 Subscriber that implements ContextSupport.

    Context keys are represented as String while values can be from heterogeneous types.

    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:
    Uni.withContext(BiFunction), Uni.attachContext(), UniSubscribe, Multi.withContext(BiFunction), Multi.attachContext(), MultiSubscribe
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(java.lang.String key)
      Checks whether the context has an entry for a given key.
      Context delete​(java.lang.String key)
      Delete an entry for a given key, if present.
      static Context empty()
      Creates a new empty context.
      boolean equals​(java.lang.Object other)  
      static Context from​(java.util.Map<java.lang.String,​?> entries)
      Creates a context by copying the entries from a Map.
      <T> T get​(java.lang.String key)
      Get a value for a key.
      <T> T getOrElse​(java.lang.String key, java.util.function.Supplier<? extends T> alternativeSupplier)
      Get a value for a key, or provide an alternative value when not present.
      int hashCode()  
      boolean isEmpty()
      Test whether the context is empty.
      java.util.Set<java.lang.String> keys()
      Gives the set of keys present in the context at the time the method is being called.
      static Context of​(java.lang.Object... entries)
      Creates a context from key / value pairs.
      Context put​(java.lang.String key, java.lang.Object value)
      Stores a value for a given key.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • empty

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

        public static Context of​(java.lang.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:
        java.lang.IllegalArgumentException - when entries is not balanced
        java.lang.NullPointerException - when entries is null
      • from

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

        public boolean contains​(java.lang.String 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
      • get

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

        public <T> T getOrElse​(java.lang.String key,
                               java.util.function.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
      • put

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

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

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

        public java.util.Set<java.lang.String> 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​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object