GuavaDataStore.Builder


public final class GuavaDataStore.Builder<T extends Object>


Builder class for a GuavaDataStore.

Summary

Public constructors

<T extends Object> Builder(
    @NonNull Serializer<@NonNull T> serializer,
    @NonNull Callable<@NonNull File> produceFile
)
<T extends Object> Builder(
    @NonNull Context context,
    @NonNull String fileName,
    @NonNull Serializer<@NonNull T> serializer
)

Create a GuavaDataStoreBuilder with the Context and name from which to derive the DataStore file.

Public methods

final @NonNull GuavaDataStore.Builder<@NonNull T>

Add a DataMigration to the Datastore.

final @NonNull GuavaDataStore<@NonNull T>

Build the DataStore.

final @NonNull GuavaDataStore.Builder<@NonNull T>

Flag used to signal the creation of a multi-process DataStore using a MultiProcessCoordinator.

final @NonNull GuavaDataStore.Builder<@NonNull T>

Sets the corruption handler to install into the DataStore.

final @NonNull GuavaDataStore.Builder<@NonNull T>

Sets the Executor used by the DataStore.

Public constructors

Builder

public <T extends Object> Builder(
    @NonNull Serializer<@NonNull T> serializer,
    @NonNull Callable<@NonNull File> produceFile
)

Builder

public <T extends Object> Builder(
    @NonNull Context context,
    @NonNull String fileName,
    @NonNull Serializer<@NonNull T> serializer
)

Create a GuavaDataStoreBuilder with the Context and name from which to derive the DataStore file. The file is generated by File(this.filesDir, "datastore/$fileName"). The user is responsible for ensuring that there is never more than one DataStore acting on a file at a time.

Either produceFile or context & name must be set, but not both. This is enforced by the two constructors.

Parameters
@NonNull Context context

the Context from which we retrieve files directory.

@NonNull String fileName

the filename relative to Context.applicationContext.filesDir that DataStore acts on. The File is obtained from dataStoreFile. It is created in the "/datastore" subdirectory.

@NonNull Serializer<@NonNull T> serializer

the Serializer for the type that this DataStore acts on.

Public methods

addDataMigration

Added in 1.2.0-alpha02
public final @NonNull GuavaDataStore.Builder<@NonNull T> addDataMigration(@NonNull DataMigration<@NonNull T> dataMigration)

Add a DataMigration to the Datastore. Migrations are run in the order they are added.

Parameters
@NonNull DataMigration<@NonNull T> dataMigration

the migration to add

build

Added in 1.2.0-alpha02
public final @NonNull GuavaDataStore<@NonNull T> build()

Build the DataStore.

Returns
@NonNull GuavaDataStore<@NonNull T>

the DataStore with the provided parameters

enableMultiProcess

Added in 1.2.0-alpha02
public final @NonNull GuavaDataStore.Builder<@NonNull T> enableMultiProcess()

Flag used to signal the creation of a multi-process DataStore using a MultiProcessCoordinator.

By default, the builder sets up a DataStore intended for single-application use. It prioritizes speed and is created using DataStoreFactory. However, if you anticipate any scenario where different parts of your system (even if they are just reading data) might access the DataStore at the same time from separate processes, the enableMultiProcess flag must be set to true. This ensures a MultiProcessDataStoreFactory is used, providing the necessary safeguards for concurrent access across multiple processes.

setCorruptionHandler

Added in 1.2.0-alpha02
public final @NonNull GuavaDataStore.Builder<@NonNull T> setCorruptionHandler(
    @NonNull ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler
)

Sets the corruption handler to install into the DataStore.

This parameter is optional and defaults to no corruption handler.

Parameters
@NonNull ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler

the handler to invoke when there is a file corruption

setExecutor

Added in 1.2.0-alpha02
public final @NonNull GuavaDataStore.Builder<@NonNull T> setExecutor(@NonNull Executor executor)

Sets the Executor used by the DataStore.

This parameter is optional and defaults to Dispatchers.IO.

Parameters
@NonNull Executor executor

the executor to be used by DataStore