Class AbstractSingleton

    • Field Detail

      • m_aRWLock

        protected final com.helger.commons.concurrent.SimpleReadWriteLock m_aRWLock
    • Constructor Detail

      • AbstractSingleton

        @UsedViaReflection
        protected AbstractSingleton()
        Ctor.
    • Method Detail

      • writeAbstractSingletonFields

        protected final void writeAbstractSingletonFields​(@Nonnull
                                                          ObjectOutputStream aOOS)
                                                   throws IOException
        Write the internal status variables to the passed ObjectOutputStream. This can be used to make singletons serializable.
        Parameters:
        aOOS - The output stream to write to. May not be null.
        Throws:
        IOException - In case writing failed
      • onAfterInstantiation

        @OverrideOnDemand
        protected void onAfterInstantiation​(@Nonnull
                                            IScope aScope)
        Called after the singleton was instantiated. The constructor has finished, and calling getInstance will work! This method is present to init the object with a virtual table present.
        Parameters:
        aScope - The scope in which the object was instantiated. Never null.
      • setInInstantiation

        protected final void setInInstantiation​(boolean bInInstantiation)
      • isInInstantiation

        public final boolean isInInstantiation()
        Returns:
        true if this singleton is currently in the phase of instantiation, false if it is instantiated or already destroyed.
      • setInstantiated

        protected final void setInstantiated​(boolean bInstantiated)
      • isInstantiated

        public final boolean isInstantiated()
        Returns:
        true if this singleton was already instantiated, false if it is active.
      • setInPreDestruction

        protected final void setInPreDestruction​(boolean bInPreDestruction)
      • isInPreDestruction

        public final boolean isInPreDestruction()
        Returns:
        true if this singleton is currently in the phase of pre destruction, false if it is active or already destroyed.
      • setInDestruction

        protected final void setInDestruction​(boolean bInDestruction)
      • isInDestruction

        public final boolean isInDestruction()
        Returns:
        true if this singleton is currently in the phase of destruction, false if it is active or already destroyed.
      • setDestroyed

        protected final void setDestroyed​(boolean bDestroyed)
      • isDestroyed

        public final boolean isDestroyed()
        Returns:
        true if this singleton was already destroyed, false if it is active.
      • onBeforeDestroy

        @OverrideOnDemand
        protected void onBeforeDestroy​(@Nonnull
                                       IScope aScopeToBeDestroyed)
                                throws Exception
        Called before this singleton is destroyed. This method is called when "inPreDestruction" is true, "inDestruction" is still false and "isDestroyed" is false.
        Parameters:
        aScopeToBeDestroyed - The scope that will be destroyed. Never null.
        Throws:
        Exception - If something goes wrong
      • onBeforeScopeDestruction

        public final void onBeforeScopeDestruction​(@Nonnull
                                                   IScope aScopeToBeDestroyed)
                                            throws Exception
        Description copied from interface: IScopeDestructionAware
        Called before the owning scope is destroyed. You may perform some last actions before the scope is really destroyed. This method is called after the IScope.preDestroy() callback is invoked and before the scope is set as being "in destruction".
        Specified by:
        onBeforeScopeDestruction in interface IScopeDestructionAware
        Parameters:
        aScopeToBeDestroyed - The scope that will be destroyed. Never null.
        Throws:
        Exception - in case of an error
      • onDestroy

        @OverrideOnDemand
        protected void onDestroy​(@Nonnull
                                 IScope aScopeInDestruction)
                          throws Exception
        Called when this singleton is destroyed. Perform all cleanup in this method. This method is called when "inPreDestruction" is false , "inDestruction" is true and "isDestroyed" is false.
        Parameters:
        aScopeInDestruction - The scope in destruction. Never null.
        Throws:
        Exception - If something goes wrong
      • onScopeDestruction

        public final void onScopeDestruction​(@Nonnull
                                             IScope aScopeInDestruction)
                                      throws Exception
        Description copied from interface: IScopeDestructionAware
        Called when the owning scope is destroyed. You may perform some cleanup work in here. This is method is called when the scope is already "in destruction".
        Specified by:
        onScopeDestruction in interface IScopeDestructionAware
        Parameters:
        aScopeInDestruction - The scope in destruction. Never null.
        Throws:
        Exception - in case of an error
      • isUsableObject

        public final boolean isUsableObject()
        Returns:
        true if the object is instantiated and neither in destruction nor destroyed.
      • getSingletonScopeKey

        @Nonnull
        public static final String getSingletonScopeKey​(@Nonnull
                                                        Class<? extends AbstractSingleton> aClass)
        Create the key which is used to reference the object within the scope.
        Parameters:
        aClass - The class for which the key is to be created. May not be null.
        Returns:
        The non-null key.
      • getSingletonIfInstantiated

        @Nullable
        public static final <T extends AbstractSingleton> T getSingletonIfInstantiated​(@Nullable
                                                                                       IScope aScope,
                                                                                       @Nonnull
                                                                                       Class<T> aClass)
        Get the singleton object if it is already instantiated inside a scope or null if it is not instantiated.
        Type Parameters:
        T - The type to be returned
        Parameters:
        aScope - The scope to check. May be null to avoid constructing a scope.
        aClass - The class to be checked. May not be null.
        Returns:
        The singleton for the specified class is already instantiated, null otherwise.
      • isSingletonInstantiated

        public static final boolean isSingletonInstantiated​(@Nullable
                                                            IScope aScope,
                                                            @Nonnull
                                                            Class<? extends AbstractSingleton> aClass)
        Check if a singleton is already instantiated inside a scope
        Parameters:
        aScope - The scope to check. May be null to avoid constructing a scope.
        aClass - The class to be checked. May not be null.
        Returns:
        true if the singleton for the specified class is already instantiated, false otherwise.
      • getSingleton

        @Nonnull
        public static final <T extends AbstractSingleton> T getSingleton​(@Nonnull
                                                                         IScope aScope,
                                                                         @Nonnull
                                                                         Class<T> aClass)
        Get the singleton object in the passed scope, using the passed class. If the singleton is not yet instantiated, a new instance is created.
        Type Parameters:
        T - The singleton type
        Parameters:
        aScope - The scope to be used. May not be null.
        aClass - The class to be used. May not be null. The class must be public as needs to have a public no-argument constructor.
        Returns:
        The singleton object and never null.
      • getAllSingletons

        @Nonnull
        @ReturnsMutableCopy
        public static final <T extends AbstractSingleton> com.helger.commons.collection.impl.ICommonsList<T> getAllSingletons​(@Nullable
                                                                                                                              IScope aScope,
                                                                                                                              @Nonnull
                                                                                                                              Class<T> aDesiredClass)
        Get all singleton objects registered in the respective sub-class of this class.
        Type Parameters:
        T - The singleton type to be retrieved
        Parameters:
        aScope - The scope to use. May be null to avoid creating a new scope.
        aDesiredClass - The desired sub-class of this class. May not be null.
        Returns:
        A non-null list with all instances of the passed class in the passed scope.