diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-12-15 14:48:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-15 14:48:40 +0200 |
commit | 4b3b5a232589128151324221eb6fbf51eb5387ab (patch) | |
tree | 834f51f4af862a1b387cf5061fc55ba58e6ba9b5 /server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java | |
parent | 0defe58203e0f3509239a32cc4a06510d79954a2 (diff) | |
download | vaadin-framework-4b3b5a232589128151324221eb6fbf51eb5387ab.tar.gz vaadin-framework-4b3b5a232589128151324221eb6fbf51eb5387ab.zip |
Rename com.vaadin.server.data to com.vaadin.data.provider (#8004)
Renamed corresponding test packages too.
Fixes vaadin/framework8-issues#110
Diffstat (limited to 'server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java')
-rw-r--r-- | server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java new file mode 100644 index 0000000000..2ceebc995a --- /dev/null +++ b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java @@ -0,0 +1,106 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.provider; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; + +import com.vaadin.server.SerializableFunction; +import com.vaadin.shared.Registration; + +/** + * A {@link DataProvider} for any back end. + * + * @param <T> + * data provider data type + * @param <F> + * data provider filter type + */ +public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> { + + private final SerializableFunction<Query<T, F>, Stream<T>> request; + private final SerializableFunction<Query<T, F>, Integer> sizeCallback; + + /** + * Constructs a new DataProvider to request data from an arbitrary back end + * request function. + * + * @param request + * function that requests data from back end based on query + * @param sizeCallback + * function that return the amount of data in back end for query + */ + public BackEndDataProvider( + SerializableFunction<Query<T, F>, Stream<T>> request, + SerializableFunction<Query<T, F>, Integer> sizeCallback) { + Objects.requireNonNull(request, "Request function can't be null"); + Objects.requireNonNull(sizeCallback, "Size callback can't be null"); + this.request = request; + this.sizeCallback = sizeCallback; + } + + @Override + public Stream<T> fetch(Query<T, F> query) { + return request.apply(query); + } + + @Override + public int size(Query<T, F> query) { + return sizeCallback.apply(query); + } + + /** + * Sets a default sorting order to the data provider. + * + * @param sortOrders + * a list of sorting information containing field ids and + * directions + * @return new data provider with modified sorting + */ + @SuppressWarnings("serial") + public BackEndDataProvider<T, F> sortingBy( + List<SortOrder<String>> sortOrders) { + BackEndDataProvider<T, F> parent = this; + return new BackEndDataProvider<T, F>(query -> { + List<SortOrder<String>> queryOrder = new ArrayList<>( + query.getSortOrders()); + queryOrder.addAll(sortOrders); + return parent.fetch(new Query<>(query.getOffset(), query.getLimit(), + queryOrder, query.getInMemorySorting(), + query.getFilter().orElse(null))); + }, sizeCallback) { + + @Override + public Registration addDataProviderListener( + DataProviderListener listener) { + return parent.addDataProviderListener(listener); + } + + @Override + public void refreshAll() { + parent.refreshAll(); + } + }; + } + + @Override + public boolean isInMemory() { + return false; + } + +} |