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 makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean
fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean
fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
fun getConverts(psychomatrix: Psychomatrix): 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)
val operationsHistory: Deferred<List<Operation>>
get() = async {
mutableOperationsHistory
}
val operationsHistory: List<Operation>
get() = mutableOperationsHistory
val availableOperations: Deferred<List<Operation>>
get() = async {
availableConverts.await().plus(availableInverts.await())
}
val availableConverts: List<Operation>
get() = availableConverts(mutableNumbers, mutableOperationsHistory)
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 {
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
}

View File

@ -29,44 +29,44 @@ private val operations: List<Operation> = listOf(
}.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) }
}
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) }
}
sealed class Operation {
abstract suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean
abstract suspend fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean
suspend fun convert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>? = null) {
abstract fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean
abstract fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean
fun convert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>? = null) {
if (changesHistory ?.let { canConvert(numbers, changesHistory) } == true || changesHistory == null) {
doConvert(numbers, changesHistory)
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))) {
doInvert(numbers, changesHistory)
changesHistory ?.remove(this)
}
}
protected abstract suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?)
protected abstract suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?)
protected abstract fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?)
protected abstract fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<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 }
}
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)
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply {
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 {
add(twoAsByte)
add(twoAsByte)
@ -84,32 +84,32 @@ object TwoGrowFour : 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 }
}
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)
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
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)
}
}
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)
&& (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)
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply {
if (count { it == twoAsByte} <= 1) {
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 {
FourGrowTwo.invert(numbers, changesHistory)
add(eightAsByte)
@ -133,39 +133,39 @@ object EightGrowOne : 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
}
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)
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
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)
}
}
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)
}
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)
&& !changesHistory.containsSimpleGrows
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply {
remove(sixAsByte)
add(sevenAsByte)
}
}
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply {
add(sixAsByte)
remove(sevenAsByte)
@ -174,25 +174,25 @@ object SixGrowSeven : 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)
&& !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)
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
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)
}
}
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
&& (
numbers.count {
@ -201,56 +201,56 @@ object FiveGrowNine : Operation() {
) > 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)
}
override suspend fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
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)
}
}
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
&& (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)
}
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)
}
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)
}
}
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
&& 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
&& 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)
}
override suspend fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(number)
}
}

View File

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

View File

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

View File

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