MapSettings

class MapSettings(val delegate: MutableMap<String, Any> = mutableMapOf(), clearExpiredKeysDuration: Duration? = null) : ObservableSettings

A collection of storage-backed key-value data

This class allows storage of values with the Int, Long, String, Float, Double, or Boolean types, using a String reference as a key.

The CommonMapSettings implementation is intended for use in unit tests. It differs from production implementations because the state exists only in-memory and has no mechanism for persistence.

This class can be instantiated by wrapping a MutableMap or set of Pair entries.

This implementation is verified against the same test suite as the real platform-specific implementations to ensure it shares the same behavior, assuming the default mutableMapOf delegate is used.

Constructors

Link copied to clipboard
constructor(vararg items: Pair<String, Any>)
constructor(delegate: MutableMap<String, Any> = mutableMapOf(), clearExpiredKeysDuration: Duration? = null)

Types

Link copied to clipboard
class Listener(val listeners: MutableList<() -> Any>, val listener: () -> Unit) : Settings.Listener

A handle to a listener instance returned by one of the addListener methods of ObservableSettings, so it can be deactivated as needed.

Properties

Link copied to clipboard
private val delegate: MutableMap<String, Any>
Link copied to clipboard
open override val keys: Set<String>

Returns a Set containing all the keys present in this Settings.

Link copied to clipboard
open override var listener: Settings.Listener?
Link copied to clipboard
private val listeners: MutableList<() -> Any>
Link copied to clipboard
private var removeExpiredJob: Deferred<Unit>?
Link copied to clipboard
open override val size: Int

Returns the number of key-value pairs present in this Settings.

Functions

Link copied to clipboard
open override fun addBooleanListener(key: String, defaultValue: Boolean, callback: (Boolean) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addBooleanOrNullListener(key: String, callback: (Boolean?) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addDoubleListener(key: String, defaultValue: Double, callback: (Double) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addDoubleOrNullListener(key: String, callback: (Double?) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addFloatListener(key: String, defaultValue: Float, callback: (Float) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addFloatOrNullListener(key: String, callback: (Float?) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addIntListener(key: String, defaultValue: Int, callback: (Int) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addIntOrNullListener(key: String, callback: (Int?) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
private fun addListener(key: String, callback: () -> Unit): Settings.Listener
Link copied to clipboard
open override fun addLongListener(key: String, defaultValue: Long, callback: (Long) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addLongOrNullListener(key: String, callback: (Long?) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addStringListener(key: String, defaultValue: String, callback: (String) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun addStringOrNullListener(key: String, callback: (String?) -> Unit): Settings.Listener

Adds a listener which will call the supplied callback anytime the value at key changes. A ISettings reference is returned which can be used to halt callbacks by calling ISettings.deactivate. A strong reference should be held to the CommonSettings returned by this method in order to avoid it being garbage-collected.

Link copied to clipboard
open override fun cancelRemovingExpiredKeys()

In case you need to stop removing expired keys

Link copied to clipboard
open override fun clear()

Clears all values stored in this Settings instance.

Link copied to clipboard
inline operator fun Settings.contains(key: String): Boolean

Equivalent to Settings.hasKey

Link copied to clipboard
inline operator fun <T : Any> Settings.get(key: String): T?

Get the typed value stored at key if present, or return null if not. Throws IllegalArgumentException if T is not one of Int, Long, String, Float, Double, or Boolean.

inline operator fun Settings.get(key: String, defaultValue: Boolean): Boolean

Equivalent to Settings.getBoolean

inline operator fun Settings.get(key: String, defaultValue: Double): Double

Equivalent to Settings.getDouble

inline operator fun Settings.get(key: String, defaultValue: Float): Float

Equivalent to Settings.getFloat

inline operator fun Settings.get(key: String, defaultValue: Int): Int

Equivalent to Settings.getInt

inline operator fun Settings.get(key: String, defaultValue: Long): Long

Equivalent to Settings.getLong

inline operator fun Settings.get(key: String, defaultValue: String): String

Equivalent to Settings.getString

Link copied to clipboard
open override fun getBoolean(key: String, defaultValue: Boolean): Boolean

Returns the Boolean value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getBooleanFlow(key: String): Flow<Boolean?>

Returns a Flow Boolean value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

open override fun getBooleanFlow(key: String, defaultValue: Boolean): Flow<Boolean>

Returns a Flow Boolean value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getBooleanOrNull(key: String): Boolean?

Returns the Boolean value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getDouble(key: String, defaultValue: Double): Double

Returns the Double value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getDoubleFlow(key: String): Flow<Double?>

Returns a Flow Double value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

open override fun getDoubleFlow(key: String, defaultValue: Double): Flow<Double>

Returns a Flow Double value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getDoubleOrNull(key: String): Double?

Returns the Double value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getExpirableString(key: String): String?
open override fun getExpirableString(key: String, default: String?): String?
Link copied to clipboard
open override fun getFloat(key: String, defaultValue: Float): Float

Returns the Float value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getFloatFlow(key: String): Flow<Float?>

Returns a Flow Float value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

open override fun getFloatFlow(key: String, defaultValue: Float): Flow<Float>

Returns a Flow Float value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getFloatOrNull(key: String): Float?

Returns the Float value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getInt(key: String, defaultValue: Int): Int

Returns the Int value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getIntFlow(key: String): Flow<Int?>

Returns a Flow Int value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

open override fun getIntFlow(key: String, defaultValue: Int): Flow<Int>

Returns a Flow Int value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getIntOrNull(key: String): Int?

Returns the Int value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getLong(key: String, defaultValue: Long): Long

Returns the Long value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getLongFlow(key: String): Flow<Long?>

Returns a Flow Long value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

open override fun getLongFlow(key: String, defaultValue: Long): Flow<Long>

Returns a Flow Long value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getLongOrNull(key: String): Long?

Returns the Long value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getString(key: String, defaultValue: String): String

Returns the String value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getStringFlow(key: String): Flow<String?>

Returns a Flow String value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

open override fun getStringFlow(key: String, defaultValue: String): Flow<String>

Returns a Flow String value stored at key, or defaultValue if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun getStringOrNull(key: String): String?

Returns the String value stored at key, or null if no value was stored. If a value of a different type was stored at key, the behavior is not defined.

Link copied to clipboard
open override fun hasKey(key: String): Boolean

Returns true if there is a value stored at key, or false otherwise.

Link copied to clipboard
private fun invokeListeners()
Link copied to clipboard
inline operator fun Settings.minusAssign(key: String)

Equivalent to Settings.remove

Link copied to clipboard
open override fun putBoolean(key: String, value: Boolean)

Stores the Boolean value at key.

Link copied to clipboard
open override fun putDouble(key: String, value: Double)

Stores the Double value at key.

Link copied to clipboard
open override fun putExpirableString(key: String, value: String, exp: Long)
open fun putExpirableString(key: String, value: String, exp: Duration)
Link copied to clipboard
open override fun putFloat(key: String, value: Float)

Stores the Float value at key.

Link copied to clipboard
open override fun putInt(key: String, value: Int)

Stores the Int value at key.

Link copied to clipboard
open override fun putLong(key: String, value: Long)

Stores the Long value at key.

Link copied to clipboard
open override fun putString(key: String, value: String)

Stores the String value at key.

Link copied to clipboard
open override fun remove(key: String)

Removes the value stored at key.

Link copied to clipboard
open override fun removeExpiredKeys()

Will loop all keys and remove the expired ones

Link copied to clipboard
inline operator fun <T : Any> Settings.set(key: String, value: T?)

Stores a typed value at key, or remove what's there if value is null. Throws IllegalArgumentException if T is not one of Int, Long, String, Float, Double, or Boolean.

inline operator fun Settings.set(key: String, value: Boolean)

Equivalent to Settings.putBoolean

inline operator fun Settings.set(key: String, value: Double)

Equivalent to Settings.putDouble

inline operator fun Settings.set(key: String, value: Float)

Equivalent to Settings.putFloat

inline operator fun Settings.set(key: String, value: Int)

Equivalent to Settings.putInt

inline operator fun Settings.set(key: String, value: Long)

Equivalent to Settings.putLong

inline operator fun Settings.set(key: String, value: Nothing?)

Equivalent to Settings.remove

inline operator fun Settings.set(key: String, value: String)

Equivalent to Settings.putString