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

tech.harmonysoft.oss.common.ProcessingResult.kt Maven / Gradle / Ivy

package tech.harmonysoft.oss.common

/**
 * Quite often we want to return a value of particular type if the processing is successful and a value
 * of another type in case of processing failure. This class allows to handle that, for example, we might
 * have a method which fetches some data and returns it in case of success or returns error description
 * in case of failure. Its return type can be defined as `ProcessingResult` then.
 */
class ProcessingResult private constructor(
    private val _successValue: SUCCESS?,
    private val _failureValue: FAILURE?
) {

    val success: Boolean
        get() = _failureValue == null

    @Suppress("UNCHECKED_CAST")
    val successValue: SUCCESS
        get() = if (success) {
            _successValue as SUCCESS
        } else {
            throw IllegalStateException("Can't get a success value from a failed result")
        }

    @Suppress("UNCHECKED_CAST")
    val failureValue: FAILURE
        get() = if (success) {
            throw IllegalStateException("Can't get a failure value from a successful result")
        } else {
            _failureValue as FAILURE
        }

    @Suppress("UNCHECKED_CAST")
    fun  mapError(): ProcessingResult {
        if (success) {
            throw IllegalStateException("can't map non-error result $this")
        } else {
            return this as ProcessingResult
        }
    }

    override fun toString(): String {
        return if (success) {
            "success: $_successValue"
        } else {
            "failure: $_failureValue"
        }
    }

    override fun hashCode(): Int {
        return 31 * (_successValue?.hashCode() ?: 0) + (_failureValue?.hashCode() ?: 0)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) {
            return true
        }
        if (javaClass != other?.javaClass) {
            return false
        }

        other as ProcessingResult<*, *>
        return _successValue == other._successValue && _failureValue == other._failureValue
    }

    companion object {

        fun  success(value: S): ProcessingResult {
            return ProcessingResult(value, null)
        }

        fun  success(): ProcessingResult {
            return ProcessingResult(Unit, null)
        }

        fun  failure(value: F): ProcessingResult {
            return ProcessingResult(null, value)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy