Skip to content

Instantly share code, notes, and snippets.

@mezentsev
Last active November 11, 2019 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mezentsev/e52c99fe121b2c235dad9edad0c3b3e9 to your computer and use it in GitHub Desktop.
Save mezentsev/e52c99fe121b2c235dad9edad0c3b3e9 to your computer and use it in GitHub Desktop.
class MyMagicClass private constructor(builder: Builder) {
class Builder {
}
companion object {
@Volatile
private var instance: MyMagicClass? = null
fun init(builder: Builder) {
if (instance != null) {
throw IllegalStateException("Already init")
}
synchronized(this) {
if (instance == null) {
instance = MyMagicClass(builder)
} else {
throw IllegalStateException("Already init")
}
}
}
fun instance() = instance ?: throw IllegalStateException("Not init yet")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment