Created
December 2, 2019 11:11
-
-
Save fida1989/95f3b3ac44c06fb326c97775bd65df92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class SingletonHolder<out T: Any, in A>(creator: (A) -> T) { | |
private var creator: ((A) -> T)? = creator | |
@Volatile private var instance: T? = null | |
fun getInstance(arg: A): T { | |
val i = instance | |
if (i != null) { | |
return i | |
} | |
return synchronized(this) { | |
val i2 = instance | |
if (i2 != null) { | |
i2 | |
} else { | |
val created = creator!!(arg) | |
instance = created | |
creator = null | |
created | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment