mirror of
https://github.com/InsanusMokrassar/PsychomatrixBase.git
synced 2024-06-02 07:55:16 +00:00
55 lines
1.9 KiB
Kotlin
55 lines
1.9 KiB
Kotlin
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
|
|
|
|
class MutablePsychomatrix(date: DateTime) : Psychomatrix(date) {
|
|
private var mutableNumbers = super.numbers.toMutableList()
|
|
|
|
override val numbers: ByteArray
|
|
get() = mutableNumbers.toByteArray()
|
|
|
|
private val mutableOperationsHistory: MutableList<Operation> = ArrayList()
|
|
|
|
constructor(psychomatrix: Psychomatrix): this(psychomatrix.date)
|
|
|
|
val operationsHistory: Deferred<List<Operation>>
|
|
get() = async {
|
|
mutableOperationsHistory
|
|
}
|
|
|
|
val availableOperations: Deferred<List<Operation>>
|
|
get() = async {
|
|
availableConverts.await().plus(availableInverts.await())
|
|
}
|
|
|
|
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()) {
|
|
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
|
|
}
|
|
}
|