Interface Context

All Superinterfaces:
ContextView

public interface Context extends ContextView
A key/value store 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 implementations are thread-safe and immutable: mutative operations like put(Object, Object) will in fact return a new Context instance.

Note that contexts are optimized for low cardinality key/value storage, and a user might want to associate a dedicated mutable structure to a single key to represent his own context instead of using multiple put(java.lang.Object, java.lang.Object), which could be more costly. Past five user key/value pair, the Context will use a copy-on-write implementation backed by a new Map on each put(java.lang.Object, java.lang.Object).

Author:
Stephane Maldini
  • Method Details

    • empty

      static Context empty()
      Return an empty Context
      Returns:
      an empty Context
    • of

      static Context of(Object key, Object value)
      Create a Context pre-initialized with one key-value pair.
      Parameters:
      key - the key to initialize.
      value - the value for the key.
      Returns:
      a Context with a single entry.
      Throws:
      NullPointerException - if either key or value are null
    • of

      static Context of(Object key1, Object value1, Object key2, Object value2)
      Create a Context pre-initialized with two key-value pairs.
      Parameters:
      key1 - the first key to initialize.
      value1 - the value for the first key.
      key2 - the second key to initialize.
      value2 - the value for the second key.
      Returns:
      a Context with two entries.
      Throws:
      NullPointerException - if any key or value is null
    • of

      static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3)
      Create a Context pre-initialized with three key-value pairs.
      Parameters:
      key1 - the first key to initialize.
      value1 - the value for the first key.
      key2 - the second key to initialize.
      value2 - the value for the second key.
      key3 - the third key to initialize.
      value3 - the value for the third key.
      Returns:
      a Context with three entries.
      Throws:
      NullPointerException - if any key or value is null
    • of

      static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4)
      Create a Context pre-initialized with four key-value pairs.
      Parameters:
      key1 - the first key to initialize.
      value1 - the value for the first key.
      key2 - the second key to initialize.
      value2 - the value for the second key.
      key3 - the third key to initialize.
      value3 - the value for the third key.
      key4 - the fourth key to initialize.
      value4 - the value for the fourth key.
      Returns:
      a Context with four entries.
      Throws:
      NullPointerException - if any key or value is null
    • of

      static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4, Object key5, Object value5)
      Create a Context pre-initialized with five key-value pairs.
      Parameters:
      key1 - the first key to initialize.
      value1 - the value for the first key.
      key2 - the second key to initialize.
      value2 - the value for the second key.
      key3 - the third key to initialize.
      value3 - the value for the third key.
      key4 - the fourth key to initialize.
      value4 - the value for the fourth key.
      key5 - the fifth key to initialize.
      value5 - the value for the fifth key.
      Returns:
      a Context with five entries.
      Throws:
      NullPointerException - if any key or value is null
    • of

      static Context of(Map<?,?> map)
      Create a Context out of a Map. Prefer this method if you're somehow incapable of checking keys are all distinct in other of(Object, Object, Object, Object, Object, Object, Object, Object) implementations.
      Implementation Note:
      this method compacts smaller maps into a relevant fields-based implementation when map size is less than 6.
    • of

      static Context of(ContextView contextView)
      Create a Context out of a ContextView, enabling write API on top of the read-only view. If the ContextView is already a Context, return the same instance.
      Parameters:
      contextView - the ContextView to convert (or cast) to Context
      Returns:
      the converted Context for further modifications
    • readOnly

      default ContextView readOnly()
      Switch to the ContextView interface, which only allows reading from the context.
      Returns:
      the ContextView of this context
    • put

      Context put(Object key, Object value)
      Create a new Context that contains all current key/value pairs plus the given key/value pair. If that key existed in the current Context, its associated value is replaced in the resulting Context.
      Parameters:
      key - the key to add/update in the new Context
      value - the value to associate to the key in the new Context
      Returns:
      a new Context including the provided key/value
      Throws:
      NullPointerException - if either the key or value are null
    • putNonNull

      default Context putNonNull(Object key, @Nullable Object valueOrNull)
      Create a new Context that contains all current key/value pairs plus the given key/value pair only if the value is not null. If that key existed in the current Context, its associated value is replaced in the resulting Context.
      Parameters:
      key - the key to add/update in the new Context
      valueOrNull - the value to associate to the key in the new Context, null to ignore the operation
      Returns:
      a new Context including the provided key/value, or the same Context if value is null
      Throws:
      NullPointerException - if the key is null
    • delete

      Context delete(Object key)
      Return a new Context that will resolve all existing keys except the removed one, key.

      Note that if this Context doesn't contain the key, this method simply returns this same instance.

      Parameters:
      key - the key to remove.
      Returns:
      a new Context that doesn't include the provided key
    • putAll

      default Context putAll(ContextView other)
      Create a new Context by merging the content of this context and a given ContextView. If the other context is empty, the same Context instance is returned.
      Parameters:
      other - the other ContextView from which to copy entries
      Returns:
      a new Context with a merge of the entries from this context and the given context.
    • putAllMap

      default Context putAllMap(Map<?,?> from)
      Create a new Context by merging the content of this context and a given Map. If the Map is empty, the same Context instance is returned.
      Parameters:
      from - the Map from which to include entries in the resulting Context.
      Returns:
      a new Context with a merge of the entries from this context and the given Map.
    • putAll

      @Deprecated default Context putAll(Context context)
      Deprecated.
      will be removed in 3.5, kept for backward compatibility with 3.3. Until then if you need to work around the deprecation, use putAll(ContextView) combined with readOnly()
      Parameters:
      context - the Context from which to copy entries
      Returns:
      a new Context with a merge of the entries from this context and the given context.