From 069d488ee5a7c17304c674dbc56566b22236760f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 7 Sep 2018 22:21:52 +0800 Subject: [PATCH] remove most part of suspensions --- .../UseCases/ModifyPsychomatrixUseCase.kt | 10 +-- .../domain/entities/MutablePsychomatrix.kt | 30 +++---- .../domain/entities/operations/Operation.kt | 88 +++++++++---------- .../ModifyPsychomatrixUseCaseInteractor.kt | 18 ++-- .../ModifyPsychomatrixPresenterImpl.kt | 10 +-- .../presenters/ModifyPsychomatrixPresenter.kt | 10 +-- 6 files changed, 76 insertions(+), 90 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt index 8092659..e0bdc64 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt @@ -13,11 +13,11 @@ interface ModifyPsychomatrixUseCase { fun openPsychomatrixChangedSubscription(): ReceiveChannel - fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred - fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred + fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean + fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean - fun getConverts(psychomatrix: Psychomatrix): Deferred> - fun getInverts(psychomatrix: Psychomatrix): Deferred> + fun getConverts(psychomatrix: Psychomatrix): List + fun getInverts(psychomatrix: Psychomatrix): List - fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> + fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt index bb418df..a6113af 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt @@ -17,35 +17,25 @@ class MutablePsychomatrix(date: DateTime) : Psychomatrix(date) { constructor(psychomatrix: Psychomatrix): this(psychomatrix.date) - val operationsHistory: Deferred> - get() = async { - mutableOperationsHistory - } + val operationsHistory: List + get() = mutableOperationsHistory - val availableOperations: Deferred> - get() = async { - availableConverts.await().plus(availableInverts.await()) - } + val availableConverts: List + get() = availableConverts(mutableNumbers, mutableOperationsHistory) + val availableInverts: List + get() = availableInverts(mutableNumbers, mutableOperationsHistory) - val availableConverts: Deferred> - get() = async { - availableConverts(mutableNumbers, mutableOperationsHistory) - } - val availableInverts: Deferred> - get() = async { - availableInverts(mutableNumbers, mutableOperationsHistory) - } - suspend fun applyConvert(operation: Operation): Boolean { - if (operation in availableConverts.await()) { + fun applyConvert(operation: Operation): Boolean { + if (operation in availableConverts) { operation.convert(mutableNumbers, mutableOperationsHistory) return true } return false } - suspend fun applyInvert(operation: Operation): Boolean { - if (operation in availableInverts.await()) { + fun applyInvert(operation: Operation): Boolean { + if (operation in availableInverts) { operation.invert(mutableNumbers, mutableOperationsHistory) return true } diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt index 970e7d1..56cd7f5 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt @@ -29,44 +29,44 @@ private val operations: List = listOf( }.toTypedArray() ) -suspend fun availableConverts(numbers: MutableList, history: List): List { +fun availableConverts(numbers: MutableList, history: List): List { return operations.filter { it.canConvert(numbers, history) } } -suspend fun availableInverts(numbers: MutableList, history: List): List { +fun availableInverts(numbers: MutableList, history: List): List { return operations.filter { it.canInvert(numbers, history) } } sealed class Operation { - abstract suspend fun canConvert(numbers: List, changesHistory: List): Boolean - abstract suspend fun canInvert(numbers: List, changesHistory: List): Boolean - suspend fun convert(numbers: MutableList, changesHistory: MutableList? = null) { + abstract fun canConvert(numbers: List, changesHistory: List): Boolean + abstract fun canInvert(numbers: List, changesHistory: List): Boolean + fun convert(numbers: MutableList, changesHistory: MutableList? = null) { if (changesHistory ?.let { canConvert(numbers, changesHistory) } == true || changesHistory == null) { doConvert(numbers, changesHistory) changesHistory ?.add(this) } } - suspend fun invert(numbers: MutableList, changesHistory: MutableList? = null) { + fun invert(numbers: MutableList, changesHistory: MutableList? = null) { if (changesHistory == null || (changesHistory.contains(this) && canInvert(numbers, changesHistory))) { doInvert(numbers, changesHistory) changesHistory ?.remove(this) } } - protected abstract suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) - protected abstract suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) + protected abstract fun doConvert(numbers: MutableList, changesHistory: MutableList?) + protected abstract fun doInvert(numbers: MutableList, changesHistory: MutableList?) } object TwoGrowFour : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == twoAsByte } / 2 > changesHistory.count { it == FourGrowTwo } } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(fourAsByte) && changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { numbers.apply { remove(twoAsByte) remove(twoAsByte) @@ -74,7 +74,7 @@ object TwoGrowFour : Operation() { } } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { numbers.apply { add(twoAsByte) add(twoAsByte) @@ -84,32 +84,32 @@ object TwoGrowFour : Operation() { } object FourGrowTwo : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == fourAsByte } > changesHistory.count { it == TwoGrowFour } } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == twoAsByte } > 1 && changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { TwoGrowFour.invert(numbers) } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { TwoGrowFour.convert(numbers) } } object EightGrowOne : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(eightAsByte) && (TwoGrowFour.canConvert(numbers, changesHistory) || FourGrowTwo.canConvert(numbers, changesHistory)) } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == oneAsByte } > 1 && changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { numbers.apply { if (count { it == twoAsByte} <= 1) { FourGrowTwo.convert(numbers, changesHistory) @@ -122,7 +122,7 @@ object EightGrowOne : Operation() { } } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { numbers.apply { FourGrowTwo.invert(numbers, changesHistory) add(eightAsByte) @@ -133,39 +133,39 @@ object EightGrowOne : Operation() { } object OneGrowEight : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == oneAsByte} > 1 } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(eightAsByte) && changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { EightGrowOne.invert(numbers) } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { EightGrowOne.convert(numbers) } } object SixGrowSeven : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sixAsByte) } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sevenAsByte) && changesHistory.contains(this) && !changesHistory.containsSimpleGrows } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { numbers.apply { remove(sixAsByte) add(sevenAsByte) } } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { numbers.apply { add(sixAsByte) remove(sevenAsByte) @@ -174,25 +174,25 @@ object SixGrowSeven : Operation() { } object SevenGrowSix : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sevenAsByte) && !changesHistory.containsSimpleGrows } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sixAsByte) && changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { SixGrowSeven.invert(numbers) } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { SixGrowSeven.convert(numbers) } } object FiveGrowNine : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && ( numbers.count { @@ -201,56 +201,56 @@ object FiveGrowNine : Operation() { ) > changesHistory.count { it == this } } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { numbers.add(nineAsByte) } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { numbers.remove(nineAsByte) } } object NineGrowFive : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && (numbers.count { it == nineAsByte } - changesHistory.count { it == FiveGrowNine }) > changesHistory.count { it == this } } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return changesHistory.contains(this) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { numbers.add(fiveAsByte) numbers.add(fiveAsByte) } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { numbers.remove(fiveAsByte) numbers.remove(fiveAsByte) } } class GrowCustom internal constructor(val number: Byte) : Operation() { - override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { + override fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null } - override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { + override fun canInvert(numbers: List, changesHistory: List): Boolean { return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null && numbers.contains(number) } - override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doConvert(numbers: MutableList, changesHistory: MutableList?) { numbers.add(number) } - override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + override fun doInvert(numbers: MutableList, changesHistory: MutableList?) { numbers.remove(number) } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt index eeb04a9..bde8a50 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt @@ -20,27 +20,23 @@ class ModifyPsychomatrixUseCaseInteractor : ModifyPsychomatrixUseCase { return psychomatrixChangedBroadcastChannel.openSubscription() } - override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { - return async { - asMutablePsychomatrix(psychomatrix).applyConvert(operation) - } + override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean { + return asMutablePsychomatrix(psychomatrix).applyConvert(operation) } - override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { - return async { - asMutablePsychomatrix(psychomatrix).applyInvert(operation) - } + override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean { + return asMutablePsychomatrix(psychomatrix).applyInvert(operation) } - override fun getConverts(psychomatrix: Psychomatrix): Deferred> { + override fun getConverts(psychomatrix: Psychomatrix): List { return asMutablePsychomatrix(psychomatrix).availableConverts } - override fun getInverts(psychomatrix: Psychomatrix): Deferred> { + override fun getInverts(psychomatrix: Psychomatrix): List { return asMutablePsychomatrix(psychomatrix).availableInverts } - override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> { + override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List { return asMutablePsychomatrix(psychomatrix).operationsHistory } diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt index 45814b7..bddd442 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt @@ -21,23 +21,23 @@ class ModifyPsychomatrixPresenterImpl( return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription() } - override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { + override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean { return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation) } - override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { + override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean { return modifyPsychomatrixUseCase.makeInvert(psychomatrix, operation) } - override fun getConverts(psychomatrix: Psychomatrix): Deferred> { + override fun getConverts(psychomatrix: Psychomatrix): List { return modifyPsychomatrixUseCase.getConverts(psychomatrix) } - override fun getInverts(psychomatrix: Psychomatrix): Deferred> { + override fun getInverts(psychomatrix: Psychomatrix): List { return modifyPsychomatrixUseCase.getInverts(psychomatrix) } - override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> { + override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List { return getInverts(psychomatrix) } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt index f328cb2..7d51748 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt @@ -12,11 +12,11 @@ interface ModifyPsychomatrixPresenter { fun openPsychomatrixChangedSubscription(): ReceiveChannel - fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred - fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred + fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean + fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean - fun getConverts(psychomatrix: Psychomatrix): Deferred> - fun getInverts(psychomatrix: Psychomatrix): Deferred> + fun getConverts(psychomatrix: Psychomatrix): List + fun getInverts(psychomatrix: Psychomatrix): List - fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> + fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List } \ No newline at end of file