Class Filter
-
- All Implemented Interfaces:
public final class FilterThis class provides filtering functionality on collections with a query string parameter
filter. The filter syntax is inspired by MongoDB, made up of predicates containing a property name, an operator, and a value.Syntax Example:
filter=name$eq:Joe- This matches all persons with the value "Joe" in the 'name' property.Predicates can be chained with logical operators AND, OR, and NOT (to negate a predicate).
Chaining Example:
filter=name$eq:Joe$and:city$like:*port$and:country$ne:NorwayThis matches all persons named Joe, where the city includes 'port' and the country is not Norway.String filtering is case-insensitive.
Operator affinity can be controlled using parentheses.
Affinity Example:
filter=name$eq:Joe$and:$not:(city$like:*port$or:age$lt:40)This matches all persons named Joe where the city does not include 'port' or age is less than 40.All filter strings should be URL encoded.
The class supports the following filter operators:
Equals (
$eq:)Not equals (
$ne:)Greater than (
$gt:)Greater than or equal (
$gte:)Less than (
$lt:)Less than or equal (
$lte:)Is NULL (
$null:)Is NOT NULL (
$nnull:)Substring match (
$like:)Logical AND (
$and:)Logical OR (
$or:)Logical NOT (
$not:)In a list (
$in:)Not in a list (
$nin:)Having a relational condition (
$having:(<relationship-field><operator><value>))Having a relational function condition (
$having:FUNCTION(<field.subfield>)<operator><value>) where FUNCTION is one of COUNT, SUM, AVG, MIN, MAX
The
$having:(without a function) operator is used to filter by a condition on a one-to-many or many-to-many relationship. Example: On the /departments/ endpoint: filter=$having:employees(name$eq:John) - matches all departments with an employee named John.The
$having:FUNCTIONoperator is used to filter by a conditional function on a one-to-many or many-to-many relationship. Example 1: On the /departments/ endpoint: filter=$having:COUNT(employees)$gt:10 - matches all departments with more than 10 employees. Example 2: On the /departments/ endpoint: filter=$having:AVG(employees.salary)$gt:500000 - matches all departments where the average salary is greater than 100.000Note that the
$having:operator does <i>not</i> support nesting - it is only intended to be used on the top level of a filter.The
$like:operator supports wildcards for multi-character (*) and single-character (?) matches. Note that string comparisons are (normally) done lowercase, depending on the underlying database support.Example:
filter=name$like:K*so?- matches all resources with a name starting with "K", then s sequence of random characters, ending with "so" followed by any single character.If no wildcard is present, the string will be prefixed and suffixed with a (*), effectively turning it into a "contains" expression.
Null values can be filtered using
$null:and$nnull:.Time-related values support various UTC-based formats for filtering. Specific time fields can be extracted for comparison.
The
$in:and$nin:operators are used to filter by a list of values, which must be enclosed in brackets.Examples of time field filtering:
filter=birth_date$eq:01-25matches all resources with a birthdate on January 25th.filter=birth_date$gte:1995--matches all resources born in or after 1995.filter=last_login$gte:12:15:00matches all resources who last logged in after 12:15:00.filter=last_login$gte:2023-01-01T00:00:00Zmatches all resources who last logged in after 2023-01-01T00:00:00Z.filter=last_login$gte:2023-11-02T15:22:45.123+0200matches all resources who last logged in after 2023-11-02T15:22:45.123+0200.filter=last_login$gte:2023-11-02T15:22[America/New_York]matches all resources who last logged in after 2023-11-02T15:22 in the America/New_York timezone.
Special characters are escaped with a dollar sign ($). Special characters include:
The escape char itself: '$'
Asterisk: '*'
Question mark: '?'
Left parenthesis: '('
Right parenthesis: ')'
Comma: ','
Left bracket: '['
Right bracket: ']'
Colon: ':'
Hyphen: '-'
Space: ' '
Note that after escpaping is resolved, literal wildcards () and (?) are escaped with () and (?), respectively.
Example:
filter=some_attribute$like:K$*soL$?p?becomesfilter=name$like:K**soL??p?This is done to prepare the string for LIKE or language specific regular expressions in the target query language.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumFilter.Operatorpublic classFilter.ValueRepresents a sealed class
Valuethat can hold different types of values.The sealed class
Valuehas the following subclasses:StringValuefor storing aStringvalue.LongValuefor storing aLongvalue.DoubleValuefor storing aDoublevalue.BooleanValuefor storing aBooleanvalue.DateValuefor storing aLocalDatevalue.TimeValuefor storing aLocalTimevalue.DateTimeValuefor storing aLocalDateTimevalue.MonthDayValuefor storing aMonthDayvalue.YearValuefor storing anIntvalue.UtcDateTimeValuefor storing anInstantvalue.
Each subclass extends the
Valueclass and overrides thevalueproperty with the respective type.public classFilter.ConditionRepresents a condition used in filtering data.
public classFilter.Companion
-
Field Summary
Fields Modifier and Type Field Description public final static StringQUERY_PARAMpublic final static CharacterESCAPE_CHARpublic final static CharacterSINGLE_WILDCARD_CHARpublic final static CharacterMULTI_WILDCARD_CHARpublic final static StringESCAPED_SINGLE_WILDCARD_SEQUENCEpublic final static StringESCAPED_MULTI_WILDCARD_SEQUENCEprivate final Filter.ConditionrootConditionpublic final static Filter.CompanionCompanion
-
Method Summary
Modifier and Type Method Description final Filter.ConditiongetRootCondition()final Unitaccept(FilterVisitor visitor)Accepts a visitor to perform operations on the root condition of the Filter. final StringcompactPrint()Executes the compactPrintoperation, which generates a compact string representation of aFilterobject based on the providedFilterPrintVisitor.final StringprettyPrint()Returns a formatted string representation of the Filter object. final StringasString()Returns the string representation of the Filterobject.final StringtoHttpParameter()Converts the given value to an HTTP query parameter string. StringtoString()-
-
Method Detail
-
getRootCondition
final Filter.Condition getRootCondition()
-
accept
final Unit accept(FilterVisitor visitor)
Accepts a visitor to perform operations on the root condition of the Filter.
- Parameters:
visitor- The visitor that will perform operations on the root condition.
-
compactPrint
final String compactPrint()
Executes the
compactPrintoperation, which generates a compact string representation of aFilterobject based on the providedFilterPrintVisitor.- Returns:
the compact string representation of the
Filterobject.
-
prettyPrint
final String prettyPrint()
Returns a formatted string representation of the Filter object.
- Returns:
the pretty-printed string representation of the Filter object
-
asString
final String asString()
Returns the string representation of the
Filterobject.This method internally calls the
compactPrint()method to get the compact string representation of theFilter.- Returns:
the string representation of the
Filterobject
-
toHttpParameter
final String toHttpParameter()
Converts the given value to an HTTP query parameter string.
- Returns:
the HTTP query parameter string
-
-
-
-