Skip to content

Commit

Permalink
Better toString from condition and modification
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Aug 13, 2024
1 parent dcd0476 commit e7d7ed3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ sealed class Condition<T> {

@Suppress("UNCHECKED_CAST")
override fun equals(other: Any?): Boolean = (other as? Condition.Never<T>) != null

override fun toString(): String = "false"
}

@Serializable(ConditionAlwaysSerializer::class)
Expand All @@ -34,84 +36,99 @@ sealed class Condition<T> {

@Suppress("UNCHECKED_CAST")
override fun equals(other: Any?): Boolean = (other as? Condition.Always<T>) != null

override fun toString(): String = "true"
}

@Serializable(ConditionAndSerializer::class)
@SerialName("And")
data class And<T>(val conditions: List<Condition<T>>) : Condition<T>() {
override fun invoke(on: T): Boolean = conditions.all { it(on) }
override fun toString(): String = conditions.joinToString(" && ", "(", ")")
}

@Serializable(ConditionOrSerializer::class)
@SerialName("Or")
data class Or<T>(val conditions: List<Condition<T>>) : Condition<T>() {
override fun invoke(on: T): Boolean = conditions.any { it(on) }
override fun toString(): String = conditions.joinToString(" || ", "(", ")")
}

@Serializable(ConditionNotSerializer::class)
@SerialName("Not")
data class Not<T>(val condition: Condition<T>) : Condition<T>() {
override fun invoke(on: T): Boolean = !condition(on)
override fun toString(): String = "!($condition)"
}

@Serializable(ConditionEqualSerializer::class)
@SerialName("Equal")
data class Equal<T>(val value: T) : Condition<T>() {
override fun invoke(on: T): Boolean = on == value
override fun toString(): String = " == $value"
}

@Serializable(ConditionNotEqualSerializer::class)
@SerialName("NotEqual")
data class NotEqual<T>(val value: T) : Condition<T>() {
override fun invoke(on: T): Boolean = on != value
override fun toString(): String = " != $value"
}

@Serializable(ConditionInsideSerializer::class)
@SerialName("Inside")
data class Inside<T>(val values: List<T>) : Condition<T>() {
override fun invoke(on: T): Boolean = values.contains(on)
override fun toString(): String = " in $values"
}

@Serializable(ConditionNotInsideSerializer::class)
@SerialName("NotInside")
data class NotInside<T>(val values: List<T>) : Condition<T>() {
override fun invoke(on: T): Boolean = !values.contains(on)
override fun toString(): String = " !in $values"
}

@Serializable(ConditionGreaterThanSerializer::class)
@SerialName("GreaterThan")
data class GreaterThan<T: Comparable<T>>(val value: T) : Condition<T>() {
override fun invoke(on: T): Boolean = on > value
override fun toString(): String = " > $value"
}

@Serializable(ConditionLessThanSerializer::class)
@SerialName("LessThan")
data class LessThan<T: Comparable<T>>(val value: T) : Condition<T>() {
override fun invoke(on: T): Boolean = on < value
override fun toString(): String = " < $value"
}

@Serializable(ConditionGreaterThanOrEqualSerializer::class)
@SerialName("GreaterThanOrEqual")
data class GreaterThanOrEqual<T: Comparable<T>>(val value: T) : Condition<T>() {
override fun invoke(on: T): Boolean = on >= value
override fun toString(): String = " >= $value"
}

@Serializable(ConditionLessThanOrEqualSerializer::class)
@SerialName("LessThanOrEqual")
data class LessThanOrEqual<T: Comparable<T>>(val value: T) : Condition<T>() {
override fun invoke(on: T): Boolean = on <= value
override fun toString(): String = " <= $value"
}

@Serializable
@SerialName("StringContains")
data class StringContains(val value: String, val ignoreCase: Boolean = false) : Condition<String>() {
override fun invoke(on: String): Boolean = on.contains(value, ignoreCase)
override fun toString(): String = ".contains($value, $ignoreCase)"
}

@Serializable
@SerialName("GeoDistance")
data class GeoDistance(val value: GeoCoordinate, val greaterThanKilometers: Double = 0.0, val lessThanKilometers: Double = 100_000.0) : Condition<GeoCoordinate>() {
override fun invoke(on: GeoCoordinate): Boolean = on.distanceToKilometers(value) in greaterThanKilometers..lessThanKilometers
override fun toString(): String = ".distanceToKilometers(value) in $greaterThanKilometers..$lessThanKilometers"
}

@Serializable
Expand All @@ -122,6 +139,7 @@ sealed class Condition<T> {
val asText = on.toString()
return value.split(' ').all { asText.contains(it, ignoreCase) }
}
override fun toString(): String = ".fullTextSearch($value, $ignoreCase)"
}

@Serializable
Expand All @@ -130,76 +148,88 @@ sealed class Condition<T> {
@Transient
val regex = Regex(pattern, if (ignoreCase) setOf(RegexOption.IGNORE_CASE) else setOf())
override fun invoke(on: String): Boolean = regex.matches(on)
override fun toString(): String = ".contains(Regex($pattern), $ignoreCase)"
}

@Serializable(ConditionIntBitsClearSerializer::class)
@SerialName("IntBitsClear")
data class IntBitsClear(val mask: Int) : Condition<Int>() {
override fun invoke(on: Int): Boolean = on and mask == 0
override fun toString(): String = ".bitsClear(${mask.toString(16)})"
}

@Serializable(ConditionIntBitsSetSerializer::class)
@SerialName("IntBitsSet")
data class IntBitsSet(val mask: Int) : Condition<Int>() {
override fun invoke(on: Int): Boolean = on and mask == mask
override fun toString(): String = ".bitsSet(${mask.toString(16)})"
}

@Serializable(ConditionIntBitsAnyClearSerializer::class)
@SerialName("IntBitsAnyClear")
data class IntBitsAnyClear(val mask: Int) : Condition<Int>() {
override fun invoke(on: Int): Boolean = on and mask < mask
override fun toString(): String = ".bitsAnyClear(${mask.toString(16)})"
}

@Serializable(ConditionIntBitsAnySetSerializer::class)
@SerialName("IntBitsAnySet")
data class IntBitsAnySet(val mask: Int) : Condition<Int>() {
override fun invoke(on: Int): Boolean = on and mask > 0
override fun toString(): String = ".bitsAnySet(${mask.toString(16)})"
}

// TODO: Merge collection operations once Khrysalis is fully deprecated
@Serializable(ConditionListAllElementsSerializer::class)
@SerialName("ListAllElements")
data class ListAllElements<E>(val condition: Condition<E>) : Condition<List<E>>() {
override fun invoke(on: List<E>): Boolean = on.all { condition(it) }
override fun toString(): String = ".all { it$condition }"
}

@Serializable(ConditionListAnyElementsSerializer::class)
@SerialName("ListAnyElements")
data class ListAnyElements<E>(val condition: Condition<E>) : Condition<List<E>>() {
override fun invoke(on: List<E>): Boolean = on.any { condition(it) }
override fun toString(): String = ".any { it$condition }"
}

// TODO: Change to empty check
@Serializable(ConditionListSizesEqualsSerializer::class)
@SerialName("ListSizesEquals")
data class ListSizesEquals<E>(val count: Int) : Condition<List<E>>() {
override fun invoke(on: List<E>): Boolean = on.size == count
override fun toString(): String = ".size == $count"
}

@Serializable(ConditionSetAllElementsSerializer::class)
@SerialName("SetAllElements")
data class SetAllElements<E>(val condition: Condition<E>) : Condition<Set<E>>() {
override fun invoke(on: Set<E>): Boolean = on.all { condition(it) }
override fun toString(): String = ".all { it$condition }"
}

@Serializable(ConditionSetAnyElementsSerializer::class)
@SerialName("SetAnyElements")
data class SetAnyElements<E>(val condition: Condition<E>) : Condition<Set<E>>() {
override fun invoke(on: Set<E>): Boolean = on.any { condition(it) }
override fun toString(): String = ".any { it$condition }"
}

// TODO: Change to empty check
@Serializable(ConditionSetSizesEqualsSerializer::class)
@SerialName("SetSizesEquals")
data class SetSizesEquals<E>(val count: Int) : Condition<Set<E>>() {
override fun invoke(on: Set<E>): Boolean = on.size == count
override fun toString(): String = ".size == $count"
}

// TODO: Allow alternate keys once Khrysalis is fully deprecated
@Serializable(ConditionExistsSerializer::class)
@SerialName("Exists")
data class Exists<V>(val key: String) : Condition<Map<String, V>>() {
override fun invoke(on: Map<String, V>): Boolean = on.containsKey(key)
override fun toString(): String = ".containsKey($key)"
}

@Serializable
Expand All @@ -208,18 +238,26 @@ sealed class Condition<T> {
Condition<Map<String, V>>() {
@Suppress("UNCHECKED_CAST")
override fun invoke(on: Map<String, V>): Boolean = on.containsKey(key) && condition(on[key] as V)
override fun toString(): String = "[$key]$condition"
}

data class OnField<K, V>(
val key: SerializableProperty<in K, V>,
val condition: Condition<V>,
) : Condition<K>() {
override fun invoke(on: K): Boolean = condition(key.get(on))
override fun toString(): String {
return if(condition is Condition.OnField<*, *>)
"${key.name}.$condition"
else
"${key.name}$condition"
}
}

@Serializable(ConditionIfNotNullSerializer::class)
@SerialName("IfNotNull")
data class IfNotNull<T>(val condition: Condition<T>) : Condition<T?>() {
override fun invoke(on: T?): Boolean = on != null && condition(on)
override fun toString(): String = " != null && $condition"
}
}
Loading

0 comments on commit e7d7ed3

Please sign in to comment.