diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-11-21 18:43:41 +0200 |
---|---|---|
committer | Henri Sara <hesara@vaadin.com> | 2016-11-22 08:28:30 +0000 |
commit | 38a14a9621057a6016eada7271d8f3acdd4eb77d (patch) | |
tree | c323c79d2539a405814b786d39c56bf6b843b611 | |
parent | c75f71c74a53c0ca538da4785589ffb756dc0f2e (diff) | |
download | vaadin-framework-38a14a9621057a6016eada7271d8f3acdd4eb77d.tar.gz vaadin-framework-38a14a9621057a6016eada7271d8f3acdd4eb77d.zip |
Add DataProvider.setFilter method
Change-Id: I87193d3d2c66a880c2be165cfd49926fa290010b
3 files changed, 64 insertions, 3 deletions
diff --git a/server/src/main/java/com/vaadin/server/data/DataProvider.java b/server/src/main/java/com/vaadin/server/data/DataProvider.java index 19c657be70..129fe08713 100644 --- a/server/src/main/java/com/vaadin/server/data/DataProvider.java +++ b/server/src/main/java/com/vaadin/server/data/DataProvider.java @@ -90,6 +90,19 @@ public interface DataProvider<T, F> extends Serializable { Registration addDataProviderListener(DataProviderListener listener); /** + * Returns a new wrapped data provider with given default filter. Default + * filter will apply to each query and further filtering will not be + * supported in the wrapped data provider. + * + * @param filter + * the default filter + * @return wrapped data provider with provided filter + */ + public default DataProvider<T, Void> setFilter(F filter) { + return new FilteringDataProviderWrapper<>(this, filter); + } + + /** * Convert the data provider to use a different filter type. It is used for * adapting this data provider to a filter type provided by a Component such * as ComboBox. @@ -117,7 +130,7 @@ public interface DataProvider<T, F> extends Serializable { * * @return wrapped data provider */ - default <M> DataProvider<T, M> convertFilter( + public default <M> DataProvider<T, M> convertFilter( SerializableFunction<M, F> mapper) { return new FilteringDataProviderWrapper<>(this, mapper); } diff --git a/server/src/main/java/com/vaadin/server/data/FilteringDataProviderWrapper.java b/server/src/main/java/com/vaadin/server/data/FilteringDataProviderWrapper.java index 10384a9c9c..a6807cfb26 100644 --- a/server/src/main/java/com/vaadin/server/data/FilteringDataProviderWrapper.java +++ b/server/src/main/java/com/vaadin/server/data/FilteringDataProviderWrapper.java @@ -38,7 +38,32 @@ public class FilteringDataProviderWrapper<T, F, M> private DataProvider<T, M> dataProvider; private SerializableFunction<F, M> mapper; + private M staticFilter = null; + /** + * Constructs a filtering wrapper for a data provider with always applied + * static filter. + * + * @param dataProvider + * the wrapped data provider + * @param filter + * the static filter + */ + public FilteringDataProviderWrapper(DataProvider<T, M> dataProvider, + M filter) { + this.dataProvider = dataProvider; + this.staticFilter = filter; + } + + /** + * Constructs a filtering wrapper for a data provider with a mapping from + * one filter type to another. + * + * @param dataProvider + * the wrapped data provider + * @param mapper + * the filter mapping function + */ public FilteringDataProviderWrapper(DataProvider<T, M> dataProvider, SerializableFunction<F, M> mapper) { this.dataProvider = dataProvider; @@ -63,13 +88,19 @@ public class FilteringDataProviderWrapper<T, F, M> @Override public int size(Query<F> t) { return dataProvider.size(new Query<M>(t.getOffset(), t.getLimit(), - t.getSortOrders(), t.getFilter().map(mapper).orElse(null))); + t.getSortOrders(), getFilter(t))); } @Override public Stream<T> fetch(Query<F> t) { return dataProvider.fetch(new Query<M>(t.getOffset(), t.getLimit(), - t.getSortOrders(), t.getFilter().map(mapper).orElse(null))); + t.getSortOrders(), getFilter(t))); } + private M getFilter(Query<F> query) { + if (staticFilter != null) { + return staticFilter; + } + return query.getFilter().map(mapper).orElse(null); + } } diff --git a/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java b/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java index 6a53aae528..29a234061f 100644 --- a/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java +++ b/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java @@ -11,6 +11,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.server.SerializablePredicate; import com.vaadin.server.data.DataProvider; import com.vaadin.server.data.ListDataProvider; import com.vaadin.server.data.Query; @@ -216,4 +217,20 @@ public class ListDataProviderTest { dataProvider.size(new Query<>( strBean -> strBean.getValue().contains("Foo")))); } + + @Test + public void filteringListDataProvider_defaultFilter() { + SerializablePredicate<StrBean> filter = s -> s.getRandomNumber() > 4; + // Intentionally lost filter type. Not actually filterable anymore. + DataProvider<StrBean, ?> filtered = dataProvider.setFilter(filter); + + Assert.assertEquals("Filter not applied, unexpected item count", + dataProvider.size(new Query<>(filter)), + filtered.size(new Query<>())); + + Assert.assertEquals("Further filtering succeeded", + filtered.size(new Query<>()), + filtered.size((Query) new Query<SerializablePredicate<StrBean>>( + s -> s.getValue().equals("Foo")))); + } } |