public final class Predicates extends Object
PredicateBuilder
and Predicate
instances.
Special Attributes
The predicate factory methods accepting an attribute name support two special attributes:
"__key"
- instructs the predicate to act on the key associated
with an item.
"this"
- instructs the predicate to act on the value associated
with an item.
Attribute Paths
Dot notation may be used for attribute name to instruct the predicate to act on the attribute located at deeper
level of an item: given "fullName.firstName"
path the predicate will act on firstName
attribute
of the value fetched by fullName
attribute from the item itself. If any of the attributes along the path
can't be resolved, IllegalArgumentException
will be thrown. Reading of any attribute from null
will produce null
value.
Square brackets notation may be used to instruct the predicate to act on the array/collection element at the
specified index: given "names[0]"
path the predicate will act on the first item of the array/collection
fetched by names
attribute from the item. The index must be non-negative, otherwise
IllegalArgumentException
will be thrown. Reading from the index pointing beyond the end of the collection/array
will produce null
value.
Special any
keyword may be used to act on every array/collection element: given
"names[any].fullName.firstName"
path the predicate will act on firstName
attribute of the value
fetched by fullName
attribute from every array/collection element stored in the item itself under names
attribute.
Handling of null
The predicate factory methods can accept null
as a value to compare with or a pattern to match against
if and only if that is explicitly stated in the method documentation. In this case, the usual null
equality
logic applies: if null
is provided, the predicate passes an item if and only if the value stored under the
item attribute in question is also null
.
Special care must be taken while comparing with null
values stored inside items being filtered
through the predicates created by the following methods: greaterThan(java.lang.String, java.lang.Comparable)
, greaterEqual(java.lang.String, java.lang.Comparable)
, lessThan(java.lang.String, java.lang.Comparable)
,
lessEqual(java.lang.String, java.lang.Comparable)
, between(java.lang.String, java.lang.Comparable, java.lang.Comparable)
. The predicates produced by these methods intentionally violate Comparable
contract by not throwing NullPointerException
for null
values. Instead, they always evaluate to
false
and therefore never pass such items.
Implicit Type Conversion
If the type of the stored value doesn't match the type of the value provided to the predicate, implicit type conversion
is performed before predicate evaluation. The provided value is converted to match the type of the stored attribute value.
If no conversion matching the type exists, IllegalArgumentException
is thrown.
Depending on the attribute type following conversions may apply:
Numeric types
Integer.parseInt(String)
and analogous methods of other types do. Strings containing no meaningful
representation will produce NumberFormatException
.
Boolean
type
"true"
is converted to true
, all other strings are
converted to false
.
true
, the zero is converted to false
.
Object.toString()
is invoked to produce the string representation for non-string values.
Character
type
IllegalArgumentException
.
Object.toString()
, which is interpreted as a
case-sensitive enum member name.
BigInteger
type
BigInteger
by applying widening or narrowing
conversion as described in JLS 5.1.2 Widening Primitive Conversions and 5.1.3 Narrowing Primitive Conversions.
true
and false
are converted to BigInteger.ONE
and BigInteger.ZERO
respectively.
Object.toString()
, which is interpreted as
a base 10 representation in the same way as BigInteger(String)
does.
If the representation is invalid, NumberFormatException
will be thrown.
BigDecimal
type
BigDecimal
by applying widening conversion as
described in JLS 5.1.2 Widening Primitive Conversions.
true
and false
are converted to BigDecimal.ONE
and BigDecimal.ZERO
respectively.
Object.toString()
, which is interpreted as
a base 10 representation in the same way as BigDecimal(String)
does.
If the representation is invalid, NumberFormatException
will be thrown.
SQL Timestamp
type
SQL Timestamp
by applying Date.getTime()
on them.
Timestamp.valueOf(String)
does.
RuntimeException
wrapping ParseException
is thrown for invalid representations.
SQL Date
type
Date.valueOf(String)
does.
RuntimeException
wrapping ParseException
is thrown for invalid representations.
Date
type
US
locale in the same way as SimpleDateFormat
does.
RuntimeException
wrapping ParseException
is thrown for invalid representations.
UUID
type
UUID.fromString(String)
does.
IllegalArgumentException
is thrown for invalid representations.
Modifier and Type | Method and Description |
---|---|
static <K,V> Predicate<K,V> |
alwaysFalse()
Creates an always false predicate that will filter out all items.
|
static <K,V> Predicate<K,V> |
alwaysTrue()
Creates an always true predicate that will pass all items.
|
static <K,V> Predicate<K,V> |
and(Predicate... predicates)
Creates an and predicate that will perform the logical and operation on the given
predicates . |
static <K,V> Predicate<K,V> |
between(String attribute,
Comparable from,
Comparable to)
Creates a between predicate that will pass items if the value stored under the given item
attribute
is contained inside the given range. |
static <K,V> Predicate<K,V> |
equal(String attribute,
Comparable value)
Creates an equal predicate that will pass items if the given
value and the value stored under
the given item attribute are equal. |
static <K,V> Predicate<K,V> |
greaterEqual(String attribute,
Comparable value)
Creates a greater than or equal to predicate that will pass items if the value stored under the given
item
attribute is greater than or equal to the given value . |
static <K,V> Predicate<K,V> |
greaterThan(String attribute,
Comparable value)
Creates a greater than predicate that will pass items if the value stored under the given
item
attribute is greater than the given value . |
static <K,V> Predicate<K,V> |
ilike(String attribute,
String pattern)
Creates a case-insensitive like predicate that will pass items if the given
pattern matches the value
stored under the given item attribute in a case-insensitive manner. |
static <K,V> Predicate<K,V> |
in(String attribute,
Comparable... values)
Creates a in predicate that will pass items if the value stored under the given item
attribute
is a member of the given values set. |
static <K,V> Predicate<K,V> |
instanceOf(Class klass)
Creates an instance of predicate that will pass entries for which
the value class is an
instanceof the given klass . |
static <K,V> Predicate<K,V> |
lessEqual(String attribute,
Comparable value)
Creates a less than or equal to predicate that will pass items if the value stored under the given
item
attribute is less than or equal to the given value . |
static <K,V> Predicate<K,V> |
lessThan(String attribute,
Comparable value)
Creates a less than predicate that will pass items if the value stored under the given item
attribute
is less than the given value . |
static <K,V> Predicate<K,V> |
like(String attribute,
String pattern)
Creates a like predicate that will pass items if the given
pattern matches the value stored under
the given item attribute . |
static <K,V> PartitionPredicate<K,V> |
multiPartitionPredicate(Set<? extends Object> partitionKeys,
Predicate<K,V> target)
Creates a new partition predicate that restricts the execution of the target predicate to a subset of partitions.
|
static PredicateBuilder |
newPredicateBuilder()
Creates a new instance of
PredicateBuilder . |
static <K,V> Predicate<K,V> |
not(Predicate predicate)
Creates a not predicate that will negate the result of the given
predicate . |
static <K,V> Predicate<K,V> |
notEqual(String attribute,
Comparable value)
Creates a not equal predicate that will pass items if the given
value and the value stored under
the given item attribute are not equal. |
static <K,V> Predicate<K,V> |
or(Predicate... predicates)
Creates an or predicate that will perform the logical or operation on the given
predicates . |
static <K,V> PagingPredicate<K,V> |
pagingPredicate(Comparator<Map.Entry<K,V>> comparator,
int pageSize)
Creates a paging predicate with a comparator and page size.
|
static <K,V> PagingPredicate<K,V> |
pagingPredicate(int pageSize)
Creates a paging predicate with a page size.
|
static <K,V> PagingPredicate<K,V> |
pagingPredicate(Predicate<K,V> predicate,
Comparator<Map.Entry<K,V>> comparator,
int pageSize)
Creates a paging predicate with an inner predicate, comparator and page size.
|
static <K,V> PagingPredicate<K,V> |
pagingPredicate(Predicate predicate,
int pageSize)
Creates a paging predicate with an inner predicate and page size.
|
static <K,V> PartitionPredicate<K,V> |
partitionPredicate(Object partitionKey,
Predicate<K,V> target)
Creates a new partition predicate that restricts the execution of the target predicate to a single partition.
|
static <K,V> Predicate<K,V> |
regex(String attribute,
String pattern)
Creates a regex predicate that will pass items if the given
pattern matches the value stored under
the given item attribute . |
static <K,V> Predicate<K,V> |
sql(String expression)
Creates a predicate that will pass items that match the given SQL 'where' expression.
|
public static PredicateBuilder newPredicateBuilder()
PredicateBuilder
.PredicateBuilder
instance.public static <K,V> Predicate<K,V> alwaysTrue()
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.public static <K,V> Predicate<K,V> alwaysFalse()
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.public static <K,V> Predicate<K,V> instanceOf(Class klass)
instanceof
the given klass
.K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.klass
- the class the created predicate will check for.public static <K,V> Predicate<K,V> and(Predicate... predicates)
predicates
.
If the given predicates
list is empty, the created predicate will always evaluate to true
and will pass any item.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.predicates
- the child predicates to form the resulting and predicate from.public static <K,V> Predicate<K,V> not(Predicate predicate)
predicate
.K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.predicate
- the predicate to negate the value of.public static <K,V> Predicate<K,V> or(Predicate... predicates)
predicates
.
If the given predicates
list is empty, the created predicate will always evaluate to false
and will never pass any items.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.predicates
- the child predicates to form the resulting or predicate from.public static <K,V> Predicate<K,V> notEqual(String attribute, Comparable value)
value
and the value stored under
the given item attribute
are not equal.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value for comparison from.value
- the value to compare the attribute value against. Can be null
.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> equal(String attribute, Comparable value)
value
and the value stored under
the given item attribute
are equal.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value for comparison from.value
- the value to compare the attribute value against. Can be null
.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> like(String attribute, String pattern)
pattern
matches the value stored under
the given item attribute
.
See also Special Attributes, Attribute Paths and Handling of null
sections of
Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value for matching from.pattern
- the pattern to match the attribute value against. The % (percentage sign) is a placeholder for
multiple characters, the _ (underscore) is a placeholder for a single character. If you need to
match the percentage sign or the underscore character itself, escape it with the backslash,
for example "\\%"
string will match the percentage sign. Can be null
.IllegalArgumentException
- if the attribute
does not exist.ilike(String, String)
,
regex(String, String)
public static <K,V> Predicate<K,V> ilike(String attribute, String pattern)
pattern
matches the value
stored under the given item attribute
in a case-insensitive manner.
See also Special Attributes, Attribute Paths and Handling of null
sections of
Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value for matching from.pattern
- the pattern to match the attribute value against. The % (percentage sign) is a placeholder for
multiple characters, the _ (underscore) is a placeholder for a single character. If you need to
match the percentage sign or the underscore character itself, escape it with the backslash,
for example "\\%"
string will match the percentage sign. Can be null
.IllegalArgumentException
- if the attribute
does not exist.like(String, String)
,
regex(String, String)
public static <K,V> Predicate<K,V> regex(String attribute, String pattern)
pattern
matches the value stored under
the given item attribute
.
See also Special Attributes, Attribute Paths and Handling of null
sections of
Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value for matching from.pattern
- the pattern to match the attribute value against. The pattern interpreted exactly the same as
described in Pattern
. Can be null
.IllegalArgumentException
- if the attribute
does not exist.like(String, String)
,
ilike(String, String)
public static <K,V> Predicate<K,V> greaterThan(String attribute, Comparable value)
attribute
is greater than the given value
.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the left-hand side attribute to fetch the value for comparison from.value
- the right-hand side value to compare the attribute value against.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> greaterEqual(String attribute, Comparable value)
attribute
is greater than or equal to the given value
.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the left-hand side attribute to fetch the value for comparison from.value
- the right-hand side value to compare the attribute value against.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> lessThan(String attribute, Comparable value)
attribute
is less than the given value
.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the left-hand side attribute to fetch the value for comparison from.value
- the right-hand side value to compare the attribute value against.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> lessEqual(String attribute, Comparable value)
attribute
is less than or equal to the given value
.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the left-hand side attribute to fetch the value for comparison from.value
- the right-hand side value to compare the attribute value against.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> between(String attribute, Comparable from, Comparable to)
attribute
is contained inside the given range. The range begins at the given from
bound and ends at
the given to
bound. The bounds are inclusive.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value to check from.from
- the inclusive lower bound of the range to check.to
- the inclusive upper bound of the range to check.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> in(String attribute, Comparable... values)
attribute
is a member of the given values
set.
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.attribute
- the attribute to fetch the value to test from.values
- the values set to test the membership in. Individual values can be null
.IllegalArgumentException
- if the attribute
does not exist.public static <K,V> Predicate<K,V> sql(String expression)
=
, <
, >
, <=
, >=
, ==
,
!=
, <>
, BETWEEN
, IN
, LIKE
, ILIKE
, REGEX
,
AND
, OR
and NOT
. The operators are case-insensitive, but attribute names are
case sensitive.
Example: active AND (age > 20 OR salary < 60000)
See also Special Attributes, Attribute Paths, Handling of null
and
Implicit Type Conversion sections of Predicates
.
Differences to standard SQL:
field=10
evaluates to false
, if field
is null
, in standard SQL it evaluates to UNKNOWN
.
IS [NOT] NULL
is not supported, use =NULL
or <>NULL
IS [NOT] DISTINCT FROM
is not supported, but =
and <>
behave like it.
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.expression
- the 'where' expression.IllegalArgumentException
- if the SQL expression is invalid.public static <K,V> PagingPredicate<K,V> pagingPredicate(int pageSize)
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.pageSize
- page sizeIllegalArgumentException
- if pageSize is not greater than 0public static <K,V> PagingPredicate<K,V> pagingPredicate(Predicate predicate, int pageSize)
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.predicate
- the inner predicate through which results will be filteredpageSize
- the page sizeIllegalArgumentException
- if pageSize is not greater than 0IllegalArgumentException
- if inner predicate is also a paging predicatepublic static <K,V> PagingPredicate<K,V> pagingPredicate(Comparator<Map.Entry<K,V>> comparator, int pageSize)
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.comparator
- the comparator through which results will be orderedpageSize
- the page sizeIllegalArgumentException
- if pageSize is not greater than 0public static <K,V> PagingPredicate<K,V> pagingPredicate(Predicate<K,V> predicate, Comparator<Map.Entry<K,V>> comparator, int pageSize)
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.predicate
- the inner predicate through which results will be filteredcomparator
- the comparator through which results will be orderedpageSize
- the page sizeIllegalArgumentException
- if pageSize is not greater than 0IllegalArgumentException
- if inner predicate is also a PagingPredicate
public static <K,V> PartitionPredicate<K,V> partitionPredicate(Object partitionKey, Predicate<K,V> target)
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.partitionKey
- the partition keytarget
- the target Predicate
NullPointerException
- if partitionKey or target predicate are null
public static <K,V> PartitionPredicate<K,V> multiPartitionPredicate(Set<? extends Object> partitionKeys, Predicate<K,V> target)
K
- the type of keys the predicate operates on.V
- the type of values the predicate operates on.partitionKeys
- the partition keystarget
- the target Predicate
NullPointerException
- if partitionKeys or target predicate are null
IllegalArgumentException
- if partitionkeys is an empty setCopyright © 2022 Hazelcast, Inc.. All rights reserved.