diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-11-25 16:05:29 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-11-28 15:05:36 +0200 |
commit | b89b7f2d05dd66ce588311720a9ec6121a39d83c (patch) | |
tree | 6c1823dbae50a04bb1ad13f00e22523bdecd27f7 /server | |
parent | 411b8dedb02be7ed1a9332fc1f965e583bd313cc (diff) | |
download | vaadin-framework-b89b7f2d05dd66ce588311720a9ec6121a39d83c.tar.gz vaadin-framework-b89b7f2d05dd66ce588311720a9ec6121a39d83c.zip |
Add filter type to DataCommunicator
Change-Id: Id7948e2f81b6a9c593ce8b71bb3a61b578344af9
Diffstat (limited to 'server')
5 files changed, 39 insertions, 39 deletions
diff --git a/server/src/main/java/com/vaadin/server/data/DataCommunicator.java b/server/src/main/java/com/vaadin/server/data/DataCommunicator.java index 88624cee22..2628c79058 100644 --- a/server/src/main/java/com/vaadin/server/data/DataCommunicator.java +++ b/server/src/main/java/com/vaadin/server/data/DataCommunicator.java @@ -24,13 +24,11 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Set; -import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import com.vaadin.server.AbstractExtension; import com.vaadin.server.KeyMapper; -import com.vaadin.server.SerializablePredicate; import com.vaadin.shared.Range; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorClientRpc; @@ -49,10 +47,12 @@ import elemental.json.JsonObject; * * @param <T> * the bean type + * @param <F> + * the filter type * * @since 8.0 */ -public class DataCommunicator<T> extends AbstractExtension { +public class DataCommunicator<T, F> extends AbstractExtension { private Registration dataProviderUpdateRegistration; @@ -183,7 +183,7 @@ public class DataCommunicator<T> extends AbstractExtension { private final ActiveDataHandler handler = new ActiveDataHandler(); /** Empty default data provider */ - private DataProvider<T, ?> dataProvider = new BackEndDataProvider<>( + private DataProvider<T, F> dataProvider = new BackEndDataProvider<>( q -> Stream.of(), q -> 0); private final DataKeyMapper<T> keyMapper; @@ -192,8 +192,8 @@ public class DataCommunicator<T> extends AbstractExtension { private int minPushSize = 40; private Range pushRows = Range.withLength(0, minPushSize); + private F filter; private Comparator<T> inMemorySorting; - private SerializablePredicate<T> inMemoryFilter; private final List<SortOrder<String>> backEndSorting = new ArrayList<>(); private final DataCommunicatorClientRpc rpc; @@ -229,14 +229,7 @@ public class DataCommunicator<T> extends AbstractExtension { } if (initial || reset) { - int dataProviderSize; - if (getDataProvider().isInMemory() && inMemoryFilter != null) { - dataProviderSize = (int) getDataProvider().fetch(new Query<>()) - .filter(inMemoryFilter).count(); - } else { - // TODO: Apply filter - dataProviderSize = getDataProvider().size(new Query<>()); - } + int dataProviderSize = getDataProvider().size(new Query<>(filter)); rpc.reset(dataProviderSize); } @@ -247,18 +240,16 @@ public class DataCommunicator<T> extends AbstractExtension { Stream<T> rowsToPush; if (getDataProvider().isInMemory()) { + // TODO: Move in-memory sorting to Query. // We can safely request all the data when in memory - rowsToPush = getDataProvider().fetch(new Query<>()); - if (inMemoryFilter != null) { - rowsToPush = rowsToPush.filter(inMemoryFilter); - } + rowsToPush = getDataProvider().fetch(new Query<>(filter)); if (inMemorySorting != null) { rowsToPush = rowsToPush.sorted(inMemorySorting); } rowsToPush = rowsToPush.skip(offset).limit(limit); } else { rowsToPush = getDataProvider().fetch( - new Query<>(offset, limit, backEndSorting, null)); + new Query<>(offset, limit, backEndSorting, filter)); } pushData(offset, rowsToPush); } @@ -405,13 +396,13 @@ public class DataCommunicator<T> extends AbstractExtension { } /** - * Sets the {@link Predicate} to use with in-memory filtering. + * Sets the filter to use. * - * @param predicate - * predicate used to filter data + * @param filter + * the filter */ - public void setInMemoryFilter(SerializablePredicate<T> predicate) { - inMemoryFilter = predicate; + public void setFilter(F filter) { + this.filter = filter; reset(); } @@ -465,7 +456,7 @@ public class DataCommunicator<T> extends AbstractExtension { * * @return the data provider */ - public DataProvider<T, ?> getDataProvider() { + public DataProvider<T, F> getDataProvider() { return dataProvider; } @@ -475,7 +466,7 @@ public class DataCommunicator<T> extends AbstractExtension { * @param dataProvider * the data provider to set, not null */ - public void setDataProvider(DataProvider<T, ?> dataProvider) { + public void setDataProvider(DataProvider<T, F> dataProvider) { Objects.requireNonNull(dataProvider, "data provider cannot be null"); this.dataProvider = dataProvider; detachDataProviderListener(); diff --git a/server/src/main/java/com/vaadin/ui/AbstractListing.java b/server/src/main/java/com/vaadin/ui/AbstractListing.java index 8578b18673..2f455fa27b 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractListing.java +++ b/server/src/main/java/com/vaadin/ui/AbstractListing.java @@ -119,7 +119,7 @@ public abstract class AbstractListing<T> extends AbstractComponent { } } - private final DataCommunicator<T> dataCommunicator; + private final DataCommunicator<T, ?> dataCommunicator; /** * Creates a new {@code AbstractListing} with a default data communicator. @@ -147,7 +147,7 @@ public abstract class AbstractListing<T> extends AbstractComponent { * @param dataCommunicator * the data communicator to use, not null */ - protected AbstractListing(DataCommunicator<T> dataCommunicator) { + protected AbstractListing(DataCommunicator<T, ?> dataCommunicator) { Objects.requireNonNull(dataCommunicator, "dataCommunicator cannot be null"); @@ -155,8 +155,10 @@ public abstract class AbstractListing<T> extends AbstractComponent { addExtension(dataCommunicator); } + @SuppressWarnings({ "rawtypes", "unchecked" }) protected void internalSetDataProvider(DataProvider<T, ?> dataProvider) { - getDataCommunicator().setDataProvider(dataProvider); + // cast needed by compiler + getDataCommunicator().setDataProvider((DataProvider) dataProvider); } protected DataProvider<T, ?> internalGetDataProvider() { @@ -248,7 +250,7 @@ public abstract class AbstractListing<T> extends AbstractComponent { * * @return the data communicator, not null */ - public DataCommunicator<T> getDataCommunicator() { + public DataCommunicator<T, ?> getDataCommunicator() { return dataCommunicator; } diff --git a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java index 0f2d6c81bc..6510b5305a 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java @@ -88,7 +88,7 @@ public abstract class AbstractSingleSelect<T> extends AbstractListing<T> * @param dataCommunicator * the data communicator to use, not null */ - protected AbstractSingleSelect(DataCommunicator<T> dataCommunicator) { + protected AbstractSingleSelect(DataCommunicator<T, ?> dataCommunicator) { super(dataCommunicator); init(); } diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index b9630766cb..f0f90eab22 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -132,15 +132,11 @@ public class ComboBox<T> extends AbstractSingleSelect<T> @Override public void setFilter(String filterText) { filterstring = filterText; - if (filterText != null) { - getDataCommunicator().setInMemoryFilter( - item -> filter.apply(filterstring, item)); - } else { - getDataCommunicator().setInMemoryFilter(null); - } + getDataCommunicator().setFilter(filterText); } }; + // TODO: Check if this can be removed. private String filterstring; /** @@ -166,7 +162,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> * {@link #setItems(Collection)} */ public ComboBox() { - super(new DataCommunicator<T>() { + super(new DataCommunicator<T, String>() { @Override protected DataKeyMapper<T> createKeyMapper() { return new KeyMapper<T>() { @@ -611,6 +607,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> } @Override + @SuppressWarnings("unchecked") public DataProvider<T, String> getDataProvider() { return (DataProvider<T, String>) internalGetDataProvider(); } @@ -620,4 +617,11 @@ public class ComboBox<T> extends AbstractSingleSelect<T> internalSetDataProvider(dataProvider); } + @Override + @SuppressWarnings("unchecked") + public DataCommunicator<T, String> getDataCommunicator() { + // Not actually an unsafe cast. DataCommunicator is final and set by + // ComboBox. + return (DataCommunicator<T, String>) super.getDataCommunicator(); + } } diff --git a/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java b/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java index 4c40c1a654..36238a2bdc 100644 --- a/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java +++ b/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java @@ -22,6 +22,7 @@ import org.junit.Test; import org.mockito.Mockito; import com.vaadin.server.MockVaadinSession; +import com.vaadin.server.SerializablePredicate; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinService; import com.vaadin.server.VaadinSession; @@ -62,7 +63,8 @@ public class DataCommunicatorTest { } @Override - public Registration addDataProviderListener(DataProviderListener listener) { + public Registration addDataProviderListener( + DataProviderListener listener) { registration = super.addDataProviderListener(listener); return this; } @@ -79,7 +81,8 @@ public class DataCommunicatorTest { } - private static class TestDataCommunicator extends DataCommunicator<Object> { + private static class TestDataCommunicator + extends DataCommunicator<Object, SerializablePredicate<Object>> { protected void extend(UI ui) { super.extend(ui); } |