Class SingletonDelegateProperty

  • All Implemented Interfaces:
    kotlin.properties.ReadOnlyProperty

    
    public final class SingletonDelegateProperty<T extends Object>
     implements ReadOnlyProperty<Any, T>
                        

    A delegate property that ensures a singleton instance of the given type T. The instance is created using the provided creator function and is stored as a weak reference to allow for garbage collection when not in use.

    Note: Every new or different instance of SingletonDelegateProperty will manage a different singleton. So it is the class responsibility to wrap its instance around SingletonDelegateProperty to ensure that the singleton is managed properly.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      T getValue(Object thisRef, KProperty<?> property) Returns the singleton instance of type T.
      • Methods inherited from class java.lang.Object

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

      • SingletonDelegateProperty

        SingletonDelegateProperty(Function0<T> creator)
    • Method Detail

      • getValue

         T getValue(Object thisRef, KProperty<?> property)

        Returns the singleton instance of type T. If the instance does not exist, it is created using the creator function and stored as a weak reference.

        Note: The Initialization of long running constructors should be accessed from background thread which causes getValue to run on background thread.

        Returns:

        The singleton instance of type T.