Package ca.odell.glazedlists.matchers
Class Matchers
- java.lang.Object
-
- ca.odell.glazedlists.matchers.Matchers
-
public final class Matchers extends java.lang.ObjectA factory for creating Matchers.- Author:
- Jesse Wilson
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> Matcher<E>and(Matcher<? super E>... matchers)Returns a Matcher which returns a match when all of the givenmatchersreport a match.static <E> Matcher<E>beanPropertyMatcher(java.lang.Class<E> beanClass, java.lang.String propertyName, java.lang.Object expectedValue)Creates aMatcherthat uses Reflection to compare the expectedValue of the specified property of an object to theexpectedValue.static <E> booleancontains(java.util.Collection<E> collection, Matcher<? super E> matcher)Returns true if the givencollectioncontains an element that satisfies the givenmatcher; false otherise.static <E> intcount(java.util.Collection<E> collection, Matcher<? super E> matcher)Iterate through the specified collection and count all elements that match the specified matcher.static <E> Matcher<E>falseMatcher()Get aMatcherthat always returns false, therefore matching nothing..static <E> booleanfilter(java.util.Collection<E> collection, Matcher<? super E> matcher)Iterate through the specified collection and remove all elements that don't match the specified matcher.static <E> intindexOf(java.util.List<E> list, Matcher<? super E> matcher)Returns the index of the first element from the givenlistthat satisfies thematcheror -1 if no such element exists.static <E> Matcher<E>invert(Matcher<E> original)static <E> Matcher<E>isNotNull()Get aMatcherthat returns returns true iff it is given anullobject.static <E> Matcher<E>isNull()Get aMatcherthat returns returns true iff it is given anon-nullobject.static Matcher<java.lang.String>nonNullAndNonEmptyString()static <E> Matcher<E>or(Matcher<? super E>... matchers)Returns a Matcher which returns a match when any of the givenmatchersreports a match.static Matcher<java.beans.PropertyChangeEvent>propertyEventNameMatcher(boolean matchPropertyNames, java.lang.String... propertyNames)Create aMatcherthat uses the givenpropertyNamesto matchPropertyChangeEvents by their property name.static <D extends java.lang.Comparable,E>
Matcher<E>rangeMatcher(D start, D end)Creates aMatcherthat matchesComparableobjects for containment within the range between the givenstartandend.static <D extends java.lang.Comparable,E>
Matcher<E>rangeMatcher(D start, D end, Filterator<D,E> filterator)Creates aMatcherthat uses the givenfilteratorto extractComparableobjects from filtered objects and compares those Comparables against the range between the givenstartandend.static <E> E[]select(E[] items, Matcher<? super E> matcher)Return a new array containing only theitemsthat satisfy thematcher.static <E> java.util.Collection<? super E>select(java.util.Collection<E> collection, Matcher<? super E> matcher)Add all elements from the givencollectionthat satisfy thematcherto a newArrayList.static <E> java.util.Collection<? super E>select(java.util.Collection<E> collection, Matcher<? super E> matcher, java.util.Collection<? super E> results)Add all elements from the givencollectionthat satisfy thematcherto the givenresultsCollection.static <E> Matcher<E>trueMatcher()Get aMatcherthat always returns true, therefore matching everything.static <E> Matcher<E>types(java.lang.Class... classes)Returns a Matcher which reports a match when the given object to match is not null and reports on of the givenclassesas its type.static <E> MatcherEditor<E>weakReferenceProxy(MatcherEditor<E> matcherEditor)Provides a proxy to another MatcherEditor that may go out of scope without explicitly removing itself from the source MatcherEditor's set of listeners.
-
-
-
Method Detail
-
weakReferenceProxy
public static <E> MatcherEditor<E> weakReferenceProxy(MatcherEditor<E> matcherEditor)
Provides a proxy to another MatcherEditor that may go out of scope without explicitly removing itself from the source MatcherEditor's set of listeners.This exists to solve a garbage collection problem. Suppose I have a
TheMatcherEditorM which is long lived and manyMatcherEditor.Listeners, t which must listen to M while they exist. Instead of adding each of the t directly as listeners of M, add a proxy instead. The proxy will retain aWeakReferenceto the t, and will remove itself from the list of listeners for M.MatcherEditorreturned by this method makes implementing the above scheme trivial. It does two things for you automatically:- It wraps each
MatcherEditor.Listenerpassed toMatcherEditor.addMatcherEditorListener(ca.odell.glazedlists.matchers.MatcherEditor.Listener<E>)in aWeakReferenceso that the listeners are garbage collected when they become unreachable. - It registers itself as a weak listener of the
given
matcherEditorso the MatcherEditor returned by this method will be garbage collected when it becomes unreachable.
- See Also:
WeakReference
- It wraps each
-
trueMatcher
public static <E> Matcher<E> trueMatcher()
Get aMatcherthat always returns true, therefore matching everything.
-
falseMatcher
public static <E> Matcher<E> falseMatcher()
Get aMatcherthat always returns false, therefore matching nothing..
-
isNull
public static <E> Matcher<E> isNull()
Get aMatcherthat returns returns true iff it is given anon-nullobject.
-
isNotNull
public static <E> Matcher<E> isNotNull()
Get aMatcherthat returns returns true iff it is given anullobject.
-
nonNullAndNonEmptyString
public static Matcher<java.lang.String> nonNullAndNonEmptyString()
-
beanPropertyMatcher
public static <E> Matcher<E> beanPropertyMatcher(java.lang.Class<E> beanClass, java.lang.String propertyName, java.lang.Object expectedValue)
Creates aMatcherthat uses Reflection to compare the expectedValue of the specified property of an object to theexpectedValue.
-
rangeMatcher
public static <D extends java.lang.Comparable,E> Matcher<E> rangeMatcher(D start, D end)
Creates aMatcherthat matchesComparableobjects for containment within the range between the givenstartandend.
-
rangeMatcher
public static <D extends java.lang.Comparable,E> Matcher<E> rangeMatcher(D start, D end, Filterator<D,E> filterator)
Creates aMatcherthat uses the givenfilteratorto extractComparableobjects from filtered objects and compares those Comparables against the range between the givenstartandend. If at least one Comparable returned by thefilteratoris within the range, the object is considered a match.nullstartorendvalues are allowed and are interpreted as"no start"or"no end"to the range respectively.- Parameters:
start- theComparablewhich starts the rangeend- theComparablewhich ends the rangefilterator- the logic for extracting filterComparables from filtered objects
-
propertyEventNameMatcher
public static Matcher<java.beans.PropertyChangeEvent> propertyEventNameMatcher(boolean matchPropertyNames, java.lang.String... propertyNames)
Create aMatcherthat uses the givenpropertyNamesto matchPropertyChangeEvents by their property name. The concrete behaviour depends on thematchPropertyNamesparameter. If you want to match property change events against a known set of property names, use a value oftrue. Alternatively, when you specifyfalse, the specified property names will serve as an exclude list, e.g. if an event matches a specified property name, it will be filtered out.These matchers are especially useful as an event matcher in a bean connector.
- Parameters:
matchPropertyNames- iftrue, match property change events against the specified property names, iffalsefilter thempropertyNames- the property names to consider- See Also:
GlazedLists.beanConnector(Class, Matcher)
-
count
public static <E> int count(java.util.Collection<E> collection, Matcher<? super E> matcher)Iterate through the specified collection and count all elements that match the specified matcher.- Returns:
- the number of elements in the collection that are matched
-
filter
public static <E> boolean filter(java.util.Collection<E> collection, Matcher<? super E> matcher)Iterate through the specified collection and remove all elements that don't match the specified matcher.- Returns:
trueif any elements were removed from the specifiedCollection
-
select
public static <E> E[] select(E[] items, Matcher<? super E> matcher)Return a new array containing only theitemsthat satisfy thematcher.- Parameters:
items- the array of Objects to searchmatcher- the criteria for considering an element a match- Returns:
- a new array containing only the
itemsthat satisfy thematcher
-
select
public static <E> java.util.Collection<? super E> select(java.util.Collection<E> collection, Matcher<? super E> matcher)Add all elements from the givencollectionthat satisfy thematcherto a newArrayList.- Parameters:
collection- the Collection to searchmatcher- the criteria for considering an element a match- Returns:
- a new
ArrayListcontaining the elements which satisfy thematcher
-
select
public static <E> java.util.Collection<? super E> select(java.util.Collection<E> collection, Matcher<? super E> matcher, java.util.Collection<? super E> results)Add all elements from the givencollectionthat satisfy thematcherto the givenresultsCollection.resultscan be any Collection that supports theCollection.add(E)operation.- Parameters:
collection- the Collection to searchmatcher- the criteria for considering an element a matchresults- the Collection into which matching elements are added- Returns:
- the
resultsCollectioncontaining the elements which satisfy thematcher
-
contains
public static <E> boolean contains(java.util.Collection<E> collection, Matcher<? super E> matcher)Returns true if the givencollectioncontains an element that satisfies the givenmatcher; false otherise.- Parameters:
collection- the Collection to searchmatcher- the criteria for considering an element a match- Returns:
- true if the given
collectioncontains an element that satisfies the givenmatcher; false otherise
-
indexOf
public static <E> int indexOf(java.util.List<E> list, Matcher<? super E> matcher)Returns the index of the first element from the givenlistthat satisfies thematcheror -1 if no such element exists.- Parameters:
list- the List to searchmatcher- the criteria for considering an element a match- Returns:
- the index of the first element from the given
listthat satisfies thematcheror -1 if no such element exists
-
or
public static <E> Matcher<E> or(Matcher<? super E>... matchers)
Returns a Matcher which returns a match when any of the givenmatchersreports a match.- Parameters:
matchers- the Collection of Matchers to combine with an "or" operator- Returns:
- a Matcher that combines the
matchersvia an "or" operator
-
and
public static <E> Matcher<E> and(Matcher<? super E>... matchers)
Returns a Matcher which returns a match when all of the givenmatchersreport a match.- Parameters:
matchers- the Collection of Matchers to combine with an "and" operator- Returns:
- a Matcher that combines the
matchersvia an "and" operator
-
types
public static <E> Matcher<E> types(java.lang.Class... classes)
Returns a Matcher which reports a match when the given object to match is not null and reports on of the givenclassesas its type.- Parameters:
classes- the object types that are matched- Returns:
- a Matcher which reports a match when the given object to match
is not null and reports on of the given
classesas its type
-
-