tech.harmonysoft.oss.kotlin.baker.impl.Result.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-baker Show documentation
Show all versions of kotlin-baker Show documentation
A library which facilitates Kotlin objects creation from properties
package tech.harmonysoft.oss.kotlin.baker.impl
@Suppress("UNCHECKED_CAST")
class Result private constructor(
private val _successValue: S?,
private val _failureValue: F?,
val success: Boolean
) {
val successValue: S
get() {
if (!success) {
throw IllegalStateException("Can't return a success value from a failed result")
}
return _successValue as S
}
val failureValue: F
get() {
if (success) {
throw IllegalStateException("Can't return a failure value from a successful result")
}
return _failureValue as F
}
override fun toString(): String {
return if (success) {
"success: $successValue"
} else {
"failure: $failureValue"
}
}
companion object {
fun success(result: S): Result {
return Result(result, null, true)
}
fun failure(error: F): Result {
return Result(null, error, false)
}
}
}