From 874b539a5d05f6458538239977ad6c2cfdd4fffc Mon Sep 17 00:00:00 2001 From: Aurelien Hubert Date: Mon, 15 Apr 2019 19:29:51 +0200 Subject: [PATCH] Bugfix and improve Comparator --- .../android/model/comparison/Comparator.kt | 140 ++++++++---------- 1 file changed, 65 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/comparison/Comparator.kt b/app/src/main/java/net/pokeranalytics/android/model/comparison/Comparator.kt index 6ac15b43..ab2a614c 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/comparison/Comparator.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/comparison/Comparator.kt @@ -8,99 +8,89 @@ import net.pokeranalytics.android.exceptions.PokerAnalyticsException import net.pokeranalytics.android.model.filter.Filterable import net.pokeranalytics.android.model.filter.QueryCondition import net.pokeranalytics.android.model.realm.Session -import net.pokeranalytics.android.ui.fragment.CalendarFragment import java.util.* import kotlin.collections.ArrayList -import kotlin.collections.HashSet enum class Comparator { - DAY_OF_WEEK, - MONTH_OF_YEAR, - MONTH, - YEAR, - BLIND, - ; + DAY_OF_WEEK, + MONTH_OF_YEAR, + MONTH, + YEAR, + BLIND, + ; - val queryConditions: List - get() { - return when (this) { - MONTH_OF_YEAR -> List(12) { index-> QueryCondition.MONTH().apply { setMonth(index+1) } } - DAY_OF_WEEK -> listOf() - YEAR -> { - val years = arrayListOf() - val calendar = Calendar.getInstance() - calendar.time = Date() - val yearNow = calendar.get(Calendar.YEAR) - val realm = Realm.getDefaultInstance() - realm.where().sort("year", Sort.ASCENDING).findFirst()?.year?.let { - List(yearNow - it ) { index-> QueryCondition.YEAR().apply { setYear(yearNow - index) } } - } - realm.close() - years - /* - val years = arrayListOf() - val realm = Realm.getDefaultInstance() - val distinctYears = realm.where().distinct("year").findAll().sort("year", Sort.DESCENDING) - distinctYears.forEach { session -> - session.year?.let { year -> - years.add(QueryCondition.YEAR().apply { setYear(year) }) - } - } - realm.close() - years - */ - } - else -> throw PokerAnalyticsException.QueryTypeUnhandled - } - } + val queryConditions: List + get() { + return when (this) { + MONTH_OF_YEAR -> List(12) { index -> QueryCondition.MONTH().apply { setMonth(index) } } + DAY_OF_WEEK -> listOf() + YEAR -> { + val years = arrayListOf() + val calendar = Calendar.getInstance() + calendar.time = Date() + val yearNow = calendar.get(Calendar.YEAR) + val realm = Realm.getDefaultInstance() + realm.where().sort("year", Sort.ASCENDING).findFirst()?.year?.let { + for (index in 0..(yearNow - it)) { + years.add(QueryCondition.YEAR().apply { setYear(yearNow - index) }) + } + } + realm.close() + years + } + else -> throw PokerAnalyticsException.QueryTypeUnhandled + } + } } fun List.combined(): List> { - val comparatorList = ArrayList>() - this.forEach { - comparatorList.add(it.queryConditions) - } + val comparatorList = ArrayList>() + this.forEach { + comparatorList.add(it.queryConditions) + } return getCombinations(comparatorList) } inline fun List.query(realm: Realm): RealmQuery { - var realmQuery = realm.where() - this.forEach { - realmQuery = it.filter(realmQuery) - } - return realmQuery + var realmQuery = realm.where() + this.forEach { + realmQuery = it.filter(realmQuery) + } + return realmQuery } fun getCombinations(lists: List>): List> { - var combinations: MutableSet> = LinkedHashSet() - var newCombinations: MutableSet> + var combinations: MutableSet> = LinkedHashSet() + var newCombinations: MutableSet> - var index = 0 + var index = 0 - // extract each of the integers in the first list - // and add each to ints as a new list - for (i in lists[0]) { - val newList = ArrayList() - newList.add(i) - combinations.add(newList) - } - index++ - while (index < lists.size) { - val nextList = lists[index] - newCombinations = LinkedHashSet() - for (first in combinations) { - for (second in nextList) { - val newList = ArrayList() - newList.addAll(first) - newList.add(second) - newCombinations.add(newList) - } - } - combinations = newCombinations + // extract each of the integers in the first list + // and add each to ints as a new list + if (lists.isNotEmpty()) { + for (i in lists[0]) { + val newList = ArrayList() + newList.add(i) + combinations.add(newList) + } + index++ + } + while (index < lists.size) { + val nextList = lists[index] + newCombinations = LinkedHashSet() + for (first in combinations) { + for (second in nextList) { + val newList = ArrayList() + newList.addAll(first) + newList.add(second) + newCombinations.add(newList) + } + } + combinations = newCombinations - index++ - } + index++ + } - return combinations.toList() + return combinations.toList() } \ No newline at end of file