Class WrapperWithContext<T>


  • public abstract class WrapperWithContext<T>
    extends Wrapper<T>
    Wrapper that also contains a fixed context snapshot.
    Author:
    Sjoerd Talsma
    • Constructor Detail

      • WrapperWithContext

        protected WrapperWithContext​(ContextSnapshot snapshot,
                                     T delegate)
        Creates a new Wrapper with the specified context snapshot.
        Parameters:
        snapshot - The context snapshot (required, non-null)
        delegate - The wrapped delegate object providing core functionality
      • WrapperWithContext

        protected WrapperWithContext​(ContextSnapshotSupplier supplier,
                                     T delegate)
        Wraps the delegate and provides a context snapshot.

        Note: Make sure the supplier function does not obtain the context snapshot from any threadlocal storage! The wrapper is designed to propagate contexts from one thread to another. Therefore, the snapshot must be captured in the source thread and reactivated in the target thread. If unsure, please use the constructor with snapshot instead.

        Parameters:
        supplier - The supplier for the (fixed!) context snapshot. This can be a straightforward 'holder' object or an ongoing background call. Please do not make this supplier function access any ThreadLocal value, as the wrapper is designed to propagate the snapshot from thread to thread!
        delegate - The delegate object to be wrapped.
        See Also:
        WrapperWithContext(ContextSnapshot, Object)
    • Method Detail

      • snapshot

        protected ContextSnapshot snapshot()
        Calls the supplier for the context snapshot, making sure it is called only once for this wrapper instance.
        Returns:
        The snapshot value.
      • toString

        public String toString()
        Overrides:
        toString in class Wrapper<T>
        Returns:
        The class name and the delegate string representation.