diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CalculatePsychomatrixByDate.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CalculatePsychomatrixByDateUseCase.kt similarity index 100% rename from src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CalculatePsychomatrixByDate.kt rename to src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CalculatePsychomatrixByDateUseCase.kt 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 6082149..d9fcee9 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 @@ -1,12 +1,47 @@ 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 + private var mutableNumbers = super.numbers.toMutableList() override val numbers: ByteArray - get() = mutableNumbers + get() = mutableNumbers.toByteArray() + private val mutableOperationsHistory: MutableList = ArrayList() + val availableOperations: Deferred> + get() = async { + availableConverts.await().plus(availableInverts.await()) + } + + private val availableConverts: Deferred> + get() = async { + availableConverts(mutableNumbers, mutableOperationsHistory) + } + private val availableInverts: Deferred> + 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 + } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CalculatePsychomatrixByDateInteractor.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CalculatePsychomatrixByDateUseCaseInteractor.kt similarity index 100% rename from src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CalculatePsychomatrixByDateInteractor.kt rename to src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CalculatePsychomatrixByDateUseCaseInteractor.kt