
walkmc.cache.ICache.kt Maven / Gradle / Ivy
package walkmc.cache
import org.bukkit.entity.*
import walkmc.extensions.*
import java.util.*
/**
* A registry represents a key-value holder object.
* This can be used to store data.
*/
interface ICache : MutableMap {
/**
* The holder mutable map of this registry.
*/
val delegate: MutableMap
/**
* Try locates an existent value, or
* null if the value not exists in the storage.
*/
fun locate(key: K): V? = delegate[key]
/**
* Acquire an existent value as a [Result].
* Supporting this was safe try-catch use.
*/
fun acquire(key: K): Result = runCatching {
delegate[key]!!
}
/*
* delegation
*/
override val size: Int get() = delegate.size
override val entries: MutableSet> get() = delegate.entries
override val keys: MutableSet get() = delegate.keys
override val values: MutableCollection get() = delegate.values
override fun put(key: K, value: V): V? = delegate.put(key, value)
override fun remove(key: K): V? = delegate.remove(key)
override fun containsKey(key: K): Boolean = delegate.containsKey(key)
override fun containsValue(value: V): Boolean = delegate.containsValue(value)
override fun get(key: K): V? = delegate[key]
override fun isEmpty(): Boolean = delegate.isEmpty()
override fun clear() = delegate.clear()
override fun putAll(from: Map) = delegate.putAll(from)
}
/**
* Gets a value of this registry by index or throws an exception if the value of
* the specified index not exists.
*/
fun ICache<*, V>.acquire(index: Int): V = values.toList()[index]
/**
* Gets a value of this registry by index or null if the value of
* the specified index not exists.
*/
fun ICache<*, V>.locate(index: Int): V? = values.toList().getOrNull(index)
/**
* Submits the specified player to this registry.
*/
fun ICache.put(player: Player, value: V) = put(player.uuid, value)
/**
* Unsubmits the specified player to this registry.
*/
fun ICache.unsubmit(player: Player) = remove(player.uuid)
/**
* Locates the specified player to this registry.
*/
fun ICache.locate(player: Player) = locate(player.uuid)
/**
* Acquires the specified player to this registry.
*/
fun ICache.acquire(player: Player) = acquire(player.uuid)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy