-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created KeyValueProperty and related components
- Loading branch information
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
data-shared/src/commonMain/kotlin/com/mooncloak/vpn/data/shared/DefaultKeyValueProperty.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.mooncloak.vpn.data.shared | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.serialization.KSerializer | ||
|
||
internal open class DefaultKeyValueProperty<Value : Any> internal constructor( | ||
protected val key: String, | ||
protected val serializer: KSerializer<Value>, | ||
protected val storage: MutableKeyValueStorage | ||
) : KeyValueProperty<Value>, | ||
MutableKeyValueProperty<Value> { | ||
|
||
override suspend fun get(): Value? = | ||
storage.get( | ||
key = key, | ||
deserializer = serializer | ||
) | ||
|
||
override suspend fun set(value: Value?) { | ||
storage.set( | ||
key = key, | ||
value = value, | ||
serializer = serializer | ||
) | ||
} | ||
|
||
override suspend fun remove() { | ||
storage.remove(key = key) | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is DefaultKeyValueProperty<*>) return false | ||
|
||
if (key != other.key) return false | ||
if (serializer != other.serializer) return false | ||
|
||
return storage == other.storage | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = key.hashCode() | ||
result = 31 * result + serializer.hashCode() | ||
result = 31 * result + storage.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String = "DefaultKeyValueProperty(key='$key', serializer=$serializer, storage=$storage)" | ||
} | ||
|
||
internal class DefaultFlowableKeyValueProperty<Value : Any> internal constructor( | ||
key: String, | ||
serializer: KSerializer<Value>, | ||
storage: MutableKeyValueStorage, | ||
private val flowableStorage: FlowableKeyValueStorage | ||
) : DefaultKeyValueProperty<Value>( | ||
key = key, | ||
serializer = serializer, | ||
storage = storage | ||
), KeyValueProperty<Value>, | ||
MutableKeyValueProperty<Value>, | ||
FlowableKeyValueProperty<Value> { | ||
|
||
override fun flow(): Flow<Value?> = | ||
flowableStorage.flow( | ||
key = key, | ||
deserializer = serializer | ||
) | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is DefaultFlowableKeyValueProperty<*>) return false | ||
if (!super.equals(other)) return false | ||
|
||
return flowableStorage == other.flowableStorage | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = super.hashCode() | ||
result = 31 * result + flowableStorage.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String = "DefaultFlowableKeyValueProperty(flowableStorage=$flowableStorage)" | ||
} |
70 changes: 70 additions & 0 deletions
70
data-shared/src/commonMain/kotlin/com/mooncloak/vpn/data/shared/KeyValueProperty.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.mooncloak.vpn.data.shared | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Represents a property that holds a value associated with a key. | ||
* | ||
* This interface provides a way to retrieve the stored value asynchronously. | ||
* | ||
* @param Value The type of the value held by this property. | ||
*/ | ||
public interface KeyValueProperty<Value : Any> { | ||
|
||
/** | ||
* Retrieves a value. | ||
* | ||
* This function attempts to retrieve a value. | ||
* | ||
* @return The retrieved value if it exists, or `null` if no value is available. | ||
*/ | ||
public suspend fun get(): Value? | ||
|
||
public companion object | ||
} | ||
|
||
/** | ||
* Represents a key-value property that can be modified. | ||
* | ||
* This interface extends [KeyValueProperty] and provides methods to set and remove the associated value. | ||
* | ||
* @param Value The type of the value associated with the property. Must be non-nullable. | ||
*/ | ||
public interface MutableKeyValueProperty<Value : Any> : KeyValueProperty<Value> { | ||
|
||
/** | ||
* Sets the current value. | ||
* | ||
* This function allows updating the stored value asynchronously. | ||
* | ||
* @param value The new value to set. Can be null. | ||
*/ | ||
public suspend fun set(value: Value?) | ||
|
||
/** | ||
* Removes the current resource or object associated with this instance. | ||
*/ | ||
public suspend fun remove() | ||
|
||
public companion object | ||
} | ||
|
||
/** | ||
* Represents a key-value property that provides a reactive stream of its value changes. | ||
* | ||
* This interface extends [KeyValueProperty] and adds the capability to observe changes to the property's value through | ||
* a [Flow]. This allows for reactive programming patterns to be used when interacting with the property. | ||
* | ||
* @param Value The type of the value stored in the property. | ||
* | ||
* @see KeyValueProperty | ||
*/ | ||
public interface FlowableKeyValueProperty<Value : Any> : KeyValueProperty<Value> { | ||
|
||
/** | ||
* Retrieves a [Flow] of the underlying value changes. | ||
*/ | ||
public fun flow(): Flow<Value?> | ||
|
||
public companion object | ||
} |