porting QueryStringValue to sealed interface with a sub category for the content based values

- allows for handling those cases separately for normalisation
This commit is contained in:
Adam Brown 2021-10-27 12:19:49 +01:00
parent 54daee2798
commit 72508c61d9
2 changed files with 21 additions and 13 deletions

View File

@ -19,14 +19,17 @@ package org.matrix.android.sdk.api.query
/**
* Basic query language. All these cases are mutually exclusive.
*/
sealed class QueryStringValue {
object NoCondition : QueryStringValue()
object IsNull : QueryStringValue()
object IsNotNull : QueryStringValue()
object IsEmpty : QueryStringValue()
object IsNotEmpty : QueryStringValue()
data class Equals(val string: String, val case: Case = Case.SENSITIVE) : QueryStringValue()
data class Contains(val string: String, val case: Case = Case.SENSITIVE) : QueryStringValue()
sealed interface QueryStringValue {
sealed interface ContentQueryStringValue : QueryStringValue
object NoCondition : QueryStringValue
object IsNull : QueryStringValue
object IsNotNull : QueryStringValue
object IsEmpty : QueryStringValue
object IsNotEmpty : QueryStringValue
data class Equals(val string: String, val case: Case = Case.SENSITIVE) : ContentQueryStringValue
data class Contains(val string: String, val case: Case = Case.SENSITIVE) : ContentQueryStringValue
enum class Case {
SENSITIVE,

View File

@ -20,19 +20,24 @@ import io.realm.Case
import io.realm.RealmObject
import io.realm.RealmQuery
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.query.QueryStringValue.ContentQueryStringValue
import timber.log.Timber
fun <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: QueryStringValue): RealmQuery<T> {
when (queryStringValue) {
is QueryStringValue.NoCondition -> Timber.v("No condition to process")
return when (queryStringValue) {
is QueryStringValue.NoCondition -> {
Timber.v("No condition to process")
this
}
is QueryStringValue.IsNotNull -> isNotNull(field)
is QueryStringValue.IsNull -> isNull(field)
is QueryStringValue.IsEmpty -> isEmpty(field)
is QueryStringValue.IsNotEmpty -> isNotEmpty(field)
is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase())
is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase())
is ContentQueryStringValue -> when (queryStringValue) {
is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase())
is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase())
}
}
return this
}
private fun QueryStringValue.Case.toRealmCase(): Case {