Interface ContextView

All Known Subinterfaces:
Context

public interface ContextView
A read-only view of a collection of key/value pairs that is propagated between components such as operators via the context protocol. Contexts are ideal to transport orthogonal information such as tracing or security tokens.

Context is an immutable variant of the same key/value pairs structure which exposes a write API that returns new instances on each write.

Since:
3.4.0
Author:
Simon Baslé
  • Method Details

    • get

      <T> T get(Object key)
      Resolve a value given a key that exists within the Context, or throw a NoSuchElementException if the key is not present.
      Type Parameters:
      T - an unchecked casted generic for fluent typing convenience
      Parameters:
      key - a lookup key to resolve the value within the context
      Returns:
      the value resolved for this key (throws if key not found)
      Throws:
      NoSuchElementException - when the given key is not present
      See Also:
    • get

      default <T> T get(Class<T> key)
      Resolve a value given a type key within the Context.
      Type Parameters:
      T - an unchecked casted generic for fluent typing convenience
      Parameters:
      key - a type key to resolve the value within the context
      Returns:
      the value resolved for this type key (throws if key not found)
      Throws:
      NoSuchElementException - when the given type key is not present
      See Also:
    • getOrDefault

      default <T> @Nullable T getOrDefault(Object key, @Nullable T defaultValue)
      Resolve a value given a key within the Context. If unresolved return the passed default value.
      Parameters:
      key - a lookup key to resolve the value within the context
      defaultValue - a fallback value if key doesn't resolve
      Returns:
      the value resolved for this key, or the given default if not present
    • getOrEmpty

      default <T> Optional<T> getOrEmpty(Object key)
      Resolve a value given a key within the Context.
      Parameters:
      key - a lookup key to resolve the value within the context
      Returns:
      an Optional of the value for that key.
    • hasKey

      boolean hasKey(Object key)
      Return true if a particular key resolves to a value within the Context.
      Parameters:
      key - a lookup key to test for
      Returns:
      true if this context contains the given key
    • isEmpty

      default boolean isEmpty()
      Return true if the Context is empty.
      Returns:
      true if the Context is empty.
    • size

      int size()
      Return the size of this Context, the number of immutable key/value pairs stored inside it.
      Returns:
      the size of the Context
    • stream

      Stream key/value pairs from this Context
      Returns:
      a Stream of key/value pairs held by this context
    • forEach

      default void forEach(BiConsumer<Object,Object> action)
      Perform the given action for each entry in this ContextView. If the action throws an exception, it is immediately propagated to the caller and the remaining items will not be processed.
      Parameters:
      action - The action to be performed for each entry
      Throws:
      NullPointerException - if the specified action is null