From 68b342d0e4807fe3f09ab4eef5914484f197a496 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 5 Sep 2018 14:13:27 +0800 Subject: [PATCH] make numbers in can* methods of operations immutable --- .../domain/entities/operations/Operation.kt | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt index 88dc30f..2742e5a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt @@ -42,8 +42,8 @@ suspend fun availableInverts(numbers: MutableList, operations: List, changesHistory: List): Boolean - abstract suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean + abstract suspend fun canConvert(numbers: List, changesHistory: List): Boolean + abstract suspend fun canInvert(numbers: List, changesHistory: List): Boolean suspend fun convert(numbers: MutableList, changesHistory: MutableList? = null) { if (changesHistory ?.let { canConvert(numbers, changesHistory) } == true || changesHistory == null) { doConvert(numbers, changesHistory) @@ -63,10 +63,10 @@ sealed class Operation { } object TwoGrowFour : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == twoAsByte } / 2 > changesHistory.count { it == FourGrowTwo } } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(fourAsByte) && changesHistory.contains(this) } @@ -88,10 +88,10 @@ object TwoGrowFour : Operation() { } object FourGrowTwo : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == fourAsByte } > changesHistory.count { it == TwoGrowFour } } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == twoAsByte } > 1 && changesHistory.contains(this) } @@ -105,11 +105,11 @@ object FourGrowTwo : Operation() { } object EightGrowOne : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(eightAsByte) && (TwoGrowFour.canConvert(numbers, changesHistory) || FourGrowTwo.canConvert(numbers, changesHistory)) } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == oneAsByte } > 1 && changesHistory.contains(this) } @@ -137,10 +137,10 @@ object EightGrowOne : Operation() { } object OneGrowEight : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.count { it == oneAsByte} > 1 } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(eightAsByte) && changesHistory.contains(this) } @@ -154,10 +154,10 @@ object OneGrowEight : Operation() { } object SixGrowSeven : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sixAsByte) } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sevenAsByte) && changesHistory.contains(this) && !changesHistory.containsSimpleGrows } @@ -178,11 +178,11 @@ object SixGrowSeven : Operation() { } object SevenGrowSix : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sevenAsByte) && !changesHistory.containsSimpleGrows } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return numbers.contains(sixAsByte) && changesHistory.contains(this) } @@ -196,7 +196,7 @@ object SevenGrowSix : Operation() { } object FiveGrowNine : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && ( numbers.count { @@ -205,7 +205,7 @@ object FiveGrowNine : Operation() { ) > changesHistory.count { it == this } } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return changesHistory.contains(this) } @@ -219,12 +219,12 @@ object FiveGrowNine : Operation() { } object NineGrowFive : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && (numbers.count { it == nineAsByte } - changesHistory.count { it == FiveGrowNine }) > changesHistory.count { it == this } } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return changesHistory.contains(this) } @@ -240,12 +240,12 @@ object NineGrowFive : Operation() { } private class GrowCustom(private val number: Byte) : Operation() { - override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null } - override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + override suspend fun canInvert(numbers: List, changesHistory: List): Boolean { return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null && numbers.contains(number) }