Interface ContextSnapshotFactory


public interface ContextSnapshotFactory
Factory for creating ContextSnapshot objects and restoring ThreadLocal values using a context object for which a ContextAccessor exists in the ContextRegistry.
Since:
1.0.3
  • Method Details

    • captureAll

      ContextSnapshot captureAll(Object... contexts)
      Capture values from ThreadLocal and from other context objects using all accessors from a ContextRegistry instance.

      Values captured multiple times are overridden in the snapshot by the order of contexts given as arguments.

      Parameters:
      contexts - context objects to extract values from
      Returns:
      a snapshot with saved context values
    • captureFrom

      ContextSnapshot captureFrom(Object... contexts)
      Create a ContextSnapshot by reading values from the given context objects.

      Values captured multiple times are overridden in the snapshot by the order of contexts given as arguments.

      Parameters:
      contexts - the contexts to read values from
      Returns:
      the created ContextSnapshot
    • setThreadLocalsFrom

      <C> ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, String... keys)
      Read the values specified by keys from the given source context, and if found, use them to set ThreadLocal values. If no keys are provided, all keys are used. Essentially, a shortcut that bypasses the need to create of ContextSnapshot first via captureFrom(Object...), followed by ContextSnapshot.setThreadLocals().
      Type Parameters:
      C - the type of the target context
      Parameters:
      sourceContext - the source context to read values from
      keys - the keys of the values to read. If none provided, all keys are considered.
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.
    • builder

      Creates a builder for configuring the factory.
      Returns:
      an instance that provides defaults, that can be configured to provide to the created ContextSnapshotFactory.