-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de12506
commit 52ade38
Showing
3 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
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
32 changes: 31 additions & 1 deletion
32
satchel-core/src/main/java/cafe/adriel/satchel/ktx/satchel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,48 @@ | ||
package cafe.adriel.satchel.ktx | ||
|
||
import cafe.adriel.satchel.SatchelStorage | ||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
inline fun <reified T : Any> SatchelStorage.value(key: String): ReadWriteProperty<Any, T?> = | ||
object : ReadWriteProperty<Any, T?> { | ||
override fun getValue(thisRef: Any, property: KProperty<*>): T? = | ||
get(key) | ||
|
||
override fun setValue(thisRef: Any, property: KProperty<*>, value: T?) = | ||
when (value) { | ||
null -> remove(key) | ||
else -> set(key, value) | ||
} | ||
} | ||
|
||
inline fun <reified T : Any> SatchelStorage.value(key: String, defaultValue: T): ReadWriteProperty<Any, T> = | ||
object : ReadWriteProperty<Any, T> { | ||
override fun getValue(thisRef: Any, property: KProperty<*>): T = | ||
getOrDefault(key, defaultValue) | ||
|
||
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) = | ||
set(key, value) | ||
} | ||
|
||
inline infix operator fun <reified T : Any> SatchelStorage.get(key: String): T? = | ||
getAny(key) as? T | ||
|
||
inline fun <reified T : Any> SatchelStorage.getOrDefault(key: String, defaultValue: T): T = | ||
get(key) ?: defaultValue | ||
|
||
inline fun <reified T : Any> SatchelStorage.getOrElse(key: String, defaultValue: () -> T): T = | ||
inline fun <reified T : Any> SatchelStorage.getOrDefault(key: String, defaultValue: () -> T): T = | ||
get(key) ?: defaultValue() | ||
|
||
inline fun <reified T : Any> SatchelStorage.getOrSet(key: String, defaultValue: T): T = | ||
get(key) ?: run { | ||
set(key, defaultValue) | ||
defaultValue | ||
} | ||
|
||
inline fun <reified T : Any> SatchelStorage.getOrSet(key: String, defaultValue: () -> T): T = | ||
get(key) ?: run { | ||
val value = defaultValue() | ||
set(key, value) | ||
value | ||
} |
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