diff options
author | Henri Sara <henri.sara@itmill.com> | 2011-03-10 15:32:10 +0000 |
---|---|---|
committer | Henri Sara <henri.sara@itmill.com> | 2011-03-10 15:32:10 +0000 |
commit | f11a23b57b25aa48833bc56b3c48c1dac8d4c571 (patch) | |
tree | 0f13e16c9fdc62aabb71b634dfd8717b00e0479e /src | |
parent | 9f2c36a22f006ec64980572a91fe5069fa93ad47 (diff) | |
download | vaadin-framework-f11a23b57b25aa48833bc56b3c48c1dac8d4c571.tar.gz vaadin-framework-f11a23b57b25aa48833bc56b3c48c1dac8d4c571.zip |
#6286 Container filtering improvements: logical And and Or filters
svn changeset:17714/svn branch:6.6
Diffstat (limited to 'src')
-rw-r--r-- | src/com/vaadin/data/util/filter/AbstractJunctionFilter.java | 72 | ||||
-rw-r--r-- | src/com/vaadin/data/util/filter/And.java | 39 | ||||
-rw-r--r-- | src/com/vaadin/data/util/filter/Or.java | 58 |
3 files changed, 169 insertions, 0 deletions
diff --git a/src/com/vaadin/data/util/filter/AbstractJunctionFilter.java b/src/com/vaadin/data/util/filter/AbstractJunctionFilter.java new file mode 100644 index 0000000000..f6464763c4 --- /dev/null +++ b/src/com/vaadin/data/util/filter/AbstractJunctionFilter.java @@ -0,0 +1,72 @@ +package com.vaadin.data.util.filter; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +import com.vaadin.data.Container.Filter; + +/** + * Abstract base class for filters that are composed of multiple sub-filters. + * + * The method {@link #appliesToProperty(Object)} is provided to help + * implementing {@link Filter} for in-memory filters. + * + * @since 6.6 + */ +public abstract class AbstractJunctionFilter implements Filter { + + protected final Collection<Filter> filters; + + public AbstractJunctionFilter(Filter... filters) { + this.filters = Collections.unmodifiableCollection(Arrays + .asList(filters)); + } + + /** + * Returns an unmodifiable collection of the sub-filters of this composite + * filter. + * + * @return + */ + public Collection<Filter> getFilters() { + return filters; + } + + /** + * Returns true if a change in the named property may affect the filtering + * result. If some of the sub-filters are not in-memory filters, true is + * returned. + * + * By default, all sub-filters are iterated to check if any of them applies. + * If there are no sub-filters, false is returned - override in subclasses + * to change this behavior. + */ + public boolean appliesToProperty(Object propertyId) { + for (Filter filter : getFilters()) { + if (filter.appliesToProperty(propertyId)) { + return true; + } + } + return false; + } + + @Override + public boolean equals(Object obj) { + if (obj == null || !getClass().equals(obj.getClass())) { + return false; + } + AbstractJunctionFilter other = (AbstractJunctionFilter) obj; + // contents comparison with equals() + return Arrays.equals(filters.toArray(), other.filters.toArray()); + } + + @Override + public int hashCode() { + int hash = getFilters().size(); + for (Filter filter : filters) { + hash = (hash << 1) ^ filter.hashCode(); + } + return hash; + } +}
\ No newline at end of file diff --git a/src/com/vaadin/data/util/filter/And.java b/src/com/vaadin/data/util/filter/And.java new file mode 100644 index 0000000000..ec1a2bf8c6 --- /dev/null +++ b/src/com/vaadin/data/util/filter/And.java @@ -0,0 +1,39 @@ +package com.vaadin.data.util.filter; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; + +/** + * A compound {@link Filter} that accepts an item if all of its filters accept + * the item. + * + * If no filters are given, the filter should accept all items. + * + * This filter also directly supports in-memory filtering when all sub-filters + * do so. + * + * @see Or + * + * @since 6.6 + */ +public class And extends AbstractJunctionFilter implements Filter { + + /** + * + * @param filters + * filters of which the And filter will be composed + */ + public And(Filter... filters) { + super(filters); + } + + public boolean passesFilter(Item item) throws UnsupportedFilterException { + for (Filter filter : getFilters()) { + if (!filter.passesFilter(item)) { + return false; + } + } + return true; + } + +} diff --git a/src/com/vaadin/data/util/filter/Or.java b/src/com/vaadin/data/util/filter/Or.java new file mode 100644 index 0000000000..c4a3684e48 --- /dev/null +++ b/src/com/vaadin/data/util/filter/Or.java @@ -0,0 +1,58 @@ +package com.vaadin.data.util.filter; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; + +/** + * A compound {@link Filter} that accepts an item if any of its filters accept + * the item. + * + * If no filters are given, the filter should reject all items. + * + * This filter also directly supports in-memory filtering when all sub-filters + * do so. + * + * @see And + * + * @since 6.6 + */ +public class Or extends AbstractJunctionFilter implements Filter { + + /** + * + * @param filters + * filters of which the Or filter will be composed + */ + public Or(Filter... filters) { + super(filters); + } + + public boolean passesFilter(Item item) throws UnsupportedFilterException { + for (Filter filter : getFilters()) { + if (filter.passesFilter(item)) { + return true; + } + } + return false; + } + + /** + * Returns true if a change in the named property may affect the filtering + * result. If some of the sub-filters are not in-memory filters, true is + * returned. + * + * By default, all sub-filters are iterated to check if any of them applies. + * If there are no sub-filters, true is returned as an empty Or rejects all + * items. + */ + @Override + public boolean appliesToProperty(Object propertyId) { + if (getFilters().isEmpty()) { + // empty Or filters out everything + return true; + } else { + return super.appliesToProperty(propertyId); + } + } + +} |