EventDrivenStateContainer

State container that handles events and transitions states accordingly.

This container combines state management with event handling, allowing for event-driven state transitions while maintaining thread safety and validation guarantees.

Parameters

T

The type of state being managed

Types

Link copied to clipboard
class Builder<T>(initialState: T, validator: StateValidator<T>)

Builder for creating EventDrivenStateContainer instances with multiple event handlers.

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

Returns true if a handler is registered for the given event.

fun canHandle(eventClass: KClass<out StateEvent>): Boolean

Returns true if a handler is registered for the given event type.

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

Handles an event and potentially transitions the state.

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

Registers an event handler for the specified event type.

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

Executes an action with the current state.