[Commonizer] Small clean-up in IllegalCommonizerStateException

This commit is contained in:
Dmitriy Dolovov
2020-08-05 19:23:57 +07:00
parent 494fb39399
commit 3b398ed57f
6 changed files with 25 additions and 11 deletions
@@ -22,7 +22,7 @@ abstract class AbstractListCommonizer<T, R>(
private var error = false
final override val result: List<R>
get() = commonizers?.takeIf { !error }?.map { it.result } ?: throw IllegalCommonizerStateException()
get() = checkState(commonizers, error).map { it.result }
final override fun commonizeWith(next: List<T>): Boolean {
if (error)
@@ -27,7 +27,8 @@ abstract class AbstractNullableCommonizer<T : Any, R : Any, WT, WR>(
final override val result: R?
get() = when (state) {
State.EMPTY, State.ERROR -> throw IllegalCommonizerStateException()
State.EMPTY -> failInEmptyState()
State.ERROR -> failInErrorState()
State.WITH_WRAPPED -> builder(wrapped.result)
State.WITHOUT_WRAPPED -> null // null means there is no commonized result
}
@@ -61,7 +61,7 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
override val result: CirAnnotation
get() {
val level: DeprecationLevel = level ?: throw IllegalCommonizerStateException()
val level: DeprecationLevel = level ?: failInEmptyState()
val messageValue: StringValue = message.toDeprecationMessageValue()
val constantValueArguments: Map<Name, ConstantValue<*>> = if (level == WARNING) {
@@ -20,13 +20,14 @@ abstract class AbstractStandardCommonizer<T, R> : Commonizer<T, R> {
private var state = State.EMPTY
protected val hasResult: Boolean
get() = when (state) {
State.EMPTY, State.ERROR -> false
State.IN_PROGRESS -> true
}
get() = state == State.IN_PROGRESS
final override val result: R
get() = if (hasResult) commonizationResult() else throw IllegalCommonizerStateException()
get() = when (state) {
State.EMPTY -> failInEmptyState()
State.ERROR -> failInErrorState()
State.IN_PROGRESS -> commonizationResult()
}
final override fun commonizeWith(next: T): Boolean {
val result = when (state) {
@@ -49,4 +50,16 @@ abstract class AbstractStandardCommonizer<T, R> : Commonizer<T, R> {
protected abstract fun doCommonizeWith(next: T): Boolean
}
class IllegalCommonizerStateException : IllegalStateException("Illegal commonizer state: error or empty")
@Suppress("unused")
fun Commonizer<*, *>.failInEmptyState(): Nothing = throw IllegalCommonizerStateException("empty")
@Suppress("unused")
fun Commonizer<*, *>.failInErrorState(): Nothing = throw IllegalCommonizerStateException("empty")
fun <T : Any> Commonizer<*, *>.checkState(value: T?, error: Boolean): T = when {
value == null -> failInEmptyState()
error -> failInErrorState()
else -> value
}
class IllegalCommonizerStateException(message: String) : IllegalStateException("Illegal commonizer state: $message")
@@ -12,7 +12,7 @@ class ModalityCommonizer : Commonizer<Modality, Modality> {
private var error = false
override val result: Modality
get() = temp?.takeIf { !error } ?: throw IllegalCommonizerStateException()
get() = checkState(temp, error)
override fun commonizeWith(next: Modality): Boolean {
if (error)
@@ -20,7 +20,7 @@ abstract class VisibilityCommonizer : Commonizer<CirHasVisibility, Visibility> {
private var temp: Visibility? = null
override val result: Visibility
get() = temp?.takeIf { it != Visibilities.UNKNOWN } ?: throw IllegalCommonizerStateException()
get() = checkState(temp, temp == Visibilities.UNKNOWN)
override fun commonizeWith(next: CirHasVisibility): Boolean {
if (temp == Visibilities.UNKNOWN)