summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenri Sara <henri.sara@itmill.com>2011-03-14 10:27:31 +0000
committerHenri Sara <henri.sara@itmill.com>2011-03-14 10:27:31 +0000
commit42787665b1d4d0fc59a8c3c0b875dbbe021e73ab (patch)
treee1b0a38440ce52bcbe36a644c885b0c79b76b566
parentfe2e938d2d14b7906f7c7317115c5bf31b49e973 (diff)
downloadvaadin-framework-42787665b1d4d0fc59a8c3c0b875dbbe021e73ab.tar.gz
vaadin-framework-42787665b1d4d0fc59a8c3c0b875dbbe021e73ab.zip
#6286 Container filtering improvements: Not (negation) filter and unit test
svn changeset:17750/svn branch:6.6
-rw-r--r--src/com/vaadin/data/util/filter/Not.java64
-rw-r--r--tests/src/com/vaadin/tests/server/container/filter/NotFilterTest.java42
2 files changed, 106 insertions, 0 deletions
diff --git a/src/com/vaadin/data/util/filter/Not.java b/src/com/vaadin/data/util/filter/Not.java
new file mode 100644
index 0000000000..0e2ec4c55e
--- /dev/null
+++ b/src/com/vaadin/data/util/filter/Not.java
@@ -0,0 +1,64 @@
+package com.vaadin.data.util.filter;
+
+import com.vaadin.data.Container.Filter;
+import com.vaadin.data.Item;
+
+/**
+ * Negating filter that accepts the items rejected by another filter.
+ *
+ * This filter directly supports in-memory filtering when the negated filter
+ * does so.
+ *
+ * @since 6.6
+ */
+public class Not implements Filter {
+ private final Filter filter;
+
+ /**
+ * Constructs a filter that negates a filter.
+ *
+ * @param filter
+ * {@link Filter} to negate, not-null
+ */
+ public Not(Filter filter) {
+ this.filter = filter;
+ }
+
+ /**
+ * Returns the negated filter.
+ *
+ * @return Filter
+ */
+ public Filter getFilter() {
+ return filter;
+ }
+
+ public boolean passesFilter(Item item) throws UnsupportedOperationException {
+ return !filter.passesFilter(item);
+ }
+
+ /**
+ * Returns true if a change in the named property may affect the filtering
+ * result. Return value is the same as {@link #appliesToProperty(Object)}
+ * for the negated filter.
+ *
+ * @return boolean
+ */
+ public boolean appliesToProperty(Object propertyId) {
+ return filter.appliesToProperty(propertyId);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null || !getClass().equals(obj.getClass())) {
+ return false;
+ }
+ return filter.equals(((Not) obj).getFilter());
+ }
+
+ @Override
+ public int hashCode() {
+ return filter.hashCode();
+ }
+
+}
diff --git a/tests/src/com/vaadin/tests/server/container/filter/NotFilterTest.java b/tests/src/com/vaadin/tests/server/container/filter/NotFilterTest.java
new file mode 100644
index 0000000000..983b4cf60a
--- /dev/null
+++ b/tests/src/com/vaadin/tests/server/container/filter/NotFilterTest.java
@@ -0,0 +1,42 @@
+package com.vaadin.tests.server.container.filter;
+
+import junit.framework.Assert;
+
+import com.vaadin.data.Container.Filter;
+import com.vaadin.data.Item;
+import com.vaadin.data.util.BeanItem;
+import com.vaadin.data.util.filter.And;
+import com.vaadin.data.util.filter.Not;
+
+public class NotFilterTest extends AbstractFilterTest {
+
+ protected Item item1 = new BeanItem<Integer>(1);
+ protected Item item2 = new BeanItem<Integer>(2);
+
+ public void testNot() {
+ Filter origFilter = new SameItemFilter(item1);
+ Filter filter = new Not(origFilter);
+
+ Assert.assertTrue(origFilter.passesFilter(item1));
+ Assert.assertFalse(origFilter.passesFilter(item2));
+ Assert.assertFalse(filter.passesFilter(item1));
+ Assert.assertTrue(filter.passesFilter(item2));
+ }
+
+ public void testNotEqualsHashCode() {
+ Filter origFilter = new SameItemFilter(item1);
+ Filter filter1 = new Not(origFilter);
+ Filter filter1b = new Not(new SameItemFilter(item1));
+ Filter filter2 = new Not(new SameItemFilter(item2));
+
+ // equals()
+ Assert.assertEquals(filter1, filter1b);
+ Assert.assertFalse(filter1.equals(filter2));
+ Assert.assertFalse(filter1.equals(origFilter));
+ Assert.assertFalse(filter1.equals(new And()));
+
+ // hashCode()
+ Assert.assertEquals(filter1.hashCode(), filter1b.hashCode());
+ }
+
+}