Class Filters<E>
- java.lang.Object
-
- org.assertj.core.api.filter.Filters<E>
-
- Type Parameters:
E
- the type of element of group to filter.
public class Filters<E> extends Object
Filters the elements of a given
or array according to the specified filter criteria.Iterable
Filter criteria can be expressed either by a
Condition
or a pseudo filter language on elements properties.With fluent filter language on element properties/fields :
WithassertThat(filter(players).with("pointsPerGame").greaterThan(20) .and("assistsPerGame").greaterThan(7).get()) .containsOnly(james, rose);
Condition
:List<Player> players = ...; Condition<Player> potentialMVP = new Condition<Player>("is a possible MVP"){ public boolean matches(Player player) { return player.getPointsPerGame() > 20 && player.getAssistsPerGame() > 7; }; }; // use filter static method to build Filters assertThat(filter(players).being(potentialMVP).get()).containsOnly(james, rose);
- Author:
- Joel Costigliola, Mikhail Mazursky
-
-
Field Summary
Fields Modifier and Type Field Description (package private) List<E>
filteredIterable
(package private) Iterable<E>
initialIterable
private static PropertyOrFieldSupport
PROPERTY_OR_FIELD_SUPPORT
private String
propertyOrFieldNameToFilterOn
The name of the property used for filtering.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Filters<E>
and(String propertyOrFieldName)
Alias ofwith(String)
for synthetic sugar to write things like :private Filters<E>
applyFilterCondition(Condition<? super E> condition)
Filters<E>
being(Condition<? super E> condition)
Filter the underlying group, keeping only elements satisfying the givenCondition
.
Same ashaving(Condition)
- pick the method you prefer to have the most readable code.private void
checkPropertyNameToFilterOnIsNotNull()
Filters<E>
equalsTo(Object propertyValue)
Filters the underlying iterable to keep object with property (specified bywith(String)
) equals to given value.static <E> Filters<E>
filter(E[] array)
Creates a new
with the array to filter.Filters
static <E> Filters<E>
filter(Iterable<E> iterable)
List<E>
get()
Returns the resulting filtered Iterable<E> (even if the constructor parameter type was an array).Filters<E>
having(Condition<? super E> condition)
Filter the underlying group, keeping only elements satisfying the givenCondition
.
Same asbeing(Condition)
- pick the method you prefer to have the most readable code.Filters<E>
in(Object... propertyValues)
Filters the underlying iterable to keep object with property (specified bywith(String)
) equals to one of the given values.private static boolean
isItemInArray(Object item, Object[] arrayOfValues)
Returnstrue
if given item is in given array,false
otherwise.Filters<E>
notEqualsTo(Object propertyValue)
Filters the underlying iterable to keep object with property (specified bywith(String)
) not equals to given value.Filters<E>
notIn(Object... propertyValues)
Filters the underlying iterable to keep object with property (specified bywith(String)
) not in the given values.private void
validatePropertyOrFieldName(String propertyOrFieldName)
Filters<E>
with(String propertyOrFieldName)
Sets the name of the property used for filtering, it may be a nested property like"address.street.name"
.Filters<E>
with(String propertyOrFieldName, Object propertyValue)
Filter the underlying group, keeping only elements with a property equals to given value.
-
-
-
Field Detail
-
PROPERTY_OR_FIELD_SUPPORT
private static final PropertyOrFieldSupport PROPERTY_OR_FIELD_SUPPORT
-
propertyOrFieldNameToFilterOn
private String propertyOrFieldNameToFilterOn
The name of the property used for filtering.
-
-
Method Detail
-
filter
public static <E> Filters<E> filter(Iterable<E> iterable)
Creates a new
with theFilters
Iterable
to filter.Chain this call to express filter criteria either by a
Condition
or a pseudo filter language on elements properties or fields (reading private fields is supported but disabled by callingAssertions.setAllowExtractingPrivateFields(false)
.Note that the given
Iterable
is not modified, the filters are performed on a copy.With fluent filter language on element properties/fields :
WithList<Player> players = ...; assertThat(filter(players).with("pointsPerGame").greaterThan(20) .and("assistsPerGame").greaterThan(7).get()) .containsOnly(james, rose);
Condition
:public boolean matches(Player player) { return player.getPointsPerGame() > 20 && player.getAssistsPerGame() > 7; }; }; // use filter static method to build Filters assertThat(filter(players).being(potentialMVP).get()).containsOnly(james, rose);
- Type Parameters:
E
- the iterable elements type.- Parameters:
iterable
- theIterable
to filter.- Returns:
- the created
.Filters
- Throws:
NullPointerException
- if the given iterable isnull
.
-
filter
public static <E> Filters<E> filter(E[] array)
Creates a new
with the array to filter.Filters
Chain this call to express filter criteria either by a
Condition
or a pseudo filter language on elements properties.Note that the given array is not modified, the filters are performed on an
Iterable
copy of the array.With
Condition
:
WithPlayer[] players = ...; assertThat(filter(players).with("pointsPerGame").greaterThan(20) .and("assistsPerGame").greaterThan(7).get()) .containsOnly(james, rose);
Condition
:Condition<Player> potentialMVP = new Condition<Player>("is a possible MVP"){ public boolean matches(Player player) { return player.getPointsPerGame() > 20 && player.getAssistsPerGame() > 7; }; }; // use filter static method to build Filters assertThat(filter(players).being(potentialMVP).get()).containsOnly(james, rose);
- Type Parameters:
E
- the array elements type.- Parameters:
array
- the array to filter.- Returns:
- the created
.Filters
- Throws:
NullPointerException
- if the given array isnull
.
-
being
public Filters<E> being(Condition<? super E> condition)
Filter the underlying group, keeping only elements satisfying the givenCondition
.
Same ashaving(Condition)
- pick the method you prefer to have the most readable code.List<Player> players = ...; Condition<Player> potentialMVP = new Condition<Player>("is a possible MVP") { public boolean matches(Player player) { return player.getPointsPerGame() > 20 && player.getAssistsPerGame() > 7; }; }; // use filter static method to build Filters assertThat(filter(players).being(potentialMVP).get()).containsOnly(james, rose);
- Parameters:
condition
- the filterCondition
.- Returns:
- this
Filters
to chain other filter operations. - Throws:
IllegalArgumentException
- if the given condition isnull
.
-
having
public Filters<E> having(Condition<? super E> condition)
Filter the underlying group, keeping only elements satisfying the givenCondition
.
Same asbeing(Condition)
- pick the method you prefer to have the most readable code.List<Player> players = ...; Condition<Player> mvpStats = new Condition<Player>("is a possible MVP") { public boolean matches(Player player) { return player.getPointsPerGame() > 20 && player.getAssistsPerGame() > 7; }; }; // use filter static method to build Filters assertThat(filter(players).having(mvpStats).get()).containsOnly(james, rose);
- Parameters:
condition
- the filterCondition
.- Returns:
- this
Filters
to chain other filter operations. - Throws:
IllegalArgumentException
- if the given condition isnull
.
-
with
public Filters<E> with(String propertyOrFieldName, Object propertyValue)
Filter the underlying group, keeping only elements with a property equals to given value.Let's, for example, filter Employees with name "Alex" :
which is shortcut of :filter(employees).with("name", "Alex").get();
filter(employees).with("name").equalsTo("Alex").get();
- Parameters:
propertyOrFieldName
- the name of the property/field whose value will compared to given value. It may be a nested property.propertyValue
- the expected property value.- Returns:
- this
Filters
to chain other filter operations. - Throws:
IntrospectionError
- if an element in the givenIterable
does not have a property with a given propertyOrFieldName.IllegalArgumentException
- if the given propertyOrFieldName isnull
.
-
with
public Filters<E> with(String propertyOrFieldName)
Sets the name of the property used for filtering, it may be a nested property like"address.street.name"
.The typical usage is to chain this call with a comparison method, for example :
filter(employees).with("name").equalsTo("Alex").get();
- Parameters:
propertyOrFieldName
- the name of the property/field used for filtering. It may be a nested property.- Returns:
- this
Filters
to chain other filter operation. - Throws:
IllegalArgumentException
- if the given propertyOrFieldName isnull
.
-
validatePropertyOrFieldName
private void validatePropertyOrFieldName(String propertyOrFieldName)
-
and
public Filters<E> and(String propertyOrFieldName)
Alias ofwith(String)
for synthetic sugar to write things like :filter(employees).with("name").equalsTo("Alex").and("job").notEqualsTo("lawyer").get();
- Parameters:
propertyOrFieldName
- the name of the property/field used for filtering. It may be a nested property.- Returns:
- this
Filters
to chain other filter operation. - Throws:
IllegalArgumentException
- if the given propertyOrFieldName isnull
.
-
equalsTo
public Filters<E> equalsTo(Object propertyValue)
Filters the underlying iterable to keep object with property (specified bywith(String)
) equals to given value.Typical usage :
filter(employees).with("name").equalsTo("Luke").get();
- Parameters:
propertyValue
- the filter value.- Returns:
- this
Filters
to chain other filter operation. - Throws:
IllegalArgumentException
- if the property name to filter on has not been set.
-
notEqualsTo
public Filters<E> notEqualsTo(Object propertyValue)
Filters the underlying iterable to keep object with property (specified bywith(String)
) not equals to given value.Typical usage :
filter(employees).with("name").notEqualsTo("Vader").get();
- Parameters:
propertyValue
- the filter value.- Returns:
- this
Filters
to chain other filter operation. - Throws:
IllegalArgumentException
- if the property name to filter on has not been set.
-
checkPropertyNameToFilterOnIsNotNull
private void checkPropertyNameToFilterOnIsNotNull()
-
in
public Filters<E> in(Object... propertyValues)
Filters the underlying iterable to keep object with property (specified bywith(String)
) equals to one of the given values.Typical usage :
filter(players).with("team").in("Bulls", "Lakers").get();
- Parameters:
propertyValues
- the filter values.- Returns:
- this
Filters
to chain other filter operation. - Throws:
IllegalArgumentException
- if the property name to filter on has not been set.
-
notIn
public Filters<E> notIn(Object... propertyValues)
Filters the underlying iterable to keep object with property (specified bywith(String)
) not in the given values.Typical usage :
filter(players).with("team").notIn("Heat", "Lakers").get();
- Parameters:
propertyValues
- the filter values.- Returns:
- this
Filters
to chain other filter operation. - Throws:
IllegalArgumentException
- if the property name to filter on has not been set.
-
isItemInArray
private static boolean isItemInArray(Object item, Object[] arrayOfValues)
Returnstrue
if given item is in given array,false
otherwise.- Parameters:
item
- the object to look for in arrayOfValuesarrayOfValues
- the array of values- Returns:
true
if given item is in given array,false
otherwise.
-
-