com.vaadin.data.provider.
Class DataProviderWrapper<T,F,M>
- java.lang.Object
-
- com.vaadin.data.provider.DataProviderWrapper<T,F,M>
-
Type Parameters:
T
- data provider data typeF
- wrapper query filter typeM
- underlying data provider filter typeAll Implemented Interfaces:
DataProvider<T,F>
,Serializable
Direct Known Subclasses:
public abstract class DataProviderWrapper<T,F,M> extends Object implements DataProvider<T,F>
Wrapper class for modifying, chaining and replacing filters and sorting in a query. Used to create a suitable
Query
for the underlying data provider with correct filters and sorting.Since:
8.0
Author:
Vaadin Ltd.
See Also:
-
-
Field Summary
Fields Modifier and Type Field Description protected DataProvider<T,M>
dataProvider
The actual data provider behind this wrapper.
-
Constructor Summary
Constructors Modifier Constructor Description protected
DataProviderWrapper(DataProvider<T,M> dataProvider)
Constructs a filtering wrapper for a data provider.
-
Method Summary
All Methods Modifier and Type Method Description Registration
addDataProviderListener(DataProviderListener<T> listener)
Adds a data provider listener.
Stream<T>
fetch(Query<T,F> t)
Fetches data from this DataProvider using given
query
.protected abstract M
getFilter(Query<T,F> query)
Gets the filter that should be used in the modified Query.
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.
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> t)
Gets the amount of data in this DataProvider.
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.vaadin.data.provider.DataProvider
withConfigurableFilter, withConfigurableFilter, withConvertedFilter
-
-
-
-
Field Detail
-
dataProvider
protected DataProvider<T,M> dataProvider
The actual data provider behind this wrapper.
-
-
Constructor Detail
-
DataProviderWrapper
protected DataProviderWrapper(DataProvider<T,M> dataProvider)
Constructs a filtering wrapper for a data provider.
Parameters:
dataProvider
- the wrapped data provider, notnull
-
-
Method Detail
-
isInMemory
public boolean isInMemory()
Description copied from interface:
DataProvider
Gets whether the DataProvider content all available in memory or does it use some external backend.
Specified by:
isInMemory
in interfaceDataProvider<T,F>
Returns:
true
if all data is in memory;false
if not
-
refreshAll
public void refreshAll()
Description copied from interface:
DataProvider
Refreshes all data based on currently available data in the underlying provider.
Specified by:
refreshAll
in interfaceDataProvider<T,F>
-
refreshItem
public void refreshItem(T item)
Description copied from interface:
DataProvider
Refreshes the given item. This method should be used to inform all
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)
andObject.hashCode()
to consider both the old and the new item instances to be equal, or alternativelyDataProvider.getId(Object)
should be implemented to return an appropriate identifier.Specified by:
refreshItem
in interfaceDataProvider<T,F>
Parameters:
item
- the item to refreshSee Also:
-
getId
public Object getId(T item)
Description copied from interface:
DataProvider
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
Object.equals(Object)
andObject.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.
Specified by:
getId
in interfaceDataProvider<T,F>
Parameters:
item
- the item to get identifier for; notnull
Returns:
the identifier for given item; not
null
-
addDataProviderListener
public Registration addDataProviderListener(DataProviderListener<T> listener)
Description copied from interface:
DataProvider
Adds a data provider listener. The listener is called when some piece of data is updated.
The
DataProvider.refreshAll()
method firesDataChangeEvent
each time when it's called. It allows to update UI components when user changes something in the underlying data.Specified by:
addDataProviderListener
in interfaceDataProvider<T,F>
Parameters:
listener
- the data change listener, not nullReturns:
a registration for the listener
See Also:
-
size
public int size(Query<T,F> t)
Description copied from interface:
DataProvider
Gets the amount of data in this DataProvider.
Specified by:
size
in interfaceDataProvider<T,F>
Parameters:
t
- query with sorting and filteringReturns:
the size of the data provider
-
fetch
public Stream<T> fetch(Query<T,F> t)
Description copied from interface:
DataProvider
Fetches data from this DataProvider using given
query
.Depending on the use case this operation might open I/O channels.
Consumers of the returned stream are responsible for closing it when all the stream operations are done to ensure that any resources feeding the stream are properly released. Failure to close an I/O stream might lead to resource leaks.
It is strongly recommended to use a try-with-resources block to automatically close an I/O stream after its terminal operation has been executed. Below is an example of how to properly use and close the stream:
NOTE: This mainly concerns data providers wheretry (Stream<T> stream = fetch(query)) { stream.forEach(System.out::println); // Example terminal operation }
DataProvider.isInMemory()
returnsfalse
. While it is technically possible to configure or extend an in-memory data provider in a way that compromises the in-memory nature, doing so is conceptually frowned upon. In applications that choose to do so regardless, the use of this pattern is strongly recommended even ifDataProvider.isInMemory()
returnstrue
.The try-with-resources pattern is safe to use with this method even if you are unsure whether or not it returns an I/O stream, but it's unnecessary to apply the pattern to all stream handling everywhere. For most streams the only consumed resource is memory, and Java's built-in cleanup is sufficient to free it.
With large data sets where memory consumption might become a problem it is recommended to use lazy data providers instead of in-memory ones. See e.g.
DataProvider.fromCallbacks(FetchCallback, CountCallback)
orBackEndDataProvider
for more info.Specified by:
fetch
in interfaceDataProvider<T,F>
Parameters:
t
- given query to request dataReturns:
the result of the query request: a stream of data objects, not
null
-
-