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(Preferences.Key<T> key)

Returns true if this Preferences contains the specified key.

boolean
equals(Object other)
T
<T extends Object> get(Preferences.Key<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(Preferences.Key<T> key)

Remove a preferences from this MutablePreferences.

final void
<T extends Object> set(Preferences.Key<T> key, 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

public @NonNull 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
@NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object>

a map containing all the preferences in this Preferences

clear

public final void clear()

contains

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

Returns true if this Preferences contains the specified key.

Parameters
Preferences.Key<T> key

the key to check for

equals

public boolean equals(Object other)

get

public T <T extends Object> get(Preferences.Key<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

Preferences.Key<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

public int hashCode()

minusAssign

public final void minusAssign(Preferences.Key<?> 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
Preferences.Key<?> key

the key to remove from this MutablePreferences

plusAssign

public final void plusAssign(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
Preferences prefs

Preferences to append to this MutablePreferences