[Android] Util class to create singleton with argument
Geoffrey Métais
git at videolan.org
Thu Aug 9 10:38:20 CEST 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Aug 9 10:29:30 2018 +0200| [7ea8e692803dacf20fd408d9591953d89981c7f1] | committer: Geoffrey Métais
Util class to create singleton with argument
> https://code.videolan.org/videolan/vlc-android/commit/7ea8e692803dacf20fd408d9591953d89981c7f1
---
.../java/org/videolan/tools/SingletonHolder.kt | 37 ++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/tools/src/main/java/org/videolan/tools/SingletonHolder.kt b/tools/src/main/java/org/videolan/tools/SingletonHolder.kt
new file mode 100644
index 000000000..b9b551001
--- /dev/null
+++ b/tools/src/main/java/org/videolan/tools/SingletonHolder.kt
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * SingletonHolder.kt
+ * ****************************************************************************
+ * Copyright © 2018 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ ******************************************************************************/
+
+package org.videolan.tools
+
+open class SingletonHolder<out T, in A>(creator: (A) -> T) {
+ private var creator: ((A) -> T)? = creator
+ @Volatile private var instance: T? = null
+
+ fun getInstance(arg: A) = instance ?: synchronized(this) {
+ val i2 = instance
+ if (i2 != null) i2
+ else {
+ val created = creator!!(arg)
+ instance = created
+ creator = null
+ created
+ }
+ }
+}
More information about the Android
mailing list