MutablePreferences

public final class MutablePreferences extends Preferences

Object
   ↳ Preferences
     ↳ MutablePreferences

Mutable version of Preferences. Allows for creating Preferences with different key-value pairs.

Summary

Public methods

@NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object>

Retrieve a map of all key preference pairs.

final void
boolean
<T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)

Returns true if this Preferences contains the specified key.

boolean
@Nullable T
<T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)

Get a preference with a key.

int
final void

Removes the preference with the given key from this MutablePreferences.

final void

Appends or replaces all pairs from prefs to this MutablePreferences.

final void

Appends or replaces all pair to this MutablePreferences.

final void

Appends or replaces all pairs to this MutablePreferences.

final @NonNull T
<T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)

Remove a preferences from this MutablePreferences.

final void
<T extends Object> set(@NonNull Preferences.Key<@NonNull T> key, @NonNull T value)

Set a key value pair in MutablePreferences.

@NonNull String

For better debugging.

Inherited methods

From class Preferences
final @NonNull MutablePreferences

Gets a mutable copy of Preferences which contains all the preferences in this Preferences.

final @NonNull Preferences

Gets a read-only copy of Preferences which contains all the preferences in this Preferences.

Public methods

asMap

@NonNull
public Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull ObjectasMap()

Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.

Returns
Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object>

a map containing all the preferences in this Preferences

clear

@NonNull
public final void clear()

contains

@NonNull
public boolean <T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)

Returns true if this Preferences contains the specified key.

Parameters
@NonNull Preferences.Key<@NonNull T> key

the key to check for

equals

@NonNull
public boolean equals(@Nullable Object other)

get

@Nullable
public T <T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)

Get a preference with a key. If the key is not set, returns null.

If T is Set, this returns an unmodifiable set which will throw a runtime exception when mutated. Do not try to mutate the returned set.

Use MutablePreferences.set to change the value of a preference (inside a DataStore.edit block).

Parameters
<T extends Object>

the type of the preference

@NonNull Preferences.Key<@NonNull T> key

the key for the preference

Throws
kotlin.ClassCastException

if there is something stored with the same name as key but it cannot be cast to T

hashCode

@NonNull
public int hashCode()

minusAssign

@NonNull
public final void minusAssign(@NonNull Preferences.Key<@NonNull ?> key)

Removes the preference with the given key from this MutablePreferences. If this Preferences does not contain the key, this is a no-op.

Example usage: mutablePrefs -= COUNTER_KEY

Parameters
@NonNull Preferences.Key<@NonNull ?> key

the key to remove from this MutablePreferences

plusAssign

@NonNull
public final void plusAssign(@NonNull Preferences prefs)

Appends or replaces all pairs from prefs to this MutablePreferences. Keys in prefs will overwrite keys in this Preferences.

Example usage: mutablePrefs += preferencesOf(COUNTER_KEY to 100, NAME to "abcdef")

Parameters
@NonNull Preferences prefs

Preferences to append to this MutablePreferences

plusAssign

@NonNull
public final void plusAssign(@NonNull Preferences.Pair<@NonNull ?> pair)

Appends or replaces all pair to this MutablePreferences.

Example usage: mutablePrefs += COUNTER_KEY to 100

Parameters
@NonNull Preferences.Pair<@NonNull ?> pair

the Preference.Pair to add to this MutablePreferences

putAll

@NonNull
public final void putAll(@NonNull Preferences.Pair<@NonNull ?> pairs)

Appends or replaces all pairs to this MutablePreferences.

Parameters
@NonNull Preferences.Pair<@NonNull ?> pairs

the pairs to append to this MutablePreferences

remove

@NonNull
public final T <T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)

Remove a preferences from this MutablePreferences.

Parameters
@NonNull Preferences.Key<@NonNull T> key

the key to remove this MutablePreferences

Returns
T

the original value of this preference key.

set

@NonNull
public final void <T extends Object> set(@NonNull Preferences.Key<@NonNull T> key, @NonNull T value)

Set a key value pair in MutablePreferences.

Example usage: val COUNTER_KEY = intPreferencesKey("counter")

// Once edit completes successfully, preferenceStore will contain the incremented counter. preferenceStore.edit { prefs: MutablePreferences -> prefs\[COUNTER_KEY\] = prefs\[COUNTER_KEY\] :? 0 + 1 }

Parameters
@NonNull Preferences.Key<@NonNull T> key

the preference to set

@NonNull Preferences.Key<@NonNull T> key

the value to set the preference to

toString

@NonNull
public String toString()

For better debugging.