remove most part of suspensions

This commit is contained in:
InsanusMokrassar 2018-09-07 22:21:52 +08:00
parent 4a84f602a9
commit 069d488ee5
6 changed files with 76 additions and 90 deletions

View File

@ -13,11 +13,11 @@ interface ModifyPsychomatrixUseCase {
fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert>
fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean
fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean
fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getConverts(psychomatrix: Psychomatrix): List<Operation>
fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getInverts(psychomatrix: Psychomatrix): List<Operation>
fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List<Operation>
} }

View File

@ -17,35 +17,25 @@ class MutablePsychomatrix(date: DateTime) : Psychomatrix(date) {
constructor(psychomatrix: Psychomatrix): this(psychomatrix.date) constructor(psychomatrix: Psychomatrix): this(psychomatrix.date)
val operationsHistory: Deferred<List<Operation>> val operationsHistory: List<Operation>
get() = async { get() = mutableOperationsHistory
mutableOperationsHistory
}
val availableOperations: Deferred<List<Operation>> val availableConverts: List<Operation>
get() = async { get() = availableConverts(mutableNumbers, mutableOperationsHistory)
availableConverts.await().plus(availableInverts.await()) val availableInverts: List<Operation>
} get() = availableInverts(mutableNumbers, mutableOperationsHistory)
val availableConverts: Deferred<List<Operation>>
get() = async {
availableConverts(mutableNumbers, mutableOperationsHistory)
}
val availableInverts: Deferred<List<Operation>>
get() = async {
availableInverts(mutableNumbers, mutableOperationsHistory)
}
suspend fun applyConvert(operation: Operation): Boolean { fun applyConvert(operation: Operation): Boolean {
if (operation in availableConverts.await()) { if (operation in availableConverts) {
operation.convert(mutableNumbers, mutableOperationsHistory) operation.convert(mutableNumbers, mutableOperationsHistory)
return true return true
} }
return false return false
} }
suspend fun applyInvert(operation: Operation): Boolean { fun applyInvert(operation: Operation): Boolean {
if (operation in availableInverts.await()) { if (operation in availableInverts) {
operation.invert(mutableNumbers, mutableOperationsHistory) operation.invert(mutableNumbers, mutableOperationsHistory)
return true return true
} }

View File

@ -29,44 +29,44 @@ private val operations: List<Operation> = listOf(
}.toTypedArray() }.toTypedArray()
) )
suspend fun availableConverts(numbers: MutableList<Byte>, history: List<Operation>): List<Operation> { fun availableConverts(numbers: MutableList<Byte>, history: List<Operation>): List<Operation> {
return operations.filter { it.canConvert(numbers, history) } return operations.filter { it.canConvert(numbers, history) }
} }
suspend fun availableInverts(numbers: MutableList<Byte>, history: List<Operation>): List<Operation> { fun availableInverts(numbers: MutableList<Byte>, history: List<Operation>): List<Operation> {
return operations.filter { it.canInvert(numbers, history) } return operations.filter { it.canInvert(numbers, history) }
} }
sealed class Operation { sealed class Operation {
abstract suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean abstract fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean
abstract suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean abstract fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean
suspend fun convert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>? = null) { fun convert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>? = null) {
if (changesHistory ?.let { canConvert(numbers, changesHistory) } == true || changesHistory == null) { if (changesHistory ?.let { canConvert(numbers, changesHistory) } == true || changesHistory == null) {
doConvert(numbers, changesHistory) doConvert(numbers, changesHistory)
changesHistory ?.add(this) changesHistory ?.add(this)
} }
} }
suspend fun invert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>? = null) { fun invert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>? = null) {
if (changesHistory == null || (changesHistory.contains(this) && canInvert(numbers, changesHistory))) { if (changesHistory == null || (changesHistory.contains(this) && canInvert(numbers, changesHistory))) {
doInvert(numbers, changesHistory) doInvert(numbers, changesHistory)
changesHistory ?.remove(this) changesHistory ?.remove(this)
} }
} }
protected abstract suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) protected abstract fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?)
protected abstract suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) protected abstract fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?)
} }
object TwoGrowFour : Operation() { object TwoGrowFour : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == twoAsByte } / 2 > changesHistory.count { it == FourGrowTwo } return numbers.count { it == twoAsByte } / 2 > changesHistory.count { it == FourGrowTwo }
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(fourAsByte) && changesHistory.contains(this) return numbers.contains(fourAsByte) && changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
remove(twoAsByte) remove(twoAsByte)
remove(twoAsByte) remove(twoAsByte)
@ -74,7 +74,7 @@ object TwoGrowFour : Operation() {
} }
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
add(twoAsByte) add(twoAsByte)
add(twoAsByte) add(twoAsByte)
@ -84,32 +84,32 @@ object TwoGrowFour : Operation() {
} }
object FourGrowTwo : Operation() { object FourGrowTwo : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == fourAsByte } > changesHistory.count { it == TwoGrowFour } return numbers.count { it == fourAsByte } > changesHistory.count { it == TwoGrowFour }
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == twoAsByte } > 1 && changesHistory.contains(this) return numbers.count { it == twoAsByte } > 1 && changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
TwoGrowFour.invert(numbers) TwoGrowFour.invert(numbers)
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
TwoGrowFour.convert(numbers) TwoGrowFour.convert(numbers)
} }
} }
object EightGrowOne : Operation() { object EightGrowOne : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(eightAsByte) return numbers.contains(eightAsByte)
&& (TwoGrowFour.canConvert(numbers, changesHistory) || FourGrowTwo.canConvert(numbers, changesHistory)) && (TwoGrowFour.canConvert(numbers, changesHistory) || FourGrowTwo.canConvert(numbers, changesHistory))
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == oneAsByte } > 1 && changesHistory.contains(this) return numbers.count { it == oneAsByte } > 1 && changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
if (count { it == twoAsByte} <= 1) { if (count { it == twoAsByte} <= 1) {
FourGrowTwo.convert(numbers, changesHistory) FourGrowTwo.convert(numbers, changesHistory)
@ -122,7 +122,7 @@ object EightGrowOne : Operation() {
} }
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
FourGrowTwo.invert(numbers, changesHistory) FourGrowTwo.invert(numbers, changesHistory)
add(eightAsByte) add(eightAsByte)
@ -133,39 +133,39 @@ object EightGrowOne : Operation() {
} }
object OneGrowEight : Operation() { object OneGrowEight : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == oneAsByte} > 1 return numbers.count { it == oneAsByte} > 1
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(eightAsByte) && changesHistory.contains(this) return numbers.contains(eightAsByte) && changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
EightGrowOne.invert(numbers) EightGrowOne.invert(numbers)
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
EightGrowOne.convert(numbers) EightGrowOne.convert(numbers)
} }
} }
object SixGrowSeven : Operation() { object SixGrowSeven : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sixAsByte) return numbers.contains(sixAsByte)
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sevenAsByte) && changesHistory.contains(this) return numbers.contains(sevenAsByte) && changesHistory.contains(this)
&& !changesHistory.containsSimpleGrows && !changesHistory.containsSimpleGrows
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
remove(sixAsByte) remove(sixAsByte)
add(sevenAsByte) add(sevenAsByte)
} }
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
add(sixAsByte) add(sixAsByte)
remove(sevenAsByte) remove(sevenAsByte)
@ -174,25 +174,25 @@ object SixGrowSeven : Operation() {
} }
object SevenGrowSix : Operation() { object SevenGrowSix : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sevenAsByte) return numbers.contains(sevenAsByte)
&& !changesHistory.containsSimpleGrows && !changesHistory.containsSimpleGrows
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sixAsByte) && changesHistory.contains(this) return numbers.contains(sixAsByte) && changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
SixGrowSeven.invert(numbers) SixGrowSeven.invert(numbers)
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
SixGrowSeven.convert(numbers) SixGrowSeven.convert(numbers)
} }
} }
object FiveGrowNine : Operation() { object FiveGrowNine : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& ( && (
numbers.count { numbers.count {
@ -201,56 +201,56 @@ object FiveGrowNine : Operation() {
) > changesHistory.count { it == this } ) > changesHistory.count { it == this }
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.contains(this) return changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.add(nineAsByte) numbers.add(nineAsByte)
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(nineAsByte) numbers.remove(nineAsByte)
} }
} }
object NineGrowFive : Operation() { object NineGrowFive : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& (numbers.count { it == nineAsByte } - changesHistory.count { it == FiveGrowNine }) > changesHistory.count { it == this } && (numbers.count { it == nineAsByte } - changesHistory.count { it == FiveGrowNine }) > changesHistory.count { it == this }
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.contains(this) return changesHistory.contains(this)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.add(fiveAsByte) numbers.add(fiveAsByte)
numbers.add(fiveAsByte) numbers.add(fiveAsByte)
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(fiveAsByte) numbers.remove(fiveAsByte)
numbers.remove(fiveAsByte) numbers.remove(fiveAsByte)
} }
} }
class GrowCustom internal constructor(val number: Byte) : Operation() { class GrowCustom internal constructor(val number: Byte) : Operation() {
override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null
} }
override suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null
&& numbers.contains(number) && numbers.contains(number)
} }
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.add(number) numbers.add(number)
} }
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(number) numbers.remove(number)
} }
} }

View File

@ -20,27 +20,23 @@ class ModifyPsychomatrixUseCaseInteractor : ModifyPsychomatrixUseCase {
return psychomatrixChangedBroadcastChannel.openSubscription() return psychomatrixChangedBroadcastChannel.openSubscription()
} }
override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean {
return async { return asMutablePsychomatrix(psychomatrix).applyConvert(operation)
asMutablePsychomatrix(psychomatrix).applyConvert(operation)
}
} }
override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean {
return async { return asMutablePsychomatrix(psychomatrix).applyInvert(operation)
asMutablePsychomatrix(psychomatrix).applyInvert(operation)
}
} }
override fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getConverts(psychomatrix: Psychomatrix): List<Operation> {
return asMutablePsychomatrix(psychomatrix).availableConverts return asMutablePsychomatrix(psychomatrix).availableConverts
} }
override fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getInverts(psychomatrix: Psychomatrix): List<Operation> {
return asMutablePsychomatrix(psychomatrix).availableInverts return asMutablePsychomatrix(psychomatrix).availableInverts
} }
override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List<Operation> {
return asMutablePsychomatrix(psychomatrix).operationsHistory return asMutablePsychomatrix(psychomatrix).operationsHistory
} }

View File

@ -21,23 +21,23 @@ class ModifyPsychomatrixPresenterImpl(
return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription() return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription()
} }
override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean {
return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation) return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation)
} }
override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean {
return modifyPsychomatrixUseCase.makeInvert(psychomatrix, operation) return modifyPsychomatrixUseCase.makeInvert(psychomatrix, operation)
} }
override fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getConverts(psychomatrix: Psychomatrix): List<Operation> {
return modifyPsychomatrixUseCase.getConverts(psychomatrix) return modifyPsychomatrixUseCase.getConverts(psychomatrix)
} }
override fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getInverts(psychomatrix: Psychomatrix): List<Operation> {
return modifyPsychomatrixUseCase.getInverts(psychomatrix) return modifyPsychomatrixUseCase.getInverts(psychomatrix)
} }
override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List<Operation> {
return getInverts(psychomatrix) return getInverts(psychomatrix)
} }
} }

View File

@ -12,11 +12,11 @@ interface ModifyPsychomatrixPresenter {
fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert>
fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean
fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean
fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getConverts(psychomatrix: Psychomatrix): List<Operation>
fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getInverts(psychomatrix: Psychomatrix): List<Operation>
fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List<Operation>
} }