From 61ee1ab804a82c80ce15bc49595bc41b0beb98b3 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 1 Apr 2009 11:53:22 +0000 Subject: [PATCH] #2517 merged to 6.0: BeanItemContainer ate exceptions; generalized Filter from IndexedContainer. svn changeset:7270/svn branch:6.0 --- .../toolkit/data/util/BeanItemContainer.java | 38 +------- src/com/itmill/toolkit/data/util/Filter.java | 87 +++++++++++++++++++ .../toolkit/data/util/IndexedContainer.java | 67 +------------- 3 files changed, 90 insertions(+), 102 deletions(-) create mode 100644 src/com/itmill/toolkit/data/util/Filter.java diff --git a/src/com/itmill/toolkit/data/util/BeanItemContainer.java b/src/com/itmill/toolkit/data/util/BeanItemContainer.java index 7fa747372d..457526efc4 100644 --- a/src/com/itmill/toolkit/data/util/BeanItemContainer.java +++ b/src/com/itmill/toolkit/data/util/BeanItemContainer.java @@ -28,6 +28,7 @@ import com.itmill.toolkit.data.Property.ValueChangeNotifier; *

* Bean objects act as identifiers. For this reason, they should implement * Object.equals(Object) and Object.hashCode() . + *

* * @param * @@ -362,24 +363,6 @@ public class BeanItemContainer implements Indexed, Sortable, Filterable, } } - class Filter { - - private final Object propertyId; - private final String filterString; - private final boolean onlyMatchPrefix; - private final boolean ignoreCase; - - public Filter(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - this.propertyId = propertyId; - this.ignoreCase = ignoreCase; - this.filterString = ignoreCase ? filterString.toLowerCase() - : filterString; - this.onlyMatchPrefix = onlyMatchPrefix; - } - - } - @SuppressWarnings("unchecked") public void addContainerFilter(Object propertyId, String filterString, boolean ignoreCase, boolean onlyMatchPrefix) { @@ -423,24 +406,7 @@ public class BeanItemContainer implements Indexed, Sortable, Filterable, Iterator iterator = list.iterator(); while (iterator.hasNext()) { BT bean = iterator.next(); - // TODO #2517: should not swallow exceptions - requires several - // checks - try { - String value = getContainerProperty(bean, f.propertyId) - .getValue().toString(); - if (f.ignoreCase) { - value = value.toLowerCase(); - } - if (f.onlyMatchPrefix) { - if (!value.startsWith(f.filterString)) { - iterator.remove(); - } - } else { - if (!value.contains(f.filterString)) { - iterator.remove(); - } - } - } catch (Exception e) { + if (!f.passesFilter(getItem(bean))) { iterator.remove(); } } diff --git a/src/com/itmill/toolkit/data/util/Filter.java b/src/com/itmill/toolkit/data/util/Filter.java new file mode 100644 index 0000000000..cbb1efae0d --- /dev/null +++ b/src/com/itmill/toolkit/data/util/Filter.java @@ -0,0 +1,87 @@ +package com.itmill.toolkit.data.util; + +import com.itmill.toolkit.data.Item; +import com.itmill.toolkit.data.Property; + +/** + * A default filter that can be used to implement + * {@link com.itmill.toolkit.data.Container.Filterable}. + * + * @since 5.4 + */ +public class Filter { + final Object propertyId; + final String filterString; + final boolean ignoreCase; + final boolean onlyMatchPrefix; + + Filter(Object propertyId, String filterString, boolean ignoreCase, + boolean onlyMatchPrefix) { + this.propertyId = propertyId; + ; + this.filterString = ignoreCase ? filterString.toLowerCase() + : filterString; + this.ignoreCase = ignoreCase; + this.onlyMatchPrefix = onlyMatchPrefix; + } + + /** + * Check if an item passes the filter. + * + * @param item + * @return true if the item is accepted by this filter + */ + public boolean passesFilter(Item item) { + final Property p = item.getItemProperty(propertyId); + if (p == null || p.toString() == null) { + return false; + } + final String value = ignoreCase ? p.toString().toLowerCase() : p + .toString(); + if (onlyMatchPrefix) { + if (!value.startsWith(filterString)) { + return false; + } + } else { + if (!value.contains(filterString)) { + return false; + } + } + return true; + } + + @Override + public boolean equals(Object obj) { + + // Only ones of the objects of the same class can be equal + if (!(obj instanceof Filter)) { + return false; + } + final Filter o = (Filter) obj; + + // Checks the properties one by one + if (propertyId != o.propertyId && o.propertyId != null + && !o.propertyId.equals(propertyId)) { + return false; + } + if (filterString != o.filterString && o.filterString != null + && !o.filterString.equals(filterString)) { + return false; + } + if (ignoreCase != o.ignoreCase) { + return false; + } + if (onlyMatchPrefix != o.onlyMatchPrefix) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + return (propertyId != null ? propertyId.hashCode() : 0) + ^ (filterString != null ? filterString.hashCode() : 0); + } + +} \ No newline at end of file diff --git a/src/com/itmill/toolkit/data/util/IndexedContainer.java b/src/com/itmill/toolkit/data/util/IndexedContainer.java index af4b1e131c..0dbe44fc46 100644 --- a/src/com/itmill/toolkit/data/util/IndexedContainer.java +++ b/src/com/itmill/toolkit/data/util/IndexedContainer.java @@ -1566,57 +1566,6 @@ public class IndexedContainer implements Container.Indexed, return super.equals(obj); } - private class Filter { - Object propertyId; - String filterString; - boolean ignoreCase; - boolean onlyMatchPrefix; - - Filter(Object propertyId, String filterString, boolean ignoreCase, - boolean onlyMatchPrefix) { - this.propertyId = propertyId; - ; - this.filterString = filterString; - this.ignoreCase = ignoreCase; - this.onlyMatchPrefix = onlyMatchPrefix; - } - - @Override - public boolean equals(Object obj) { - - // Only ones of the objects of the same class can be equal - if (!(obj instanceof Filter)) { - return false; - } - final Filter o = (Filter) obj; - - // Checks the properties one by one - if (propertyId != o.propertyId && o.propertyId != null - && !o.propertyId.equals(propertyId)) { - return false; - } - if (filterString != o.filterString && o.filterString != null - && !o.filterString.equals(filterString)) { - return false; - } - if (ignoreCase != o.ignoreCase) { - return false; - } - if (onlyMatchPrefix != o.onlyMatchPrefix) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - return (propertyId != null ? propertyId.hashCode() : 0) - ^ (filterString != null ? filterString.hashCode() : 0); - } - - } - public void addContainerFilter(Object propertyId, String filterString, boolean ignoreCase, boolean onlyMatchPrefix) { if (filters == null) { @@ -1705,23 +1654,9 @@ public class IndexedContainer implements Container.Indexed, final Iterator i = filters.iterator(); while (i.hasNext()) { final Filter f = i.next(); - final String s1 = f.ignoreCase ? f.filterString.toLowerCase() - : f.filterString; - final Property p = item.getItemProperty(f.propertyId); - if (p == null || p.toString() == null) { + if (!f.passesFilter(item)) { return false; } - final String s2 = f.ignoreCase ? p.toString().toLowerCase() : p - .toString(); - if (f.onlyMatchPrefix) { - if (s2.indexOf(s1) != 0) { - return false; - } - } else { - if (s2.indexOf(s1) < 0) { - return false; - } - } } return true; } -- 2.39.5