/* * 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.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; import com.vaadin.data.HasDataProvider; import com.vaadin.data.HasFilterableDataProvider; import com.vaadin.data.provider.CallbackDataProvider.CountCallback; import com.vaadin.data.provider.CallbackDataProvider.FetchCallback; import com.vaadin.server.SerializableBiFunction; import com.vaadin.server.SerializableFunction; import com.vaadin.shared.Registration; /** * A common interface for fetching data from a backend. The {@link DataProvider} * interface is used by listing components implementing {@link HasDataProvider} * or {@link HasFilterableDataProvider}. The listing component will provide a * {@link Query} object with request information, and the data provider uses * this information to return a stream containing requested beans. *
* Vaadin comes with a ready-made solution for in-memory data, known as
* {@link ListDataProvider} which can be created using static {@code create}
* methods in this interface. For custom backends such as SQL, EntityManager,
* REST APIs or SpringData, use a {@link BackEndDataProvider} or its subclass.
*
* @author Vaadin Ltd.
*
* @param
* For this to work properly, the item must either implement
* {@link #equals(Object)} and {@link #hashCode()} to consider both the old
* and the new item instances to be equal, or alternatively
* {@link #getId(Object)} should be implemented to return an appropriate
* identifier.
*
* @see #getId(Object)
*
* @param item
* the item to refresh
*/
void refreshItem(T item);
/**
* Refreshes all data based on currently available data in the underlying
* provider.
*/
void refreshAll();
/**
* Gets an identifier for the given item. This identifier is used by the
* framework to determine equality between two items.
*
* Default is to use item itself as its own identifier. If the item has
* {@link Object#equals(Object)} and {@link Object#hashCode()} implemented
* in a way that it can be compared to other items, no changes are required.
*
* Note: This method will be called often by the Framework.
* It should not do any expensive operations.
*
* @param item
* the item to get identifier for; not {@code null}
* @return the identifier for given item; not {@code null}
*/
public default Object getId(T item) {
Objects.requireNonNull(item, "Cannot provide an id for a null item.");
return item;
}
/**
* Adds a data provider listener. The listener is called when some piece of
* data is updated.
*
* The {@link #refreshAll()} method fires {@link DataChangeEvent} each time
* when it's called. It allows to update UI components when user changes
* something in the underlying data.
*
* @see #refreshAll()
* @param listener
* the data change listener, not null
* @return a registration for the listener
*/
Registration addDataProviderListener(DataProviderListener
* For example receiving a String from ComboBox and making a Predicate based
* on it:
*
*
* The collection is used as-is. Changes in the collection will be visible
* via the created data provider. The caller should copy the collection if
* necessary.
*
* @param
* The items are copied into a new backing list, so structural changes to
* the provided array will not be visible via the created data provider.
*
* @param
* This is a shorthand for using {@link #ofCollection(Collection)} after
* collecting the items in the stream to a list with e.g.
* {@code stream.collect(Collectors.toList));}.
*
* Using big streams is not recommended, you should instead use a
* lazy data provider. See
* {@link #fromCallbacks(FetchCallback, CountCallback)} or
* {@link BackEndDataProvider} for more info.
*
* @param
* The query that is passed to each callback may contain a filter value that
* is provided by the component querying for data.
*
* @param fetchCallback
* function that returns a stream of items from the back end for
* a query
* @param countCallback
* function that returns the number of items in the back end for
* a query
* @return a new callback data provider
*/
public static
* The query that is passed to each callback will not contain any filter
* values.
*
* @param fetchCallback
* function that returns a stream of items from the back end for
* a query
* @param countCallback
* function that returns the number of items in the back end for
* a query
* @return a new callback data provider
*/
public static
* DataProvider<Person, Predicate<Person>> dataProvider;
* // ComboBox uses String as the filter type
* DataProvider<Person, String> wrappedProvider = dataProvider
* .withConvertedFilter(filterText -> {
* SerializablePredicate<Person> predicate = person -> person.getName()
* .startsWith(filterText);
* return predicate;
* });
* comboBox.setDataProvider(wrappedProvider);
*
*
* @param filterConverter
* callback that converts the filter in the query of the wrapped
* data provider into a filter supported by this data provider.
* Will only be called if the query contains a filter. Not
* null
*
* @param null
*/
public default null
, but the
* callback will not be invoked at all if both would be
* null
. Not null
.
*
* @return a data provider with a configurable filter, not null
*/
public default ConfigurableFilterDataProvider
filterCombiner) {
return new ConfigurableFilterDataProviderWrapper
null
*/
public default ConfigurableFilterDataProvidernull
* @return a new list data provider
*/
public static