PlayerAndroid/app/src/main/java/software/lavender/music/query/filters.kt

87 lines
2.0 KiB
Kotlin

package software.lavender.music.query
interface QueryFilter<T> {
fun matches(testee: T): Boolean
}
abstract class NumericFilter<T : Number>(
val gt: T?,
val lt: T?,
val eq: T?,
val ne: T?
) : QueryFilter<T>
class IntFilter(
gt: Int? = null,
lt: Int? = null,
eq: Int? = null,
ne: Int? = null
) : NumericFilter<Int>(gt, lt, eq, ne) {
override fun matches(testee: Int): Boolean {
if (eq != null) {
return testee == eq
}
if (lt != null && testee >= lt) {
return false
}
if (gt != null && testee <= gt) {
return false
}
if (ne != null && testee == ne) {
return false
}
return true
}
}
class LongFilter(
gt: Long? = null,
lt: Long? = null,
eq: Long? = null,
ne: Long? = null
) : NumericFilter<Long>(gt, lt, eq, ne) {
override fun matches(testee: Long): Boolean {
if (eq != null) {
return testee == eq
}
if (lt != null && testee >= lt) {
return false
}
if (gt != null && testee <= gt) {
return false
}
if (ne != null && testee == ne) {
return false
}
return true
}
}
class StringFilter(
val eq: String? = null,
val like: String? = null,
val ne: String? = null,
val _in: Array<String>? = null,
val isNull: Boolean = false,
val lower: Boolean = false,
) : QueryFilter<String?> {
override fun matches(testee: String?): Boolean {
if (testee == null) {
return isNull
}
val other = if (lower) testee.lowercase() else testee
if (eq != null) {
return other == eq
}
if (like != null) {
return other.like(like)
}
if (ne != null) {
return other != ne
}
if (_in != null) {
return _in.contains(other)
}
return true
}
}