ca.odell.glazedlists.impl.matchers.RangeMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glazedlists_java15 Show documentation
Show all versions of glazedlists_java15 Show documentation
Event-driven lists for dynamically filtered and sorted tables
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.matchers;
import ca.odell.glazedlists.Filterator;
import ca.odell.glazedlists.matchers.Matcher;
import java.util.ArrayList;
import java.util.List;
/**
* A Matcher for matching {@link Comparable}s within a range. The list of
* objects being filtered contains either:
*
*
* - Comparable objects
*
- objects from which Comparable can be extracted via a {@link Filterator}
*
*
* @author James Lemieux
*/
public class RangeMatcher implements Matcher {
/** The start of the range; null
if the range has no starting value. */
private final D start;
/** The end of the range; null
if the range has no ending value. */
private final D end;
/**
* The Filterator which can extract Comparables from the filter objects;
* null
implies the objects to be filtered are already
* Comparable objects.
*/
private final Filterator filterator;
/** a heavily recycled list of filter Comparables, call clear() before use */
private final List filterComparables = new ArrayList();
/**
* This constructor should be used when the objects to be filtered are
* already {@link Comparable} objects and thus no conversion is necessary
* to be used with this matcher.
*
* @param start the start of the range to filter on; null
* indicates there is no start to the range
* @param end the end of the range to filter on; null
* indicates there is no end to the range
*/
public RangeMatcher(D start, D end) {
this(start, end, null);
}
/**
* This constructor should be used when the objects to be filtered are not
* {@link Comparable} objects, but contain {@link Comparable} objects which
* can be extracted. The given filterator
is used to extract
* those {@link Comparable} objects from the filtered objects.
*
* @param start the start of the range to filter on; null
* indicates there is no start to the range
* @param end the end of the range to filter on; null
* indicates there is no end to the range
* @param filterator contains the logic to fetch {@link Comparable} objects
* from the list of filtered objects
*/
public RangeMatcher(D start, D end, Filterator filterator) {
this.start = start;
this.end = end;
this.filterator = filterator;
}
/** {@inheritDoc} */
public boolean matches(E item) {
filterComparables.clear();
if (filterator == null)
filterComparables.add((D) item);
else
filterator.getFilterValues(filterComparables, item);
// ensure the range contains at least one extracted Comparable
for (int c = 0; c < filterComparables.size(); c++) {
D filterComparable = filterComparables.get(c);
// check if the filterComparable is within the defined range
if (filterComparable != null) {
if (start != null && start.compareTo(filterComparable) > 0)
continue;
if (end != null && end.compareTo(filterComparable) < 0)
continue;
}
// a filterComparable is within the given range, so the object matches
return true;
}
// no filterComparable fell within this range
return false;
}
/** {@inheritDoc} */
@Override
public String toString() {
return "[RangeMatcher between " + start + " and " + end + "]";
}
}