Class Context

java.lang.Object
dev.enola.common.context.Context
All Implemented Interfaces:
AutoCloseable

public class Context extends Object implements AutoCloseable
Contexts đŸ§¿ put things into perspective!

Contexts are "hierarchical", and child contexts "mask" keys in their parent.

This class is NOT thread safe. Might you want to use TLC instead?

  • Constructor Details

  • Method Details

    • push

      public <K extends Enum<K> & Context.Key<T>, T> Context push(K key, T value)
      Push, but not too hard…
      Parameters:
      key - Key.
      value - Value to associate with the key.
      Returns:
      this, for chaining.
      Throws:
      IllegalStateException - if this Context already has another value for the key.
    • push

      public <T> void push(Context.Key<T> key, T instance)
    • push

      public <T> Context push(Class<T> key, T value)
    • get

      public <K extends Enum<K> & Context.Key<T>, T> T get(K key)
      Get the value for the given key, from this or its parent context.

      Never null, but may throw IllegalStateException if not available.

      Use optional(Enum) to check if key is available in Context.

    • get

      public <T> T get(Class<T> key)
      Gets the instance of Class, from this or its parent context.

      Never null, but may throw IllegalStateException if not available.

      Use optional(Class) to check if key is available in Context.

    • optional

      public <T> Optional<T> optional(Class<T> key)
      Gets the instance of Class, from this or its parent context, if available.

      Use get(Class) if key must be available in Context.

    • optional

      public <T, K extends Enum<K> & Context.Key<T>> Optional<T> optional(K key)
      Get the value for the given key, from this or its parent context, if available.

      Use get(Enum) if key must be available in Context.

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public void close()
      Close this context. Don't use it anymore!
      Specified by:
      close in interface AutoCloseable