Just as we filtered issues for text using TextFilterList,
we can filter with arbitrary criteria by implementing our own Matcher. This interface specifies a single method, matches() that tests whether an element matches. If you've worked with predicates, a Matcher is the same thing.
For our filter, we implement a Matcher that matches all Issues reported by a predefined set of users.
import java.util.*;
// glazed lists
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.matchers.*;
// a simple issues library
import ca.odell.issuezilla.*;
/**
* This {@link Matcher} only matches users in a predefined set.
*
* @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a>
*/
public class IssuesForUsersMatcher implements Matcher {
/** the users to match */
private Set users = new HashSet();
/**
* Create a new {@link IssuesForUsersMatcher} that matches only
* {@link Issue}s that have one or more user in the specified list.
*/
public IssuesForUsersMatcher(Collection users) {
// defensive copy all the users
this.users.addAll(users);
}
/**
* Test whether to include or not include the specified issue based
* on whether or not their user is selected.
*/
public boolean matches(Object o) {
if(o == null) return false;
if(users.isEmpty()) return true;
Issue issue = (Issue)o;
String user = issue.getReporter();
return users.contains(user);
}
}With this IssuesForUsersMatcher in place, we can create an EventList that contains only issues that match the users we specify:
List users = new ArrayList();
users.add("jessewilson");
users.add("kevinmaltby");
users.add("tmao");
Matcher usersMatcher = new IssuesForUsersMatcher(users);
EventList issues = ...
FilterList IssuesForUsers = new FilterList(issues, usersMatcher);
To avoid concurrency and consistency problems, it is best to make all Matcher implementations immutable. This will guarantee that your matches() method can be safely called from any thread without synchronization.