Class ContextManager<C extends Contextual<C>>

  • Type Parameters:
    C - the public type of the contextual object
    All Implemented Interfaces:
    java.util.function.Supplier<C>

    public final class ContextManager<C extends Contextual<C>>
    extends java.lang.Object
    implements java.util.function.Supplier<C>
    A context manager for a Contextual type.
    Author:
    David M. Lloyd
    • Constructor Summary

      Constructors 
      Constructor Description
      ContextManager​(java.lang.Class<C> type)
      Construct a new instance, with a name matching the class name of the given type.
      ContextManager​(java.lang.Class<C> type, java.lang.String name)
      Construct a new instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      C get()
      Get the currently active context, possibly examining the per-thread or global defaults.
      C getClassLoaderDefault​(java.lang.ClassLoader classLoader)
      Get the class loader default instance.
      C getGlobalDefault()
      Get the global default context instance.
      java.util.function.Supplier<C> getPrivilegedSupplier()
      Get a privileged supplier for this context manager which returns the currently active context without a permission check.
      C getThreadDefault()
      Get the per-thread default context instance.
      void setClassLoaderDefault​(java.lang.ClassLoader classLoader, C classLoaderDefault)
      Set the per-class loader default instance supplier.
      void setClassLoaderDefaultSupplier​(java.lang.ClassLoader classLoader, java.util.function.Supplier<C> supplier)
      Set the per-class loader default instance supplier.
      void setGlobalDefault​(C globalDefault)
      Set the global default instance.
      void setGlobalDefaultSupplier​(java.util.function.Supplier<C> supplier)
      Set the global default instance supplier.
      boolean setGlobalDefaultSupplierIfNotSet​(java.util.function.Supplier<java.util.function.Supplier<C>> supplierSupplier)
      Set the global default instance supplier, but only if it was not already set.
      void setThreadDefault​(C threadDefault)
      Set the per-thread default instance.
      void setThreadDefaultSupplier​(java.util.function.Supplier<C> supplier)
      Set the per-thread default instance supplier.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ContextManager

        public ContextManager​(java.lang.Class<C> type)
        Construct a new instance, with a name matching the class name of the given type.
        Parameters:
        type - the type class of the context object (must not be null)
      • ContextManager

        public ContextManager​(java.lang.Class<C> type,
                              java.lang.String name)
        Construct a new instance.
        Parameters:
        type - the type class of the context object (must not be null)
        name - the name to use for permission checks (must not be null or empty)
    • Method Detail

      • getGlobalDefault

        public C getGlobalDefault()
        Get the global default context instance. Note that the global default is determined by way of a Supplier so the returned value may vary from call to call, depending on the policy of that Supplier.
        Returns:
        the global default, or null if none is installed or available
      • setGlobalDefaultSupplier

        public void setGlobalDefaultSupplier​(java.util.function.Supplier<C> supplier)
        Set the global default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getGlobalDefault() will obtain results consistent with a general expectation of stability.
        Parameters:
        supplier - the supplier, or null to remove the global default
      • setGlobalDefaultSupplierIfNotSet

        public boolean setGlobalDefaultSupplierIfNotSet​(java.util.function.Supplier<java.util.function.Supplier<C>> supplierSupplier)
        Set the global default instance supplier, but only if it was not already set. If no supplier is set, the given supplier supplier is queried to get the new value to set.
        Parameters:
        supplierSupplier - the supplier supplier (must not be null)
        Returns:
        true if the supplier was set, false if it was already set to something else
        See Also:
        setGlobalDefaultSupplier(Supplier)
      • setGlobalDefault

        public void setGlobalDefault​(C globalDefault)
        Set the global default instance. This instance will be returned from all subsequent calls to getGlobalDefault(), replacing any previous instance or supplier that was set.
        Parameters:
        globalDefault - the global default value, or null to remove the global default
      • getClassLoaderDefault

        public C getClassLoaderDefault​(java.lang.ClassLoader classLoader)
        Get the class loader default instance. Note that the class loader default is determined by way of a Supplier so the returned value may vary from call to call, depending on the policy of that Supplier.
        Parameters:
        classLoader - the class loader
        Returns:
        the global default, or null if none is installed or available
      • setClassLoaderDefaultSupplier

        public void setClassLoaderDefaultSupplier​(java.lang.ClassLoader classLoader,
                                                  java.util.function.Supplier<C> supplier)
        Set the per-class loader default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getClassLoaderDefault(ClassLoader) will obtain results consistent with a general expectation of stability.
        Parameters:
        classLoader - the class loader (must not be null)
        supplier - the supplier, or null to remove the default for this class loader
      • setClassLoaderDefault

        public void setClassLoaderDefault​(java.lang.ClassLoader classLoader,
                                          C classLoaderDefault)
        Set the per-class loader default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getClassLoaderDefault(ClassLoader) will obtain results consistent with a general expectation of stability.
        Parameters:
        classLoader - the class loader (must not be null)
        classLoaderDefault - the class loader default value, or null to remove the default
      • getThreadDefault

        public C getThreadDefault()
        Get the per-thread default context instance. Note that the per-thread default is determined by way of a Supplier so the returned value may vary from call to call, depending on the policy of that Supplier.
        Returns:
        the per-thread default, or null if none is installed or available
      • setThreadDefaultSupplier

        public void setThreadDefaultSupplier​(java.util.function.Supplier<C> supplier)
        Set the per-thread default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getThreadDefault() will obtain results consistent with a general expectation of stability.
        Parameters:
        supplier - the supplier, or null to remove the per-thread default
      • setThreadDefault

        public void setThreadDefault​(C threadDefault)
        Set the per-thread default instance. This instance will be returned from all subsequent calls to getThreadDefault(), replacing any previous instance or supplier that was set.
        Parameters:
        threadDefault - the per-thread default value, or null to remove the per-thread default
      • get

        public C get()
        Get the currently active context, possibly examining the per-thread or global defaults.
        Specified by:
        get in interface java.util.function.Supplier<C extends Contextual<C>>
        Returns:
        the current context, or null if none is active
      • getPrivilegedSupplier

        public java.util.function.Supplier<C> getPrivilegedSupplier()
        Get a privileged supplier for this context manager which returns the currently active context without a permission check.
        Returns:
        the privileged supplier