Add StreamCodec.Nullable
This commit is contained in:
parent
b42219ac97
commit
9d6205f9ff
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=2.16.0
|
projectVersion=2.16.1
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ru.dbotthepony.kommons.io
|
package ru.dbotthepony.kommons.io
|
||||||
|
|
||||||
import ru.dbotthepony.kommons.math.RGBAColor
|
import ru.dbotthepony.kommons.math.RGBAColor
|
||||||
|
import ru.dbotthepony.kommons.util.KOptional
|
||||||
import java.io.DataInputStream
|
import java.io.DataInputStream
|
||||||
import java.io.DataOutputStream
|
import java.io.DataOutputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -39,29 +40,6 @@ interface StreamCodec<V> {
|
|||||||
comparator: ((a: V, b: V) -> Boolean) = { a, b -> a == b }
|
comparator: ((a: V, b: V) -> Boolean) = { a, b -> a == b }
|
||||||
) : this({ stream -> reader.invoke(stream) }, writer, copier, comparator)
|
) : this({ stream -> reader.invoke(stream) }, writer, copier, comparator)
|
||||||
|
|
||||||
val nullable = object : StreamCodec<V?> {
|
|
||||||
override fun read(stream: DataInputStream): V? {
|
|
||||||
return if (stream.read() == 0) null else reader.invoke(stream)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun write(stream: DataOutputStream, value: V?) {
|
|
||||||
if (value === null) stream.write(0) else {
|
|
||||||
stream.write(1)
|
|
||||||
writer.invoke(stream, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun copy(value: V?): V? {
|
|
||||||
return if (value === null) null else copier.invoke(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun compare(a: V?, b: V?): Boolean {
|
|
||||||
if (a === null && b === null) return true
|
|
||||||
if (a === null || b === null) return false
|
|
||||||
return comparator.invoke(a, b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun read(stream: DataInputStream): V {
|
override fun read(stream: DataInputStream): V {
|
||||||
return reader.invoke(stream)
|
return reader.invoke(stream)
|
||||||
}
|
}
|
||||||
@ -79,6 +57,31 @@ interface StreamCodec<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Nullable<V>(val parent: StreamCodec<V>) : StreamCodec<V?> {
|
||||||
|
override fun read(stream: DataInputStream): V? {
|
||||||
|
return if (stream.read() == 0) null else parent.read(stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun write(stream: DataOutputStream, value: V?) {
|
||||||
|
if (value === null)
|
||||||
|
stream.write(0)
|
||||||
|
else {
|
||||||
|
stream.write(1)
|
||||||
|
parent.write(stream, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun copy(value: V?): V? {
|
||||||
|
return if (value === null) null else parent.copy(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun compare(a: V?, b: V?): Boolean {
|
||||||
|
if (a === null && b === null) return true
|
||||||
|
if (a === null || b === null) return false
|
||||||
|
return parent.compare(a, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Collection<E, C : MutableCollection<E>>(val elementCodec: StreamCodec<E>, val collectionFactory: (Int) -> C) : StreamCodec<C> {
|
class Collection<E, C : MutableCollection<E>>(val elementCodec: StreamCodec<E>, val collectionFactory: (Int) -> C) : StreamCodec<C> {
|
||||||
override fun read(stream: DataInputStream): C {
|
override fun read(stream: DataInputStream): C {
|
||||||
val size = stream.readVarInt()
|
val size = stream.readVarInt()
|
||||||
@ -247,8 +250,9 @@ fun <T, R> StreamCodec<T>.map(from: T.() -> R, to: R.() -> T, copy: R.() -> R):
|
|||||||
return StreamCodec.Mapping(this, from, to, copy)
|
return StreamCodec.Mapping(this, from, to, copy)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Any> StreamCodec<T>.optional() = StreamCodec.Optional(this)
|
fun <T : Any> StreamCodec<T>.optional(): StreamCodec<Optional<T>> = StreamCodec.Optional(this)
|
||||||
fun <T> StreamCodec<T>.koptional() = StreamCodec.KOptional(this)
|
fun <T> StreamCodec<T>.koptional(): StreamCodec<KOptional<T>> = StreamCodec.KOptional(this)
|
||||||
|
fun <T> StreamCodec<T>.nullable(): StreamCodec<T?> = StreamCodec.Nullable(this)
|
||||||
|
|
||||||
val NullValueCodec = StreamCodec.Impl({ _ -> null }, { _, _ -> })
|
val NullValueCodec = StreamCodec.Impl({ _ -> null }, { _, _ -> })
|
||||||
val BooleanValueCodec = StreamCodec.Impl(DataInputStream::readBoolean, 1L, DataOutputStream::writeBoolean)
|
val BooleanValueCodec = StreamCodec.Impl(DataInputStream::readBoolean, 1L, DataOutputStream::writeBoolean)
|
||||||
|
Loading…
Reference in New Issue
Block a user