diff --git a/gradle.properties b/gradle.properties index b815800..b530ef8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=3.3.0 +projectVersion=3.3.1 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/util/Either.kt b/src/main/kotlin/ru/dbotthepony/kommons/util/Either.kt index 5753d3d..0b618d9 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/util/Either.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/util/Either.kt @@ -3,7 +3,28 @@ package ru.dbotthepony.kommons.util /** * Implements a value container which contain either [L] or [R] value */ -class Either private constructor(val left: KOptional, val right: KOptional) { +sealed class Either { + private class Left(override val left: KOptional) : Either() { + override val right: KOptional + get() = KOptional() + + override fun swap(): Either { + return Right(left) + } + } + + private class Right(override val right: KOptional) : Either() { + override val left: KOptional + get() = KOptional() + + override fun swap(): Either { + return Left(right) + } + } + + abstract val left: KOptional + abstract val right: KOptional + val isLeft: Boolean get() = left.isPresent val isRight: Boolean get() = right.isPresent @@ -63,19 +84,17 @@ class Either private constructor(val left: KOptional, val right: KOptio } } - fun swap(): Either { - return Either(right, left) - } + abstract fun swap(): Either companion object { @JvmStatic fun left(value: L): Either { - return Either(KOptional.of(value), KOptional.empty()) + return Left(KOptional(value)) } @JvmStatic fun right(value: R): Either { - return Either(KOptional.empty(), KOptional.of(value)) + return Right(KOptional(value)) } } }