From a3f22be582f1259b2aa33a3b84c5a6b2ff2460de Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 5 Sep 2018 20:03:16 +0800 Subject: [PATCH] add convert/invert oerations interaction for MutablePsychomatrix --- ... => CalculatePsychomatrixByDateUseCase.kt} | 0 .../domain/entities/MutablePsychomatrix.kt | 39 ++++++++++++++++++- ...atePsychomatrixByDateUseCaseInteractor.kt} | 0 3 files changed, 37 insertions(+), 2 deletions(-) rename src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/{CalculatePsychomatrixByDate.kt => CalculatePsychomatrixByDateUseCase.kt} (100%) rename src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/{CalculatePsychomatrixByDateInteractor.kt => CalculatePsychomatrixByDateUseCaseInteractor.kt} (100%) 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