Created
November 13, 2024 08:30
-
-
Save ziginsider/83edd809cc00e92a77a14f8217ab5f51 to your computer and use it in GitHub Desktop.
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
| /** | |
| * Cast [value] to specified type [T] or throw [ClassCastException] with [msg]. | |
| */ | |
| inline fun <reified T : Any> requireType(value: Any?, msg: () -> String): T { | |
| return value as? T ?: throw ClassCastException(msg()) | |
| } | |
| /** | |
| * Cast [value] to specified type [T] or throw [ClassCastException]. | |
| */ | |
| inline fun <reified T : Any> requireType(value: Any?): T { | |
| return value as? T ?: throw ClassCastException( | |
| "Expected value type ${T::class.java.canonicalName}. Actual is ${value?.let { it::class.java.canonicalName }}" | |
| ) | |
| } | |
| /** | |
| * Same that call lazy [LazyThreadSafetyMode.NONE] | |
| * | |
| * @see lazy | |
| * @see LazyThreadSafetyMode.NONE | |
| */ | |
| fun <T> lazyUnsafe(initializer: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE, initializer) | |
| /** | |
| * Checks if current thread is Android Main/UI thread. | |
| */ | |
| inline val isMainThread get() = Looper.getMainLooper() == Looper.myLooper() | |
| /** | |
| * Get classs loader for specified Kotlin class | |
| */ | |
| fun <T : Any> KClass<T>.javaClassLoader(): ClassLoader { | |
| return requireNotNull(this.java.classLoader) | |
| } | |
| /** | |
| * Create empty [EnumMap] | |
| */ | |
| inline fun <reified K : Enum<K>, V> enumMapOf(): EnumMap<K, V> { | |
| return EnumMap<K, V>(K::class.java) | |
| } | |
| /** | |
| * Create [EnumMap] with specified [values] | |
| */ | |
| inline fun <reified K : Enum<K>, V> enumMapOf(vararg values: Pair<K, V>): EnumMap<K, V> { | |
| return EnumMap<K, V>(K::class.java).apply { putAll(values) } | |
| } | |
| /** | |
| * Create [EnumMap] from specified [map] | |
| */ | |
| inline fun <reified K : Enum<K>, V> enumMapOf(map: Map<K, V>): EnumMap<K, V> { | |
| // Different overloads of EnumMap constrcturs will be called if map is EnumMap and for common Map | |
| return EnumMap(map) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment