Record Class FlowContext

java.lang.Object
java.lang.Record
com.fayupable.logged.core.model.FlowContext
Record Components:
traceId - a correlation identifier shared by every call in the same chain, generated once when the chain's root method is entered
depth - the zero-based nesting level of the current call within the chain; the root call has depth 0, a call it makes directly has depth 1, and so on

public record FlowContext(String traceId, int depth) extends Record
Tracks the position of the current method invocation within a chain of nested @Logged calls on the same thread.

When a @Logged method calls another @Logged method directly or indirectly, this context lets an interceptor recognize that both calls belong to the same logical operation and record their relative order. Combined with MethodInvocationEvent, this makes it possible to reconstruct a call chain such as methodA -> methodB -> methodC in log output, and to see exactly where in that chain a failure occurred.

This class holds only a correlation identifier and a depth counter; it does not store method arguments, return values, or any other invocation data, consistent with the rest of this library.

Instances are managed per-thread by the interceptor, typically through a ThreadLocal. This class itself has no thread-affinity logic; it is a plain, immutable-per-call data holder.

  • Constructor Summary

    Constructors
    Constructor
    Description
    FlowContext(String traceId, int depth)
    Creates an instance of a FlowContext record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the value of the depth record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Creates the context for a nested call one level deeper than this one, keeping the same traceId() so it remains part of the same chain.
    Creates the root context for a new call chain, deriving the trace id from a single 64-bit random value.
    final String
    Returns a string representation of this record class.
    Returns the value of the traceId record component.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • FlowContext

      public FlowContext(String traceId, int depth)
      Creates an instance of a FlowContext record class.
      Parameters:
      traceId - the value for the traceId record component
      depth - the value for the depth record component
  • Method Details

    • root

      public static FlowContext root()
      Creates the root context for a new call chain, deriving the trace id from a single 64-bit random value.

      This identifier only needs to be unique enough to avoid confusing concurrent call chains in log output; it is not a security token. A single value from ThreadLocalRandom is used deliberately instead of UUID.randomUUID(), since the latter relies on SecureRandom internally and produces twice the amount of random data than this use case requires. ThreadLocalRandom avoids any shared-state locking, making this call cheap even under high concurrency.

      Returns:
      a new root FlowContext
    • next

      public FlowContext next()
      Creates the context for a nested call one level deeper than this one, keeping the same traceId() so it remains part of the same chain.
      Returns:
      a new FlowContext with the same trace id and depth incremented by one
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • traceId

      public String traceId()
      Returns the value of the traceId record component.
      Returns:
      the value of the traceId record component
    • depth

      public int depth()
      Returns the value of the depth record component.
      Returns:
      the value of the depth record component