ImmutableStateContainer

Thread-safe implementation of StateContainer that maintains immutable state.

This implementation ensures that:

  • State objects remain immutable after creation

  • All operations are thread-safe without external synchronization

  • State validation is enforced on every update

  • Failed updates leave the current state unchanged

Parameters

T

The type of state being managed

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val currentState: T

The current state of the container.

Functions

Link copied to clipboard
fun <T> StateContainer<T>.compareAndSet(expectedState: T, newState: T): Boolean

Conditionally updates the state only if the current state matches the expected state.

Link copied to clipboard
fun <T> StateContainer<T>.getAndUpdate(updater: (T) -> T): T?

Updates the state and returns the previous state if successful, or null if failed.

Link copied to clipboard
inline fun <T, R : T> StateContainer<T>.getStateAs(): R?

Returns the current state cast to the specified type, or null if not of that type.

Link copied to clipboard
fun <T> StateContainer<T>.isState(predicate: (T) -> Boolean): Boolean

Checks if the state matches a predicate.

Link copied to clipboard
inline fun <T, R : T> StateContainer<T>.isStateType(): Boolean

Returns true if the current state is of the specified type.

Link copied to clipboard
fun <T, R> StateContainer<T>.map(mapper: (T) -> R): R

Maps the current state to a different type.

Link copied to clipboard
fun <T> StateContainer<T>.trySet(newState: T): Boolean

Attempts to set the state to a specific value.

Link copied to clipboard
fun <T> StateContainer<T>.updateAndGet(updater: (T) -> T): T?

Updates the state and returns the new state if successful, or null if failed.

Link copied to clipboard
fun <T> StateContainer<T>.updateAtomic(maxRetries: Int = 10, updater: (T) -> T): StateUpdateResult<T>

Extension function for StateContainer to perform atomic updates.

Link copied to clipboard
open override fun updateState(updater: (T) -> T): StateUpdateResult<T>

Updates the state using the provided updater function.

Link copied to clipboard
suspend fun <T> StateContainer<T>.updateSuspending(updater: suspend (T) -> T): StateUpdateResult<T>

Transforms the state using a suspending function while maintaining thread safety.

Link copied to clipboard
open override fun validateState(state: T): ValidationResult

Validates the given state using the container's validator.

Link copied to clipboard
fun <T> StateContainer<T>.withState(action: (T) -> Unit)

Executes an action with the current state.