Class AbstractConcurrentCustomScope<A extends java.lang.annotation.Annotation>

  • Type Parameters:
    A - The annotation type
    All Implemented Interfaces:
    LifeCycle<AbstractConcurrentCustomScope<A>>, CustomScope<A>, java.io.Closeable, java.lang.AutoCloseable

    public abstract class AbstractConcurrentCustomScope<A extends java.lang.annotation.Annotation>
    extends java.lang.Object
    implements CustomScope<A>, LifeCycle<AbstractConcurrentCustomScope<A>>, java.lang.AutoCloseable
    Abstract implementation of the custom scope interface that simplifies defining new scopes using the Map interface.

    Note this implementation uses a singleReentrantReadWriteLock to lock the entire scope hence it is designed for scopes that will hold a small amount of beans. For implementations that hold many beans it is recommended to use a lock per BeanIdentifier.

    Since:
    3.0.0
    • Constructor Detail

      • AbstractConcurrentCustomScope

        protected AbstractConcurrentCustomScope​(java.lang.Class<A> annotationType)
        A custom scope annotation.
        Parameters:
        annotationType - The annotation type
    • Method Detail

      • getScopeMap

        @NonNull
        protected abstract java.util.Map<BeanIdentifier,​CreatedBean<?>> getScopeMap​(boolean forCreation)
        Parameters:
        forCreation - Whether it is for creation
        Returns:
        Obtains the scope map, never null
        Throws:
        java.lang.IllegalStateException - if the scope map cannot be obtained in the current context
      • annotationType

        public final java.lang.Class<A> annotationType()
        Specified by:
        annotationType in interface CustomScope<A extends java.lang.annotation.Annotation>
        Returns:
        The scope annotation type
      • close

        public abstract void close()
        Implement the close logic for the scope.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface LifeCycle<A extends java.lang.annotation.Annotation>
      • stop

        @NonNull
        public final AbstractConcurrentCustomScope<A> stop()
        Description copied from interface: LifeCycle
        Stops the life cycle component.
        Specified by:
        stop in interface LifeCycle<A extends java.lang.annotation.Annotation>
        Returns:
        This lifecycle component
      • destroyScope

        protected void destroyScope​(@Nullable
                                    java.util.Map<BeanIdentifier,​CreatedBean<?>> scopeMap)
        Destroys the scope.
        Parameters:
        scopeMap - Th scope map
      • getOrCreate

        public final <T> T getOrCreate​(BeanCreationContext<T> creationContext)
        Description copied from interface: CustomScope
        Resolve an object from the given scope.
        Specified by:
        getOrCreate in interface CustomScope<A extends java.lang.annotation.Annotation>
        Type Parameters:
        T - The bean generic type
        Parameters:
        creationContext - The creation context
        Returns:
        The bean instance
      • doCreate

        @NonNull
        protected <T> CreatedBean<T> doCreate​(@NonNull
                                              BeanCreationContext<T> creationContext)
        Perform creation.
        Type Parameters:
        T - The generic type
        Parameters:
        creationContext - The creation context
        Returns:
        Created bean
      • remove

        public final <T> java.util.Optional<T> remove​(BeanIdentifier identifier)
        Description copied from interface: CustomScope
        Remove a bean definition from the scope.
        Specified by:
        remove in interface CustomScope<A extends java.lang.annotation.Annotation>
        Type Parameters:
        T - The generic type
        Parameters:
        identifier - The BeanIdentifier
        Returns:
        An Optional of the instance that was destroyed if it exists
      • handleDestructionException

        protected void handleDestructionException​(BeanDestructionException e)
        Method that can be overridden to customize what happens on a shutdown error.
        Parameters:
        e - The exception
      • findBeanRegistration

        public final <T> java.util.Optional<BeanRegistration<T>> findBeanRegistration​(T bean)
        Description copied from interface: CustomScope
        Get the BeanDefinition for the given bean.
        Specified by:
        findBeanRegistration in interface CustomScope<A extends java.lang.annotation.Annotation>
        Type Parameters:
        T - The bean generic type
        Parameters:
        bean - The bean
        Returns:
        The bean definition if it can be resolved