@ -22,13 +22,14 @@ import java.util.Collections;
import org.jspecify.annotations.Nullable ;
import org.jspecify.annotations.Nullable ;
/ * *
/ * *
* A simple instance filter that checks if a given instance match based on
* A simple instance filter that checks if a given instance matches based on
* a collection of includes and excludes element .
* collections of includes and excludes .
*
*
* < p > Subclasses may want to override { @link # match ( Object , Object ) } to provide
* < p > Subclasses may override { @link # match ( Object , Object ) } to provide a custom
* a custom matching algorithm .
* matching algorithm .
*
*
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Sam Brannen
* @since 4 . 1
* @since 4 . 1
* @param < T > the instance type
* @param < T > the instance type
* /
* /
@ -42,17 +43,33 @@ public class InstanceFilter<T> {
/ * *
/ * *
* Create a new instance based on includes / excludes collections .
* Create a new { @code InstanceFilter } based on include and exclude collections ,
* < p > A particular element will match if it "matches" the one of the element in the
* with the { @code matchIfEmpty } flag set to { @code true } .
* includes list and does not match one of the element in the excludes list .
* < p > See { @link # InstanceFilter ( Collection , Collection , boolean ) } for details .
* < p > Subclasses may redefine what matching means . By default , an element match with
* another if it is equals according to { @link Object # equals ( Object ) }
* < p > If both collections are empty , { @code matchIfEmpty } defines if
* an element matches or not .
* @param includes the collection of includes
* @param includes the collection of includes
* @param excludes the collection of excludes
* @param excludes the collection of excludes
* @param matchIfEmpty the matching result if both the includes and the excludes
* @since 7 . 0
* collections are empty
* /
public InstanceFilter ( @Nullable Collection < ? extends T > includes ,
@Nullable Collection < ? extends T > excludes ) {
this ( includes , excludes , true ) ;
}
/ * *
* Create a new { @code InstanceFilter } based on include and exclude collections .
* < p > A particular element will match if it < em > matches < / em > one of the elements
* in the { @code includes } list and does not match one of the elements in the
* { @code excludes } list .
* < p > Subclasses may redefine what matching means . By default , an element
* { @linkplain # match ( Object , Object ) matches } another if the two elements are
* { @linkplain Object # equals ( Object ) equal } .
* < p > If both collections are empty , { @code matchIfEmpty } defines if an element
* matches or not .
* @param includes the collection of includes
* @param excludes the collection of excludes
* @param matchIfEmpty the matching result if the includes and the excludes
* collections are both { @code null } or empty
* /
* /
public InstanceFilter ( @Nullable Collection < ? extends T > includes ,
public InstanceFilter ( @Nullable Collection < ? extends T > includes ,
@Nullable Collection < ? extends T > excludes , boolean matchIfEmpty ) {
@Nullable Collection < ? extends T > excludes , boolean matchIfEmpty ) {
@ -87,9 +104,12 @@ public class InstanceFilter<T> {
}
}
/ * *
/ * *
* Determine if the specified { @code instance } is equal to the
* Determine if the specified { @code instance } matches the specified
* specified { @code candidate } .
* { @code candidate } .
* @param instance the instance to handle
* < p > By default , the two instances match if they are
* { @linkplain Object # equals ( Object ) equal } .
* < p > Can be overridden by subclasses .
* @param instance the instance to check
* @param candidate a candidate defined by this filter
* @param candidate a candidate defined by this filter
* @return { @code true } if the instance matches the candidate
* @return { @code true } if the instance matches the candidate
* /
* /
@ -99,10 +119,10 @@ public class InstanceFilter<T> {
/ * *
/ * *
* Determine if the specified { @code instance } matches one of the candidates .
* Determine if the specified { @code instance } matches one of the candidates .
* < p > If the candidates collection is { @code null } , returns { @code false } .
* @param instance the instance to check
* @param instance the instance to check
* @param candidates a list of candidates
* @param candidates the collection of candidates
* @return { @code true } if the instance match or the candidates collection is null
* @return { @code true } if the instance matches ; { @code false } if the
* candidates collection is empty or there is no match
* /
* /
protected boolean match ( T instance , Collection < ? extends T > candidates ) {
protected boolean match ( T instance , Collection < ? extends T > candidates ) {
for ( T candidate : candidates ) {
for ( T candidate : candidates ) {