RegistrationTokenMap

class RegistrationTokenMap<T : RegistrationToken, V>(tokenFactory: (Int) -> T)

Utility class to generate unique tokens of type T and associate a value of type V to this token. Tokens are created by using the tokenFactory.

This class is thread-safe.

It is assumed tokens can be reused after a Int.MAX_VALUE rollover.

Constructors

Link copied to clipboard
fun <T : RegistrationToken> RegistrationTokenMap(tokenFactory: (Int) -> T)

Functions

Link copied to clipboard
fun associateValueToToken(token: T, value: V)

Associates the given value to the given token.

Link copied to clipboard
fun getAssociatedValueOfToken(token: T): V?

Returns the associated value of token. Null when the token is not mapped.

Link copied to clipboard
fun removeAssociatedValueOfToken(token: T)

Removes the associated value of token, if any, from the map.

Link copied to clipboard
suspend fun <R> useNewToken(block: suspend (T) -> R): R

Creates an new token and calls block with the token as lambda argument. The entry associated to the token, if any, is removed from the map afterwards.

Link copied to clipboard
fun withAssociatedValueAndRemove(token: T, block: (V) -> Unit)

Applies block on the associated value of token, if any, and removes the associated value, if any, afterwards.

Link copied to clipboard
fun withNewToken(block: (T) -> V): V

Creates an new token and calls block with the token as lambda argument. The returned value of block is associated to the token.