Class ContextLocals

java.lang.Object
io.smallrye.common.vertx.ContextLocals

public class ContextLocals extends Object
Utilities to access Vert.x Context locals.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Optional<T>
    get(String key)
    Gets the value from the context local associated with the given key.
    static <T> T
    get(String key, T def)
    Gets the value from the context local associated with the given key.
    static <T> Optional<T>
    Gets the value associated with the given key from the parent context.
    static <T> T
    getFromParent(String key, T def)
    Gets the value associated with the given key from the parent context.
    static io.vertx.core.Context
    Gets the parent context of the current context.
    static <T> void
    put(String key, T value)
    Stores the given key/value in the context local.
    static <T> boolean
    putInParent(String key, T value)
    Puts the given key/value in the parent context.
    static boolean
    Removes the value associated with the given key from the context locals.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • get

      public static <T> Optional<T> get(String key)
      Gets the value from the context local associated with the given key.
      Type Parameters:
      T - the expected type of the associated value
      Parameters:
      key - the key, must not be null
      Returns:
      an optional containing the associated value if any, empty otherwise.
    • get

      public static <T> T get(String key, T def)
      Gets the value from the context local associated with the given key. If there is no associated value, it returns the given default.
      Type Parameters:
      T - the expected type of the associated value
      Parameters:
      key - the key, must not be null
      def - the default value returned if there is no associated value with the given key. Can be null
      Returns:
      the associated value if any, the given default otherwise.
    • put

      public static <T> void put(String key, T value)
      Stores the given key/value in the context local. This method overwrite the existing value if any.
      Type Parameters:
      T - the expected type of the associated value
      Parameters:
      key - the key, must not be null
      value - the value, must not be null
    • remove

      public static boolean remove(String key)
      Removes the value associated with the given key from the context locals.
      Parameters:
      key - the key, must not be null
      Returns:
      true if there were a value associated with the given key. false otherwise.
    • getParentContext

      public static io.vertx.core.Context getParentContext()
      Gets the parent context of the current context.
      Returns:
      the parent context if any, null otherwise.
    • putInParent

      public static <T> boolean putInParent(String key, T value)
      Puts the given key/value in the parent context.

      If the parent context cannot be found, it throws an IllegalStateException. If the parent context is a root context, it throws an IllegalStateException. If the parent context is a duplicated context, the value is put in the parent context locals.

      Values put in the parent context can only be written once, subsequent writes will be ignored. However, all the children of the parent context will have access to the value using getFromParent(String).

      Type Parameters:
      T - the expected type of the associated value
      Parameters:
      key - the key, must not be null
      value - the value, must not be null
      Returns:
      true if the value was put in the parent context, false otherwise.
    • getFromParent

      public static <T> Optional<T> getFromParent(String key)
      Gets the value associated with the given key from the parent context.

      If the parent context is not set, it returns an empty optional.

      Type Parameters:
      T - the expected type of the associated value
      Parameters:
      key - the key, must not be null
      Returns:
      an optional containing the associated value if any, empty otherwise.
    • getFromParent

      public static <T> T getFromParent(String key, T def)
      Gets the value associated with the given key from the parent context.

      If the parent context is not set, it returns the given default. If there is a parent context, but, there is no associated value with the given key, it returns the given default.

      Type Parameters:
      T - the expected type of the associated value
      Parameters:
      key - the key, must not be null
      def - the default value returned if there is no associated value with the given key.
      Returns:
      the associated value if any, the given default otherwise.