SavedStateContainer


public final class SavedStateContainer


A container that manages SavedStateValue instances to coordinate state saving and restoration across hierarchical and key-value based components.

SavedStateContainer allows registering, retrieving, removing, and nesting state-saving components. When saving or restoring state, operations are delegated across all registered SavedStateValue instances.

Summary

Public constructors

Public methods

final boolean

Checks if a SavedStateValue is registered under the given key.

final SavedStateValue<@NonNull T>
<T extends Object> get(@NonNull String key)

Retrieves the registered SavedStateValue associated with key.

final @NonNull SavedStateContainer

Retrieves an existing nested SavedStateContainer associated with key, or creates and registers a new child SavedStateContainer if none exists.

final @NonNull R
<T extends Object, R extends SavedStateValue<@NonNull T>> getOrPut(
    @NonNull String key,
    @NonNull Function0<@NonNull R> defaultValue
)

Returns an existing SavedStateValue registered for key, or evaluates defaultValue, registers the result in this container, and returns it.

final @NonNull Set<@NonNull String>

Returns a Set containing all keys registered in this SavedStateContainer.

final SavedStateValue<@NonNull T>
<T extends Object> put(@NonNull String key, @NonNull SavedStateValue<@NonNull T> value)

Registers a value for state saving and restoration under the given key.

final SavedStateValue<@NonNull T>
<T extends Object> remove(@NonNull String key)

Removes and returns the registered SavedStateValue associated with key.

final void

Restores state for all registered SavedStateValue instances using the provided savedState.

final @NonNull SavedState

Invokes state saving on all registered SavedStateValue instances and bundles their states into a single SavedState.

final void
<T extends Object> set(@NonNull String key, @NonNull SavedStateValue<@NonNull T> value)

Registers a value for state saving and restoration under the given key.

Public constructors

SavedStateContainer

Added in 1.6.0-alpha07
public SavedStateContainer()

Public methods

contains

Added in 1.6.0-alpha07
public final boolean contains(@NonNull String key)

Checks if a SavedStateValue is registered under the given key.

Parameters
@NonNull String key

unique identifier for the saved state value

Returns
boolean

true if a SavedStateValue is associated with key, false otherwise

get

Added in 1.6.0-alpha07
public final SavedStateValue<@NonNull T> <T extends Object> get(@NonNull String key)

Retrieves the registered SavedStateValue associated with key.

Parameters
@NonNull String key

unique identifier for the saved state value

Returns
SavedStateValue<@NonNull T>

registered SavedStateValue, or null if no value is found

getOrCreateContainer

Added in 1.6.0-alpha07
public final @NonNull SavedStateContainer getOrCreateContainer(@NonNull String key)

Retrieves an existing nested SavedStateContainer associated with key, or creates and registers a new child SavedStateContainer if none exists.

Parameters
@NonNull String key

unique identifier for the child container

Returns
@NonNull SavedStateContainer

existing or newly created child SavedStateContainer instance

getOrPut

Added in 1.6.0-alpha07
public final @NonNull R <T extends Object, R extends SavedStateValue<@NonNull T>> getOrPut(
    @NonNull String key,
    @NonNull Function0<@NonNull R> defaultValue
)

Returns an existing SavedStateValue registered for key, or evaluates defaultValue, registers the result in this container, and returns it.

Parameters
@NonNull String key

unique identifier for the saved state value

@NonNull Function0<@NonNull R> defaultValue

factory producing a new SavedStateValue if none is registered for key

Returns
@NonNull R

existing or newly registered SavedStateValue instance

Throws
ClassCastException

if an existing SavedStateValue registered for key is not an instance of R

keys

Added in 1.6.0-alpha07
public final @NonNull Set<@NonNull String> keys()

Returns a Set containing all keys registered in this SavedStateContainer.

Returns
@NonNull Set<@NonNull String>

set of registered keys

put

Added in 1.6.0-alpha07
public final SavedStateValue<@NonNull T> <T extends Object> put(@NonNull String key, @NonNull SavedStateValue<@NonNull T> value)

Registers a value for state saving and restoration under the given key.

If a SavedStateValue was previously registered under key, it is overwritten and returned.

Parameters
@NonNull String key

unique identifier for the saved state value

@NonNull SavedStateValue<@NonNull T> value

component responsible for state saving and restoration

Returns
SavedStateValue<@NonNull T>

previously registered SavedStateValue, or null if none was registered

remove

Added in 1.6.0-alpha07
public final SavedStateValue<@NonNull T> <T extends Object> remove(@NonNull String key)

Removes and returns the registered SavedStateValue associated with key.

Parameters
@NonNull String key

unique identifier for the saved state value to remove

Returns
SavedStateValue<@NonNull T>

previously registered SavedStateValue, or null if none was registered

restoreState

public final void restoreState(SavedState savedState)

Restores state for all registered SavedStateValue instances using the provided savedState.

Parameters
SavedState savedState

previously saved state container, or null if no state was saved

saveState

Added in 1.6.0-alpha07
public final @NonNull SavedState saveState()

Invokes state saving on all registered SavedStateValue instances and bundles their states into a single SavedState.

Returns
@NonNull SavedState

a SavedState containing the collected saved states of all registered providers

set

Added in 1.6.0-alpha07
public final void <T extends Object> set(@NonNull String key, @NonNull SavedStateValue<@NonNull T> value)

Registers a value for state saving and restoration under the given key.

If a SavedStateValue was previously registered under key, it will be overwritten.

Parameters
@NonNull String key

unique identifier for the saved state value

@NonNull SavedStateValue<@NonNull T> value

component responsible for state saving and restoration