aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-11-21 18:43:41 +0200
committerHenri Sara <hesara@vaadin.com>2016-11-22 08:28:30 +0000
commit38a14a9621057a6016eada7271d8f3acdd4eb77d (patch)
treec323c79d2539a405814b786d39c56bf6b843b611 /server/src/main
parentc75f71c74a53c0ca538da4785589ffb756dc0f2e (diff)
downloadvaadin-framework-38a14a9621057a6016eada7271d8f3acdd4eb77d.tar.gz
vaadin-framework-38a14a9621057a6016eada7271d8f3acdd4eb77d.zip
Add DataProvider.setFilter method
Change-Id: I87193d3d2c66a880c2be165cfd49926fa290010b
Diffstat (limited to 'server/src/main')
-rw-r--r--server/src/main/java/com/vaadin/server/data/DataProvider.java15
-rw-r--r--server/src/main/java/com/vaadin/server/data/FilteringDataProviderWrapper.java35
2 files changed, 47 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);
+ }
}