Skip to content

Commit

Permalink
Created KeyValueProperty and related components
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed Feb 25, 2025
1 parent 055c9e9 commit 774493e
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
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)"
}
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
}

0 comments on commit 774493e

Please sign in to comment.