Skip to content

Expiration Collections

Gabriel Souza edited this page Aug 8, 2020 · 1 revision

Expiration collections are a List and Map implementation that auto expire the values inside it.

List

ExpirationList have commum List operations with a plus of some fuctions.

typealias OnExpireCallback<T> = (T) -> Unit

interface ExpirationList<E> : MutableIterable<E> {

    /**
     * Returns the time missing to expire or `null` if the list don't contain the element
     */
    fun missingTime(element: E): Int?

    /**
     * Add the element to the list with an expiration time.
     *
     * [expireTime] in seconds
     * callback [onExpire] is called when the element expires.
     */
    fun add(element: E, expireTime: Int, onExpire: OnExpireCallback<E>? = null)

    /**
     * Add the element in the start of list with an expiration time.
     * 
     * [expireTime] in seconds
     * callback [onExpire] is called when the element expires.
     */
    fun addFirst(element: E, expireTime: Int, onExpire: OnExpireCallback<E>? = null)
}

Creating one

Expiration List uses Bukkit tasks to expire the values inside, in this case, a Plugin is needed (you will find extension to WithPlugin as well).

  • expirationListOf(): Empty expiration list
  • expirationListOf(expireTime: Int, vararg elements: E): Expiration list with a list of element that will expire in the time provided (in seconds)
  • expirationListOf(expireTime: Int, plugin: Plugin, vararg elements: Pair<E, OnExpireCallback<E>>): Expiration list with a list of element in Pair togther with the OnExpireCallback.

Map

ExpirationMap have commum Map operations with a plus of some fuctions.

typealias OnExpireMapCallback<K, V> = (K, V) -> Unit

interface ExpirationMap<K, V> : MutableMap<K, V>, WithPlugin<Plugin> {
    /**
     * Returns the missing time on seconds to expire the key,
     * -1 if was not specified the expiration time before(permanent key) or
     * `null` if the key is not contained in the map.
     */
    fun missingTime(key: K): Long?

    /**
     * Set expiration time to the key and returns `true` if the key is found
     * or false if the key is not contained in the map.
     */
    fun expire(key: K, time: Long): Boolean

    /**
     * Set expiration time to the key and returns `true` if the key is found
     * or false if the key is not contained in the map.
     *
     * [time] in seconds.
     * [callback] is called when the key expires.
     */
    fun expire(key: K, time: Long, callback: OnExpireMapCallback<K, V>): Boolean

    /**
     * Associates the specified [value] with the specified [key] in the map
     * with an expiration time.
     *
     * @return the previous value associated with the key, or `null` if the key was not present in the map.
     */
    fun put(key: K, value: V, time: Long): V?

    /**
     * Associates the specified [value] with the specified [key] in the map
     * with an expiration time.
     *
     * [time] in seconds.
     * [callback] is called when the key expires.
     *
     * @return the previous value associated with the key, or `null` if the key was not present in the map.
     */
    fun put(key: K, value: V, time: Long, callback: OnExpireMapCallback<K, V>): V?
}

Creating one

ExpirationMap uses Bukkit tasks to expire the values inside, in this case, a Plugin is needed (you will find extension to WithPlugin as well).

  • expirationMapOf(): Empty expiration map
  • expirationMapOf(expireTime: Long, vararg elements: Pair<K, V>): Expiration map with a list of element that will expire in the time provided (in seconds)
  • expirationMapOf(expireTime: Long, vararg elements: Triple<K, V, OnExpireMapCallback<K, V>>): Expiration map with a list of element in Triple togther with the OnExpireCallback.
Clone this wiki locally