Class AbstractLazyDataView<T>

java.lang.Object
com.vaadin.flow.data.provider.AbstractDataView<T>
com.vaadin.flow.data.provider.AbstractLazyDataView<T>
Type Parameters:
T - the type of data
All Implemented Interfaces:
DataView<T>, LazyDataView<T>, Serializable
Direct Known Subclasses:
ComboBoxLazyDataView, GridLazyDataView

public abstract class AbstractLazyDataView<T> extends AbstractDataView<T> implements LazyDataView<T>
Abstract lazy data view implementation which handles the interaction with a data communicator.
See Also:
  • Constructor Details

    • AbstractLazyDataView

      public AbstractLazyDataView(DataCommunicator<T> dataCommunicator, Component component)
      Creates a new instance and verifies the passed data provider is compatible with this data view implementation.
      Parameters:
      dataCommunicator - the data communicator of the component
      component - the component
  • Method Details

    • getDataCommunicator

      protected DataCommunicator<T> getDataCommunicator()
      Returns the data communicator for the component and checks that the data provider is of the correct type.
      Returns:
      the data communicator
    • getItem

      public T getItem(int index)
      Gets the item at the given index from the data available to the component. Data is filtered and sorted the same way as in the component.

      Calling this method with an index that is not currently active in the component will cause a query to the backend, so do not call this method carelessly. Use UI.beforeClientResponse(Component, SerializableConsumer) to access items that will be fetched later on.

      Specified by:
      getItem in interface DataView<T>
      Parameters:
      index - the index of the item to get
      Returns:
      item on index
      Throws:
      IndexOutOfBoundsException - requested index is outside of the filtered and sorted data set
    • getItems

      public Stream<T> getItems()
      Description copied from interface: DataView
      Get the full data available to the component. Data is filtered and sorted the same way as in the component.

      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 the stream might lead to resource leaks.

      It is strongly recommended to use a try-with-resources block to automatically close the 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 = dataView.getItems()) {
           stream.forEach(System.out::println); // Example terminal operation
       }
       
      Specified by:
      getItems in interface DataView<T>
      Overrides:
      getItems in class AbstractDataView<T>
      Returns:
      filtered and sorted data set
    • getSupportedDataProviderType

      protected Class<?> getSupportedDataProviderType()
      Description copied from class: AbstractDataView
      Returns supported DataProvider type for this DataView.
      Specified by:
      getSupportedDataProviderType in class AbstractDataView<T>
      Returns:
      supported data provider type
    • setItemCountEstimate

      public void setItemCountEstimate(int itemCountEstimate)
      Description copied from interface: LazyDataView
      Sets the estimated item count for the component. The component will automatically fetch more items once the estimate is reached or adjust the count if the backend runs out of items before the end. Use this when the backend will have a lot more items than shown by default and it should be shown to the user.

      The given estimate is discarded if it is less than the currently shown range or if the actual number of items has been determined. The estimate shouldn't be less than two pages (see setPageSize(int) in the component) or it causes unnecessary backend requests.

      Specified by:
      setItemCountEstimate in interface LazyDataView<T>
      Parameters:
      itemCountEstimate - estimated item count of the backend
      See Also:
    • getItemCountEstimate

      public int getItemCountEstimate()
      Description copied from interface: LazyDataView
      Gets the item count estimate. The default value depends on the component.
      Specified by:
      getItemCountEstimate in interface LazyDataView<T>
      Returns:
      item count estimate
      See Also:
    • setItemCountEstimateIncrease

      public void setItemCountEstimateIncrease(int itemCountEstimateIncrease)
      Description copied from interface: LazyDataView
      Sets how much the item count estimate is increased once the previous item count estimate has been reached. Use this when the user should be able to scroll down faster. The default value depends on the component.

      As an example, with an estimate of 1000 and an increase of 500, when scrolling down the item count will be: 1000, 1500, 2000, 2500... until the backend runs out of items.

      NOTE: the given increase should not be less than the setPageSize(int) set in the component, or there will be unnecessary backend requests.

      Specified by:
      setItemCountEstimateIncrease in interface LazyDataView<T>
      Parameters:
      itemCountEstimateIncrease - how much the item count estimate is increased
      See Also:
    • getItemCountEstimateIncrease

      public int getItemCountEstimateIncrease()
      Description copied from interface: LazyDataView
      Gets the item count estimate increase - how much the item count estimate is increased once the previous item count estimate has been reached. The default value depends on the component.
      Specified by:
      getItemCountEstimateIncrease in interface LazyDataView<T>
      Returns:
      the item count estimate increase
      See Also:
    • setItemCountFromDataProvider

      public void setItemCountFromDataProvider()
      Description copied from interface: LazyDataView
      Switches the component to get the exact item count from the data provider's DataProvider.size(Query). Use this when it is cheap to get the exact item count and it is desired that the user sees the "full scrollbar size".
      Specified by:
      setItemCountFromDataProvider in interface LazyDataView<T>
    • setItemCountUnknown

      public void setItemCountUnknown()
      Description copied from interface: LazyDataView
      Switches the component to automatically extend the number of items as the previous end is almost reached. The component stops increasing the number of items once the backend has run out of items. Not getting the exact size of the backend upfront can improve performance with large sets of data.

      The default initial item count and how much the item count is increased depends on the component. These values can be customized with LazyDataView.setItemCountEstimate(int) and LazyDataView.setItemCountEstimateIncrease(int) when the backend has a lot of items and faster scrolling down is desired.

      Specified by:
      setItemCountUnknown in interface LazyDataView<T>