commit
5a890c497a
@ -0,0 +1,73 @@ |
|||||||
|
package net.pokeranalytics.android.model.comparison |
||||||
|
|
||||||
|
import io.realm.Realm |
||||||
|
import io.realm.RealmQuery |
||||||
|
import io.realm.kotlin.where |
||||||
|
import net.pokeranalytics.android.exceptions.PokerAnalyticsException |
||||||
|
import net.pokeranalytics.android.model.filter.Filterable |
||||||
|
import net.pokeranalytics.android.model.filter.QueryCondition |
||||||
|
|
||||||
|
enum class Comparator { |
||||||
|
DAY_OF_WEEK, |
||||||
|
MONTH, |
||||||
|
BLIND, |
||||||
|
; |
||||||
|
|
||||||
|
val queryConditions: List<QueryCondition> |
||||||
|
get() { |
||||||
|
return when (this) { |
||||||
|
DAY_OF_WEEK -> listOf() |
||||||
|
else -> throw PokerAnalyticsException.QueryTypeUnhandled |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
fun List<Comparator>.combined(): List<List<QueryCondition>> { |
||||||
|
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 |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> getCombinations(lists: List<List<T>>): List<List<T>> { |
||||||
|
var combinations: MutableSet<List<T>> = HashSet() |
||||||
|
var newCombinations: MutableSet<List<T>> |
||||||
|
|
||||||
|
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 = HashSet() |
||||||
|
for (first in combinations) { |
||||||
|
for (second in nextList) { |
||||||
|
val newList = ArrayList<T>() |
||||||
|
newList.addAll(first) |
||||||
|
newList.add(second) |
||||||
|
newCombinations.add(newList) |
||||||
|
} |
||||||
|
} |
||||||
|
combinations = newCombinations |
||||||
|
|
||||||
|
index++ |
||||||
|
} |
||||||
|
|
||||||
|
return combinations.toList() |
||||||
|
} |
||||||
Loading…
Reference in new issue