From 0c27bb7170fa9763b8a73047b6831c3ac4d2e15a Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Wed, 25 Nov 2015 10:26:42 +0100 Subject: [PATCH] implementing equals and hashCode for CallResult classes, making get polymorphic via virtual method --- .../jetbrains/kotlin/rmi/CompileService.kt | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt b/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt index 00c9afe57ea..4056b9b22f4 100644 --- a/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt +++ b/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt @@ -37,19 +37,31 @@ public interface CompileService : Remote { } public sealed class CallResult : Serializable { - class Good(val result: R) : CallResult() - class Ok : CallResult() - class Dying : CallResult() - class Error(val message: String) : CallResult() + + class Good(val result: R) : CallResult() { + override fun get(): R = result + 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() { + 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() { + 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() { + 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<*> - fun get(): R = when (this) { - is Good -> this.result - is Dying -> throw IllegalStateException("Service is dying") - is Error -> throw IllegalStateException(this.message) - else -> throw IllegalStateException("Unknown state") - } + abstract fun get(): R } // TODO: remove!