Sweetener - Collection Restrictions - Part I
Search through the collection has always been a problem for developers and simple search enforced on the programmer writing many lines of code (Countless foreach, if-else constructions). Fortunately, class Collections
solves this problem. The integral part of this class is Criteria
and Restriction
mechanism, which allows for complex query conditions. The restrictions can be joined in chain by add method. Example criteria is shown below:
Criteria object created in this way can be used in filter
method on Collections
Many Restriction
classes have been prepared in order to allow a smooth search through the collection. Some of them are intended for numbers, some of them are intended for Collections / Arrays and there are restrictions intended for any objects:
- Restrictions for any objects
equals(String field, Object value)
Checks whether object is equals to object passed as second parameternotEquals(String field, Object value)
Checks whether object is not equals to object passed as second parameterisNull(String field)
Checks whether object is nullisNotNull(String field)
Checks whether object is not null
- Restrictions for String
equals(String field, Object value, boolean ignoreCase)
Checks whether object is equals to object passed as second parameter ignoring case sensitivitynotEquals(String field, Object value, boolean ignoreCase)
Checks whether object is not equals to object from second parameter ignoring case sensitivitylike(String field, String value)
Checks whether object contains substring passed as second parameternotLike(String field, String value)
Checks whether object does not contains substring passed as second parameter
- Restrictions for Number
greater(String field, Number value)
Check whether object is greater than the number passed as second parameterless(String field, Number value)
Check whether object is less than the number passed as second parametergreaterOrEquals(String field, Number value)
Check whether object is greater or equals to the number passed as second parameterlessOrEquals(String field, Number value)
Check whether object is less or equals to the number passed as second parameterbetween(String field, Number minValue, Number maxValue)
Check whether value is between the numbers passed as second and third parameternotBetween(String field, Number minValue, Number maxValue)
Check whether value is not between the numbers passed as second and third parameterbetween(String field, Number minValue, Number maxValue, boolean leftInclusive, boolean rightInclusive)
Check whether value is between the numbers passed as second and third parameter. leftInclusive/rightInclusive indicates whether point in left range/ right range should or shouldn’t be taken into accountnotBetween(String field, Number minValue, Number maxValue, boolean leftInclusive, boolean rightInclusive)
Check whether value is not between the numbers passed as second and third parameter. leftInclusive/rightInclusive indicates whether point in left range/ right range should or shouldn’t be taken into account
- Restrictions for Collections / Arrays
contains(String field, Object... values)
Checks whether object contains all objects passed as second parametercontainsAny(String field, Object... values)
Checks whether object contains any of objects passed as second parameternotContains(String field, Object... values)
Checks whether object does not contain all objects passed as second parameternotContainsAny(String field, Object... values)
Checks whether object does not contain any objects passed as second parameterin(String field, Object... values)
Checks whether value is equals to one of objects passed as second, third … parameternotIn(String field, Object... values)
Checks whether value is not equals to any of objects passed as second, third … parameter
A bit of practice
Assume that we have Person
object with the following properties
Let’s create lists of Person objects
Now, we can perform some searches by using filter methods on Collections object: