com.vaadin.data.provider.

Class DataProviderWrapper<T,​F,​M>

  • Type Parameters:

    T - data provider data type

    F - wrapper query filter type

    M - underlying data provider filter type

    All Implemented Interfaces:

    DataProvider<T,​F>, Serializable

    Direct Known Subclasses:

    ConfigurableFilterDataProviderWrapper

    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:

    Serialized Form

    • 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, not null

    • 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 interface DataProvider<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 interface DataProvider<T,​F>

      • 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) 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.

        Specified by:

        getId in interface DataProvider<T,​F>

        Parameters:

        item - the item to get identifier for; not null

        Returns:

        the identifier for given item; not null

      • 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 interface DataProvider<T,​F>

        Parameters:

        t - query with sorting and filtering

        Returns:

        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:

        
         try (Stream<T> stream = fetch(query)) {
             stream.forEach(System.out::println); // Example terminal operation
         }
         
        NOTE: This mainly concerns data providers where DataProvider.isInMemory() returns false. 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 if DataProvider.isInMemory() returns true.

        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) or BackEndDataProvider for more info.

        Specified by:

        fetch in interface DataProvider<T,​F>

        Parameters:

        t - given query to request data

        Returns:

        the result of the query request: a stream of data objects, not null

      • getFilter

        protected abstract M getFilter​(Query<T,​F> query)

        Gets the filter that should be used in the modified Query.

        Parameters:

        query - the current query

        Returns:

        filter for the modified Query