implementing equals and hashCode for CallResult classes, making get polymorphic via virtual method

This commit is contained in:
Ilya Chernikov
2015-11-25 10:26:42 +01:00
parent 6867023274
commit 0c27bb7170
@@ -37,19 +37,31 @@ public interface CompileService : Remote {
} }
public sealed class CallResult<out R> : Serializable { public sealed class CallResult<out R> : Serializable {
class Good<R>(val result: R) : CallResult<R>()
class Ok : CallResult<Nothing>() class Good<R>(val result: R) : CallResult<R>() {
class Dying : CallResult<Nothing>() override fun get(): R = result
class Error(val message: String) : CallResult<Nothing>() override fun equals(other: Any?): Boolean = other is Good<*> && this.result == other.result
override fun hashCode(): Int = this.javaClass.hashCode() + (result?.hashCode() ?: 1)
}
class Ok : CallResult<Nothing>() {
override fun get(): Nothing = throw IllegalStateException("Gey is inapplicable to Ok call result")
override fun equals(other: Any?): Boolean = other is Ok
override fun hashCode(): Int = this.javaClass.hashCode() + 1 // avoiding clash with the hash of class itself
}
class Dying : CallResult<Nothing>() {
override fun get(): Nothing = throw IllegalStateException("Service is dying")
override fun equals(other: Any?): Boolean = other is Dying
override fun hashCode(): Int = this.javaClass.hashCode() + 1 // see comment to Ok.hashCode
}
class Error(val message: String) : CallResult<Nothing>() {
override fun get(): Nothing = throw Exception(message)
override fun equals(other: Any?): Boolean = other is Error && this.message == other.message
override fun hashCode(): Int = this.javaClass.hashCode() + message.hashCode()
}
val isGood: Boolean get() = this is Good<*> val isGood: Boolean get() = this is Good<*>
fun get(): R = when (this) { abstract fun get(): R
is Good<R> -> this.result
is Dying -> throw IllegalStateException("Service is dying")
is Error -> throw IllegalStateException(this.message)
else -> throw IllegalStateException("Unknown state")
}
} }
// TODO: remove! // TODO: remove!