[Commonizer] Small clean-up in IllegalCommonizerStateException
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ abstract class AbstractListCommonizer<T, R>(
|
|||||||
private var error = false
|
private var error = false
|
||||||
|
|
||||||
final override val result: List<R>
|
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 {
|
final override fun commonizeWith(next: List<T>): Boolean {
|
||||||
if (error)
|
if (error)
|
||||||
|
|||||||
+2
-1
@@ -27,7 +27,8 @@ abstract class AbstractNullableCommonizer<T : Any, R : Any, WT, WR>(
|
|||||||
|
|
||||||
final override val result: R?
|
final override val result: R?
|
||||||
get() = when (state) {
|
get() = when (state) {
|
||||||
State.EMPTY, State.ERROR -> throw IllegalCommonizerStateException()
|
State.EMPTY -> failInEmptyState()
|
||||||
|
State.ERROR -> failInErrorState()
|
||||||
State.WITH_WRAPPED -> builder(wrapped.result)
|
State.WITH_WRAPPED -> builder(wrapped.result)
|
||||||
State.WITHOUT_WRAPPED -> null // null means there is no commonized result
|
State.WITHOUT_WRAPPED -> null // null means there is no commonized result
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
|
|||||||
|
|
||||||
override val result: CirAnnotation
|
override val result: CirAnnotation
|
||||||
get() {
|
get() {
|
||||||
val level: DeprecationLevel = level ?: throw IllegalCommonizerStateException()
|
val level: DeprecationLevel = level ?: failInEmptyState()
|
||||||
val messageValue: StringValue = message.toDeprecationMessageValue()
|
val messageValue: StringValue = message.toDeprecationMessageValue()
|
||||||
|
|
||||||
val constantValueArguments: Map<Name, ConstantValue<*>> = if (level == WARNING) {
|
val constantValueArguments: Map<Name, ConstantValue<*>> = if (level == WARNING) {
|
||||||
|
|||||||
+19
-6
@@ -20,13 +20,14 @@ abstract class AbstractStandardCommonizer<T, R> : Commonizer<T, R> {
|
|||||||
private var state = State.EMPTY
|
private var state = State.EMPTY
|
||||||
|
|
||||||
protected val hasResult: Boolean
|
protected val hasResult: Boolean
|
||||||
get() = when (state) {
|
get() = state == State.IN_PROGRESS
|
||||||
State.EMPTY, State.ERROR -> false
|
|
||||||
State.IN_PROGRESS -> true
|
|
||||||
}
|
|
||||||
|
|
||||||
final override val result: R
|
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 {
|
final override fun commonizeWith(next: T): Boolean {
|
||||||
val result = when (state) {
|
val result = when (state) {
|
||||||
@@ -49,4 +50,16 @@ abstract class AbstractStandardCommonizer<T, R> : Commonizer<T, R> {
|
|||||||
protected abstract fun doCommonizeWith(next: T): Boolean
|
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")
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ class ModalityCommonizer : Commonizer<Modality, Modality> {
|
|||||||
private var error = false
|
private var error = false
|
||||||
|
|
||||||
override val result: Modality
|
override val result: Modality
|
||||||
get() = temp?.takeIf { !error } ?: throw IllegalCommonizerStateException()
|
get() = checkState(temp, error)
|
||||||
|
|
||||||
override fun commonizeWith(next: Modality): Boolean {
|
override fun commonizeWith(next: Modality): Boolean {
|
||||||
if (error)
|
if (error)
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ abstract class VisibilityCommonizer : Commonizer<CirHasVisibility, Visibility> {
|
|||||||
private var temp: Visibility? = null
|
private var temp: Visibility? = null
|
||||||
|
|
||||||
override val result: Visibility
|
override val result: Visibility
|
||||||
get() = temp?.takeIf { it != Visibilities.UNKNOWN } ?: throw IllegalCommonizerStateException()
|
get() = checkState(temp, temp == Visibilities.UNKNOWN)
|
||||||
|
|
||||||
override fun commonizeWith(next: CirHasVisibility): Boolean {
|
override fun commonizeWith(next: CirHasVisibility): Boolean {
|
||||||
if (temp == Visibilities.UNKNOWN)
|
if (temp == Visibilities.UNKNOWN)
|
||||||
|
|||||||
Reference in New Issue
Block a user