T
- data typeF
- filter typepublic interface DataProvider<T,F> extends Serializable
DataProvider
interface is used by listing components implementing HasDataProvider
or HasFilterableDataProvider
. The listing component will provide a
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
ListDataProvider
which can be created using static create
methods in this interface. For custom backends such as SQL, EntityManager,
REST APIs or SpringData, use a BackEndDataProvider
or its subclass.
ofCollection(Collection)
,
ofItems(Object...)
,
fromStream(Stream)
,
fromCallbacks(CallbackDataProvider.FetchCallback,
CallbackDataProvider.CountCallback)
,
fromFilteringCallbacks(CallbackDataProvider.FetchCallback,
CallbackDataProvider.CountCallback)
,
ListDataProvider
,
BackEndDataProvider
Modifier and Type | Method and Description |
---|---|
Registration |
addDataProviderListener(DataProviderListener<T> listener)
Adds a data provider listener.
|
Stream<T> |
fetch(Query<T,F> query)
Fetches data from this DataProvider using given
query . |
static <T> CallbackDataProvider<T,Void> |
fromCallbacks(CallbackDataProvider.FetchCallback<T,Void> fetchCallback,
CallbackDataProvider.CountCallback<T,Void> countCallback)
Creates a new data provider that uses callbacks for fetching and counting
items from any backing store.
|
static <T,F> CallbackDataProvider<T,F> |
fromFilteringCallbacks(CallbackDataProvider.FetchCallback<T,F> fetchCallback,
CallbackDataProvider.CountCallback<T,F> countCallback)
Creates a new data provider that uses filtering callbacks for fetching
and counting items from any backing store.
|
static <T> ListDataProvider<T> |
fromStream(Stream<T> items)
Creates a new data provider from the given stream.
|
default Object |
getId(T item)
Gets an identifier for the given item.
|
boolean |
isInMemory()
Gets whether the DataProvider content all available in memory or does it
use some external backend.
|
static <T> ListDataProvider<T> |
ofCollection(Collection<T> items)
Creates a new data provider backed by a collection.
|
static <T> ListDataProvider<T> |
ofItems(T... items)
Creates a new data provider from the given items.
|
void |
refreshAll()
Refreshes all data based on currently available data in the underlying
provider.
|
void |
refreshItem(T item)
Refreshes the given item.
|
int |
size(Query<T,F> query)
Gets the amount of data in this DataProvider.
|
default ConfigurableFilterDataProvider<T,Void,F> |
withConfigurableFilter()
Wraps this data provider to create a data provider that supports
programmatically setting a filter but no filtering through the query.
|
default <Q,C> ConfigurableFilterDataProvider<T,Q,C> |
withConfigurableFilter(SerializableBiFunction<Q,C,F> filterCombiner)
Wraps this data provider to create a data provider that supports
programmatically setting a filter that will be combined with a filter
provided through the query.
|
default <C> DataProvider<T,C> |
withConvertedFilter(SerializableFunction<C,F> filterConverter)
Wraps this data provider to create a data provider that uses a different
filter type.
|
boolean isInMemory()
true
if all data is in memory; false
if notint size(Query<T,F> query)
query
- query with sorting and filteringStream<T> fetch(Query<T,F> query)
query
.query
- given query to request datanull
void refreshItem(T item)
DataProviderListeners
that an item has been
updated or replaced with a new instance.
For this to work properly, the item must either implement
Object.equals(Object)
and Object.hashCode()
to consider
both the old and the new item instances to be equal, or alternatively
getId(Object)
should be implemented to return an appropriate
identifier.
item
- the item to refreshgetId(Object)
void refreshAll()
default Object getId(T item)
Default is to use item itself as its own identifier. If the item has
Object.equals(Object)
and 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.
item
- the item to get identifier for; not null
null
Registration addDataProviderListener(DataProviderListener<T> listener)
The refreshAll()
method fires DataChangeEvent
each time
when it's called. It allows to update UI components when user changes
something in the underlying data.
listener
- the data change listener, not nullrefreshAll()
default <C> DataProvider<T,C> withConvertedFilter(SerializableFunction<C,F> filterConverter)
For example receiving a String from ComboBox and making a Predicate based on it:
DataProvider<Person, Predicate<Person>> dataProvider; // ComboBox uses String as the filter type DataProvider<Person, String> wrappedProvider = dataProvider .withConvertedFilter(filterText -> { Predicate<Person> predicate = person -> person.getName() .startsWith(filterText); return predicate; }); comboBox.setDataProvider(wrappedProvider);
C
- the filter type that the wrapped data provider accepts;
typically provided by a ComponentfilterConverter
- 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
null
default <Q,C> ConfigurableFilterDataProvider<T,Q,C> withConfigurableFilter(SerializableBiFunction<Q,C,F> filterCombiner)
Q
- the query filter typeC
- the configurable filter typefilterCombiner
- a callback for combining and the configured filter with the
filter from the query to get a filter to pass to the wrapped
provider. Either parameter might be null
, but the
callback will not be invoked at all if both would be
null
. Not null
.null
withConfigurableFilter()
,
ConfigurableFilterDataProvider.setFilter(Object)
default ConfigurableFilterDataProvider<T,Void,F> withConfigurableFilter()
null
withConfigurableFilter(SerializableBiFunction)
,
ConfigurableFilterDataProvider.setFilter(Object)
static <T> ListDataProvider<T> ofCollection(Collection<T> items)
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.
T
- the data item typeitems
- the collection of data, not null
@SafeVarargs static <T> ListDataProvider<T> ofItems(T... items)
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.
T
- the data item typeitems
- the data itemsstatic <T> ListDataProvider<T> fromStream(Stream<T> items)
This is a shorthand for using ofCollection(Collection)
after
collecting the items in the stream to a list with e.g.
stream.collect(Collectors.toList));
.
Using big streams is not recommended, you should instead use a
lazy data provider. See
fromCallbacks(CallbackDataProvider.FetchCallback, CallbackDataProvider.CountCallback)
or BackEndDataProvider
for more info.
T
- the data item typeitems
- a stream of data items, not null
static <T,F> CallbackDataProvider<T,F> fromFilteringCallbacks(CallbackDataProvider.FetchCallback<T,F> fetchCallback, CallbackDataProvider.CountCallback<T,F> countCallback)
The query that is passed to each callback may contain a filter value that is provided by the component querying for data.
T
- data provider data typeF
- data provider filter typefetchCallback
- function that returns a stream of items from the back end for
a querycountCallback
- function that returns the number of items in the back end for
a querystatic <T> CallbackDataProvider<T,Void> fromCallbacks(CallbackDataProvider.FetchCallback<T,Void> fetchCallback, CallbackDataProvider.CountCallback<T,Void> countCallback)
The query that is passed to each callback will not contain any filter values.
T
- data provider data typefetchCallback
- function that returns a stream of items from the back end for
a querycountCallback
- function that returns the number of items in the back end for
a queryCopyright © 2019. All rights reserved.