From f64dcacd638a81ba3b4df778f07db3f8878102f4 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 23 Mar 2024 09:37:43 +0700 Subject: [PATCH] StreamCodec.Pair --- gradle.properties | 2 +- .../ru/dbotthepony/kommons/io/StreamCodec.kt | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index dfabf3e..3d73e82 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.10.1 +projectVersion=2.10.2 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/StreamCodec.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/StreamCodec.kt index c35b48a..da28fc1 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/StreamCodec.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/StreamCodec.kt @@ -213,6 +213,30 @@ interface StreamCodec { return copy.invoke(value) } } + + class Pair(private val left: StreamCodec, private val right: StreamCodec) : StreamCodec> { + override fun read(stream: DataInputStream): kotlin.Pair { + val left = left.read(stream) + val right = right.read(stream) + return left to right + } + + override fun write(stream: DataOutputStream, value: kotlin.Pair) { + left.write(stream, value.first) + right.write(stream, value.second) + } + + override fun copy(value: kotlin.Pair): kotlin.Pair { + val left = left.copy(value.first) + val right = right.copy(value.second) + + if (left !== this.left && right !== this.right) { + return left to right + } else { + return value + } + } + } } fun StreamCodec.map(from: T.() -> R, to: R.() -> T): StreamCodec {