All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.harmonysoft.oss.kotlin.baker.impl.Result.kt Maven / Gradle / Ivy

There is a newer version: 1.0.13
Show newest version
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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy