commit
564cafd5db
@ -0,0 +1,61 @@ |
||||
package net.pokeranalytics.android.model |
||||
|
||||
import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest |
||||
import net.pokeranalytics.android.components.RealmInstrumentedUnitTest |
||||
import net.pokeranalytics.android.model.filter.QueryCondition |
||||
import net.pokeranalytics.android.model.realm.Filter |
||||
import net.pokeranalytics.android.model.realm.FilterCondition |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterSectionRow |
||||
import org.junit.Assert |
||||
import org.junit.Test |
||||
|
||||
import org.junit.Assert.* |
||||
import java.util.* |
||||
|
||||
class CriteriaTest : BaseFilterInstrumentedUnitTest() { |
||||
|
||||
@Test |
||||
fun getQueryConditions() { |
||||
|
||||
val realm = this.mockRealm |
||||
realm.beginTransaction() |
||||
val cal = Calendar.getInstance() |
||||
cal.time = Date() |
||||
val s1 = Session.testInstance(100.0, false, cal.time) |
||||
cal.add(Calendar.YEAR, 1) |
||||
Session.testInstance(100.0, true, cal.time) |
||||
cal.add(Calendar.YEAR, -11) |
||||
val firstValue = cal.get(Calendar.YEAR) |
||||
Session.testInstance(100.0, true, cal.time) |
||||
cal.add(Calendar.YEAR, 7) |
||||
Session.testInstance(100.0, true, cal.time) |
||||
cal.add(Calendar.YEAR, -2) |
||||
Session.testInstance(100.0, true, cal.time) |
||||
cal.add(Calendar.YEAR, 10) |
||||
Session.testInstance(100.0, true, cal.time) |
||||
|
||||
val lastValue = firstValue + 10 |
||||
|
||||
realm.commitTransaction() |
||||
|
||||
val years = Criteria.Years.queryConditions as List<QueryCondition.AnyYear> |
||||
println("years = ${years.map { it.getDisplayName() }}") |
||||
|
||||
assertEquals(11, years.size) |
||||
assertEquals(firstValue, years.first().listOfValues.first()) |
||||
assertEquals(lastValue, years.last().listOfValues.first()) |
||||
} |
||||
|
||||
@Test |
||||
fun combined() { |
||||
|
||||
val critierias = listOf(Criteria.MonthsOfYear, Criteria.DaysOfWeek) |
||||
val combined = critierias.combined() |
||||
combined.forEach { |
||||
it.forEach {qc-> |
||||
println(qc.getDisplayName()) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -1,113 +0,0 @@ |
||||
package net.pokeranalytics.android.model.comparison |
||||
|
||||
import io.realm.Realm |
||||
import io.realm.Sort |
||||
import io.realm.kotlin.where |
||||
import net.pokeranalytics.android.exceptions.PokerAnalyticsException |
||||
import net.pokeranalytics.android.model.Criteria |
||||
import net.pokeranalytics.android.model.filter.QueryCondition |
||||
import net.pokeranalytics.android.model.realm.* |
||||
import java.util.* |
||||
import kotlin.collections.ArrayList |
||||
|
||||
enum class Comparator { |
||||
DAY_OF_WEEK, |
||||
MONTH_OF_YEAR, |
||||
YEAR, |
||||
BLIND, |
||||
CASH, |
||||
TOURNAMENT, |
||||
LIVE, |
||||
ONLINE, |
||||
GAME, |
||||
BANKROLL, |
||||
LOCATION, |
||||
TOURNAMENT_FEATURE, |
||||
TOURNAMENT_NAME, |
||||
DAY_PERIOD, |
||||
LIMIT_TYPE, |
||||
TABLE_SIZE, |
||||
TOURNAMENT_TYPE, |
||||
TOURNAMENT_ENTRY_FEE, |
||||
; |
||||
|
||||
val queryConditions: List<QueryCondition> |
||||
get() { |
||||
return when (this) { |
||||
TOURNAMENT_ENTRY_FEE -> { |
||||
val fees = arrayListOf<QueryCondition.BetweenTournamentFee>() |
||||
val realm = Realm.getDefaultInstance() |
||||
val fieldName = Session.fieldNameForQueryType(QueryCondition.BetweenTournamentFee::class.java)!! |
||||
realm.where<Session>().distinct(fieldName).findAll().sort(fieldName, Sort.ASCENDING).map { |
||||
it.tournamentEntryFee?.let { fee -> |
||||
fees.add(QueryCondition.BetweenTournamentFee().apply { between(fee, fee) }) |
||||
} |
||||
} |
||||
realm.close() |
||||
fees |
||||
} |
||||
MONTH_OF_YEAR -> Criteria.MonthsOfYear.queryConditions |
||||
DAY_OF_WEEK -> Criteria.DaysOfWeek.queryConditions |
||||
YEAR -> Criteria.Years.queryConditions |
||||
DAY_PERIOD -> Criteria.DayPeriods.queryConditions |
||||
CASH -> listOf(QueryCondition.IsCash) |
||||
TOURNAMENT -> listOf(QueryCondition.IsTournament) |
||||
LIVE -> listOf(QueryCondition.IsLive) |
||||
ONLINE -> listOf(QueryCondition.IsOnline) |
||||
BLIND -> Criteria.Blinds.queryConditions |
||||
BANKROLL -> Criteria.Bankrolls.queryConditions |
||||
GAME -> Criteria.Games.queryConditions |
||||
TOURNAMENT_NAME-> Criteria.TournamentNames.queryConditions |
||||
TOURNAMENT_FEATURE-> Criteria.TournamentFeatures.queryConditions |
||||
LOCATION-> Criteria.Locations.queryConditions |
||||
LIMIT_TYPE -> Criteria.Limits.queryConditions |
||||
TABLE_SIZE -> Criteria.TableSizes.queryConditions |
||||
TOURNAMENT_TYPE -> Criteria.TournamentTypes.queryConditions |
||||
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) |
||||
} |
||||
|
||||
fun <T> getCombinations(lists: List<List<T>>): List<List<T>> { |
||||
var combinations: MutableSet<List<T>> = LinkedHashSet() |
||||
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 |
||||
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++ |
||||
} |
||||
|
||||
return combinations.toList() |
||||
} |
||||
Loading…
Reference in new issue