Implement left and right as subclasses in Either
This commit is contained in:
parent
806472cc99
commit
be6a353bec
@ -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
|
||||
|
@ -3,7 +3,28 @@ package ru.dbotthepony.kommons.util
|
||||
/**
|
||||
* Implements a value container which contain either [L] or [R] value
|
||||
*/
|
||||
class Either<L, R> private constructor(val left: KOptional<L>, val right: KOptional<R>) {
|
||||
sealed class Either<L, R> {
|
||||
private class Left<L, R>(override val left: KOptional<L>) : Either<L, R>() {
|
||||
override val right: KOptional<R>
|
||||
get() = KOptional()
|
||||
|
||||
override fun swap(): Either<R, L> {
|
||||
return Right(left)
|
||||
}
|
||||
}
|
||||
|
||||
private class Right<L, R>(override val right: KOptional<R>) : Either<L, R>() {
|
||||
override val left: KOptional<L>
|
||||
get() = KOptional()
|
||||
|
||||
override fun swap(): Either<R, L> {
|
||||
return Left(right)
|
||||
}
|
||||
}
|
||||
|
||||
abstract val left: KOptional<L>
|
||||
abstract val right: KOptional<R>
|
||||
|
||||
val isLeft: Boolean get() = left.isPresent
|
||||
val isRight: Boolean get() = right.isPresent
|
||||
|
||||
@ -63,19 +84,17 @@ class Either<L, R> private constructor(val left: KOptional<L>, val right: KOptio
|
||||
}
|
||||
}
|
||||
|
||||
fun swap(): Either<R, L> {
|
||||
return Either(right, left)
|
||||
}
|
||||
abstract fun swap(): Either<R, L>
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun <L, R> left(value: L): Either<L, R> {
|
||||
return Either(KOptional.of(value), KOptional.empty())
|
||||
return Left(KOptional(value))
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun <L, R> right(value: R): Either<L, R> {
|
||||
return Either(KOptional.empty(), KOptional.of(value))
|
||||
return Right(KOptional(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user