add convert/invert oerations interaction for MutablePsychomatrix

This commit is contained in:
InsanusMokrassar 2018-09-05 20:03:16 +08:00
parent 68b342d0e4
commit a3f22be582
3 changed files with 37 additions and 2 deletions

View File

@ -1,12 +1,47 @@
package com.github.insanusmokrassar.PsychomatrixBase.domain.entities package com.github.insanusmokrassar.PsychomatrixBase.domain.entities
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.Operation
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.availableConverts
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.availableInverts
import kotlinx.coroutines.experimental.Deferred
import kotlinx.coroutines.experimental.async
import org.joda.time.DateTime import org.joda.time.DateTime
class MutablePsychomatrix(date: DateTime) : Psychomatrix(date) { class MutablePsychomatrix(date: DateTime) : Psychomatrix(date) {
private var mutableNumbers = super.numbers private var mutableNumbers = super.numbers.toMutableList()
override val numbers: ByteArray override val numbers: ByteArray
get() = mutableNumbers get() = mutableNumbers.toByteArray()
private val mutableOperationsHistory: MutableList<Operation> = ArrayList()
val availableOperations: Deferred<List<Operation>>
get() = async {
availableConverts.await().plus(availableInverts.await())
}
private val availableConverts: Deferred<List<Operation>>
get() = async {
availableConverts(mutableNumbers, mutableOperationsHistory)
}
private val availableInverts: Deferred<List<Operation>>
get() = async {
availableInverts(mutableNumbers, mutableOperationsHistory)
}
suspend fun applyConvert(operation: Operation): Boolean {
if (operation in availableConverts.await()) {
operation.convert(mutableNumbers, mutableOperationsHistory)
return true
}
return false
}
suspend fun applyInvert(operation: Operation): Boolean {
if (operation in availableInverts.await()) {
operation.invert(mutableNumbers, mutableOperationsHistory)
return true
}
return false
}
} }