Bugfix and improve Comparator

feature/top10
Aurelien Hubert 7 years ago
parent 80e2dacb86
commit 874b539a5d
  1. 140
      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<QueryCondition>
get() {
return when (this) {
MONTH_OF_YEAR -> List(12) { index-> QueryCondition.MONTH().apply { setMonth(index+1) } }
DAY_OF_WEEK -> listOf()
YEAR -> {
val years = arrayListOf<QueryCondition.YEAR>()
val calendar = Calendar.getInstance()
calendar.time = Date()
val yearNow = calendar.get(Calendar.YEAR)
val realm = Realm.getDefaultInstance()
realm.where<Session>().sort("year", Sort.ASCENDING).findFirst()?.year?.let {
List(yearNow - it ) { index-> QueryCondition.YEAR().apply { setYear(yearNow - index) } }
}
realm.close()
years
/*
val years = arrayListOf<QueryCondition.YEAR>()
val realm = Realm.getDefaultInstance()
val distinctYears = realm.where<Session>().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<QueryCondition>
get() {
return when (this) {
MONTH_OF_YEAR -> List(12) { index -> QueryCondition.MONTH().apply { setMonth(index) } }
DAY_OF_WEEK -> listOf()
YEAR -> {
val years = arrayListOf<QueryCondition.YEAR>()
val calendar = Calendar.getInstance()
calendar.time = Date()
val yearNow = calendar.get(Calendar.YEAR)
val realm = Realm.getDefaultInstance()
realm.where<Session>().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<Comparator>.combined(): List<List<QueryCondition>> {
val comparatorList = ArrayList<List<QueryCondition>>()
this.forEach {
comparatorList.add(it.queryConditions)
}
val comparatorList = ArrayList<List<QueryCondition>>()
this.forEach {
comparatorList.add(it.queryConditions)
}
return getCombinations(comparatorList)
}
inline fun <reified T : Filterable> List<QueryCondition>.query(realm: Realm): RealmQuery<T> {
var realmQuery = realm.where<T>()
this.forEach {
realmQuery = it.filter(realmQuery)
}
return realmQuery
var realmQuery = realm.where<T>()
this.forEach {
realmQuery = it.filter(realmQuery)
}
return realmQuery
}
fun <T> getCombinations(lists: List<List<T>>): List<List<T>> {
var combinations: MutableSet<List<T>> = LinkedHashSet()
var newCombinations: MutableSet<List<T>>
var combinations: MutableSet<List<T>> = LinkedHashSet()
var newCombinations: MutableSet<List<T>>
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<T>()
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<T>()
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<T>()
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<T>()
newList.addAll(first)
newList.add(second)
newCombinations.add(newList)
}
}
combinations = newCombinations
index++
}
index++
}
return combinations.toList()
return combinations.toList()
}
Loading…
Cancel
Save