Interface RequestLocal<T>

Type Parameters:
T - the value type

public interface RequestLocal<T>
Value that is resolved in the current RequestContext, if one is active. Conceptually similar to a ThreadLocal, but tied to the current RequestContext instead of the current thread.

Note that the request context can be propagated to background threads such as event listener threads and repository hook handler threads. As a result, the object stored in a RequestLocal could be accessed from different threads and care should be taken to ensure the stored object is thread safe when interacting with it from multiple threads (e.g. the request thread, an asynchronous event listener and/or a repository hook).

Since:
7.9
  • Method Summary

    Modifier and Type
    Method
    Description
    compute(Function<? super T,? extends T> mappingFunction)
    Attempts to compute the value given the current value.
    computeIfAbsent(Supplier<? extends T> missingValueSupplier)
    Returns the value, if present.
    get()
     
    void
    ifPresent(Consumer<? super T> consumer)
    If a value is present, invoke the specified consumer with the value, otherwise do nothing.
    boolean
     
    boolean
     
    Removes the value from the current RequestContext, if present.
    removeIf(Predicate<? super T> predicate)
    Removes the value from the current RequestContext, if a non-null value is present and the predicate matches the value.
    boolean
    set(T value)
    Sets the value.
  • Method Details

    • compute

      T compute(@Nonnull Function<? super T,? extends T> mappingFunction)
      Attempts to compute the value given the current value. The value returned by the mappingFunction is stored as the value for current RequestContext.

      If there is no current RequestContext, the mappingFunction is still invoked but the computed value is not stored.

      Parameters:
      mappingFunction - function that computes the value given the current value which may be null if no value is present.
      Returns:
      the computed value
    • computeIfAbsent

      T computeIfAbsent(@Nonnull Supplier<? extends T> missingValueSupplier)
      Returns the value, if present. If an RequestContext is active, but no value is set, the value supplied by the missingValueSupplier is stored and returned.

      If no RequestContext is active, missingValueSupplier is still invoked, but the computed value is not stored.

      Parameters:
      missingValueSupplier - supplier of the value when
      Returns:
      the value, possibly supplied by missingValueSupplier
    • get

      @Nullable T get()
      Returns:
      the value, or null if not set or no RequestContext is active
    • ifPresent

      void ifPresent(@Nonnull Consumer<? super T> consumer)
      If a value is present, invoke the specified consumer with the value, otherwise do nothing.
      Parameters:
      consumer - block to be executed if a value is present
    • isActive

      boolean isActive()
      Returns:
      true is a RequestContext is currently active, otherwise false
    • isPresent

      boolean isPresent()
      Returns:
      true when a value other than null is defined in the current RequestContext.
    • remove

      @Nullable T remove()
      Removes the value from the current RequestContext, if present.
      Returns:
      the removed value, if the value was removed; otherwise null
    • removeIf

      @Nullable T removeIf(@Nonnull Predicate<? super T> predicate)
      Removes the value from the current RequestContext, if a non-null value is present and the predicate matches the value. If no value is present, the predicate will not be called. The Predicate will never be called with a null value.
      Parameters:
      predicate - the predicate
      Returns:
      the removed value, if the value was removed; otherwise null
    • set

      boolean set(@Nullable T value)
      Sets the value. Setting a null value is equivalent to calling remove().
      Parameters:
      value - the value
      Returns:
      true if the value was set, or false if no RequestContext is active