Created
July 25, 2018 10:59
-
-
Save sergchil/4ba3aaa0d68c213cd0fff42721f4bcc4 to your computer and use it in GitHub Desktop.
Helper class that invokes once
This file contains hidden or 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
import java.util.concurrent.atomic.AtomicBoolean | |
/** | |
* Created with ❤ by Sergey Chilingaryan | |
*/ | |
// Yes, I made class for this simple call | |
// but this is more readable than having booleans and checking ifs | |
// I hate using booleans with ifs to do things once | |
class Once { | |
private val done = AtomicBoolean() | |
fun run(task: () -> Unit?) { | |
if (done.get()) return | |
if (done.compareAndSet(false, true)) { | |
task() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE