From 4b3b5a232589128151324221eb6fbf51eb5387ab Mon Sep 17 00:00:00 2001 From: Pekka Hyvönen Date: Thu, 15 Dec 2016 14:48:40 +0200 Subject: Rename com.vaadin.server.data to com.vaadin.data.provider (#8004) Renamed corresponding test packages too. Fixes vaadin/framework8-issues#110 --- .../vaadin/data/provider/BackEndDataProvider.java | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java (limited to 'server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java') 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 + * data provider data type + * @param + * data provider filter type + */ +public class BackEndDataProvider extends AbstractDataProvider { + + private final SerializableFunction, Stream> request; + private final SerializableFunction, 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, Stream> request, + SerializableFunction, 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 fetch(Query query) { + return request.apply(query); + } + + @Override + public int size(Query 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 sortingBy( + List> sortOrders) { + BackEndDataProvider parent = this; + return new BackEndDataProvider(query -> { + List> 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; + } + +} -- cgit v1.2.3