StreamCodec.Map
This commit is contained in:
parent
7a344ceece
commit
b4143a11d0
@ -4,7 +4,7 @@ kotlin.code.style=official
|
||||
specifyKotlinAsDependency=false
|
||||
|
||||
projectGroup=ru.dbotthepony.kommons
|
||||
projectVersion=2.9.25
|
||||
projectVersion=2.10.0
|
||||
|
||||
guavaDepVersion=33.0.0
|
||||
gsonDepVersion=2.8.9
|
||||
|
@ -108,6 +108,26 @@ interface StreamCodec<V> {
|
||||
}
|
||||
}
|
||||
|
||||
class Map<K, V, C : MutableMap<K, V>>(val keyCodec: StreamCodec<K>, val valueCodec: StreamCodec<V>, val factory: (Int) -> C): StreamCodec<C> {
|
||||
override fun read(stream: DataInputStream): C {
|
||||
return stream.readMap(keyCodec::read, valueCodec::read, factory)
|
||||
}
|
||||
|
||||
override fun write(stream: DataOutputStream, value: C) {
|
||||
stream.writeMap(value, keyCodec::write, valueCodec::write)
|
||||
}
|
||||
|
||||
override fun copy(value: C): C {
|
||||
val new = factory.invoke(value.size)
|
||||
|
||||
for ((k, v) in value.entries) {
|
||||
new[keyCodec.copy(k)] = valueCodec.copy(v)
|
||||
}
|
||||
|
||||
return new
|
||||
}
|
||||
}
|
||||
|
||||
class Enum<V : kotlin.Enum<V>>(clazz: Class<out V>) : StreamCodec<V> {
|
||||
val clazz = searchClass(clazz)
|
||||
val values: List<V> = listOf(*this.clazz.enumConstants!!)
|
||||
|
Loading…
Reference in New Issue
Block a user