|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-core-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.getstream.android.core.api.filter |
| 17 | + |
| 18 | +import io.getstream.android.core.internal.filter.FilterOperator |
| 19 | + |
| 20 | +/** Utility class for building filters. */ |
| 21 | +public object Filters { |
| 22 | + /** |
| 23 | + * Creates a filter that combines multiple filters with a logical AND operation. |
| 24 | + * |
| 25 | + * @param filters The filters to combine. |
| 26 | + * @return A filter that matches when all provided filters match. |
| 27 | + */ |
| 28 | + public fun <F : FilterField> and(vararg filters: Filter<F>): Filter<F> = |
| 29 | + CollectionOperationFilter(FilterOperator.AND, filters.toSet()) |
| 30 | + |
| 31 | + /** |
| 32 | + * Creates a filter that combines multiple filters with a logical OR operation. |
| 33 | + * |
| 34 | + * @param filters The filters to combine. |
| 35 | + * @return A filter that matches when any of the specified filters match. |
| 36 | + */ |
| 37 | + public fun <F : FilterField> or(vararg filters: Filter<F>): Filter<F> = |
| 38 | + CollectionOperationFilter(FilterOperator.OR, filters.toSet()) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Creates a filter that checks if this field equals a specific value. |
| 43 | + * |
| 44 | + * @param value The value to check equality against. |
| 45 | + * @return A filter that matches when this field equals the specified value. |
| 46 | + */ |
| 47 | +public fun <F : FilterField> F.equal(value: Any): Filter<F> = |
| 48 | + BinaryOperationFilter(FilterOperator.EQUAL, this, value) |
| 49 | + |
| 50 | +/** |
| 51 | + * Creates a filter that checks if this field is greater than a specific value. |
| 52 | + * |
| 53 | + * @param value The value to check against. |
| 54 | + * @return A filter that matches when this field is greater than the specified value. |
| 55 | + */ |
| 56 | +public fun <F : FilterField> F.greater(value: Any): Filter<F> = |
| 57 | + BinaryOperationFilter(FilterOperator.GREATER, this, value) |
| 58 | + |
| 59 | +/** |
| 60 | + * Creates a filter that checks if this field is greater than or equal to a specific value. |
| 61 | + * |
| 62 | + * @param value The value to check against. |
| 63 | + * @return A filter that matches when this field is greater than or equal to the specified value. |
| 64 | + */ |
| 65 | +public fun <F : FilterField> F.greaterOrEqual(value: Any): Filter<F> = |
| 66 | + BinaryOperationFilter(FilterOperator.GREATER_OR_EQUAL, this, value) |
| 67 | + |
| 68 | +/** |
| 69 | + * Creates a filter that checks if this field is less than a specific value. |
| 70 | + * |
| 71 | + * @param value The value to check against. |
| 72 | + * @return A filter that matches when this field is less than the specified value. |
| 73 | + */ |
| 74 | +public fun <F : FilterField> F.less(value: Any): Filter<F> = |
| 75 | + BinaryOperationFilter(FilterOperator.LESS, this, value) |
| 76 | + |
| 77 | +/** |
| 78 | + * Creates a filter that checks if this field is less than or equal to a specific value. |
| 79 | + * |
| 80 | + * @param value The value to check against. |
| 81 | + * @return A filter that matches when this field is less than or equal to the specified value. |
| 82 | + */ |
| 83 | +public fun <F : FilterField> F.lessOrEqual(value: Any): Filter<F> = |
| 84 | + BinaryOperationFilter(FilterOperator.LESS_OR_EQUAL, this, value) |
| 85 | + |
| 86 | +/** |
| 87 | + * Creates a filter that checks if this field's value is in a specific list of values. |
| 88 | + * |
| 89 | + * @param values The list of values to check against. |
| 90 | + * @return A filter that matches when this field's value is in the specified list. |
| 91 | + */ |
| 92 | +public fun <F : FilterField> F.`in`(values: List<Any>): Filter<F> = |
| 93 | + BinaryOperationFilter(FilterOperator.IN, this, values.toSet()) |
| 94 | + |
| 95 | +/** |
| 96 | + * Creates a filter that checks if this field's value is in a specific set of values. |
| 97 | + * |
| 98 | + * @param values The values to check against. |
| 99 | + * @return A filter that matches when this field's value is in the specified values. |
| 100 | + */ |
| 101 | +public fun <F : FilterField> F.`in`(vararg values: Any): Filter<F> = |
| 102 | + BinaryOperationFilter(FilterOperator.IN, this, values.toSet()) |
| 103 | + |
| 104 | +/** |
| 105 | + * Creates a filter that performs a full-text query on this field. |
| 106 | + * |
| 107 | + * @param value The query string to search for. |
| 108 | + * @return A filter that matches based on the full-text query. |
| 109 | + */ |
| 110 | +public fun <F : FilterField> F.query(value: String): Filter<F> = |
| 111 | + BinaryOperationFilter(FilterOperator.QUERY, this, value) |
| 112 | + |
| 113 | +/** |
| 114 | + * Creates a filter that performs autocomplete matching on this field. |
| 115 | + * |
| 116 | + * @param value The string to autocomplete against. |
| 117 | + * @return A filter that matches based on autocomplete functionality. |
| 118 | + */ |
| 119 | +public fun <F : FilterField> F.autocomplete(value: String): Filter<F> = |
| 120 | + BinaryOperationFilter(FilterOperator.AUTOCOMPLETE, this, value) |
| 121 | + |
| 122 | +/** |
| 123 | + * Creates a filter that checks if this field exists. |
| 124 | + * |
| 125 | + * @return A filter that matches when this field exists. |
| 126 | + */ |
| 127 | +public fun <F : FilterField> F.exists(): Filter<F> = |
| 128 | + BinaryOperationFilter(FilterOperator.EXISTS, this, true) |
| 129 | + |
| 130 | +/** |
| 131 | + * Creates a filter that checks if this field does not exist. |
| 132 | + * |
| 133 | + * @return A filter that matches when this field does not exist. |
| 134 | + */ |
| 135 | +public fun <F : FilterField> F.doesNotExist(): Filter<F> = |
| 136 | + BinaryOperationFilter(FilterOperator.EXISTS, this, false) |
| 137 | + |
| 138 | +/** |
| 139 | + * Creates a filter that checks if this field contains a specific value. |
| 140 | + * |
| 141 | + * @param value The value to check for within this field. |
| 142 | + * @return A filter that matches when this field contains the specified value. |
| 143 | + */ |
| 144 | +public fun <F : FilterField> F.contains(value: Any): Filter<F> = |
| 145 | + BinaryOperationFilter(FilterOperator.CONTAINS, this, value) |
| 146 | + |
| 147 | +/** |
| 148 | + * Creates a filter that checks if a specific path exists within this field. |
| 149 | + * |
| 150 | + * @param value The path to check for existence. |
| 151 | + * @return A filter that matches when the specified path exists in this field. |
| 152 | + */ |
| 153 | +public fun <F : FilterField> F.pathExists(value: String): Filter<F> = |
| 154 | + BinaryOperationFilter(FilterOperator.PATH_EXISTS, this, value) |
0 commit comments