Class DataCommunicator<T>

java.lang.Object
com.vaadin.flow.data.provider.DataCommunicator<T>
Type Parameters:
T - the bean type
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ComboBoxDataCommunicator, HierarchicalDataCommunicator, PagelessDataCommunicator

public class DataCommunicator<T> extends Object implements Serializable
DataProvider base class. This class is the base for all DataProvider communication implementations. It uses data generators (BiFunctions) to write JsonObjects representing each data object to be sent to the client-side.
Since:
1.0
See Also:
  • Field Details

    • DEFAULT_PAGE_INCREASE_COUNT

      public static final int DEFAULT_PAGE_INCREASE_COUNT
      See Also:
  • Constructor Details

    • DataCommunicator

      public DataCommunicator(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode)
      Creates a new instance.
      Parameters:
      dataGenerator - the data generator function
      arrayUpdater - array updater strategy
      dataUpdater - data updater strategy
      stateNode - the state node used to communicate for
    • DataCommunicator

      public DataCommunicator(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode, boolean fetchEnabled)
      Creates a new instance.

      Allows to setup whether the data communicator will ignore fetch and size queries to data provider until further configuration. This mode is useful when the component needs to postpone the calls to data provider until some event, i.e. dropdown open event of the combo box, but needs to configure the data communicator preliminary.

      Parameters:
      dataGenerator - the data generator function
      arrayUpdater - array updater strategy
      dataUpdater - data updater strategy
      stateNode - the state node used to communicate for
      fetchEnabled - if fetchEnabled is true then the data provider will be called to fetch the items and/or to get the items count until it's set to false
  • Method Details

    • setRequestedRange

      public void setRequestedRange(int start, int length)
      Sets the requested range of data to be sent.
      Parameters:
      start - the start of the requested range
      length - the end of the requested range
    • enablePushUpdates

      public void enablePushUpdates(Executor executor)
      Control whether DataCommunicator should push data updates to the component asynchronously or not. By default the executor service is not defined and updates are done synchronously. Setting to null will disable the feature.

      Note: This works only with Grid component. If set to true, Push needs to be enabled and set to PushMode.AUTOMATIC in order this to work.

      Parameters:
      executor - The Executor used for async updates.
    • reset

      public void reset()
      Resets all the data.

      It effectively resends all available data.

    • refresh

      public void refresh(T data)
      Informs the DataCommunicator that a data object has been updated.
      Parameters:
      data - updated data object; not null
    • confirmUpdate

      public void confirmUpdate(int updateId)
      Confirm update with the given updateId.
      Parameters:
      updateId - the update identifier
    • getDataProvider

      public DataProvider<T,?> getDataProvider()
      Gets the current data provider from this DataCommunicator.
      Returns:
      the data provider
    • setDataProvider

      public <F> SerializableConsumer<DataCommunicator.Filter<F>> setDataProvider(DataProvider<T,F> dataProvider, F initialFilter, boolean notifiesOnChange)
      Sets the current data provider for this DataCommunicator.

      The returned consumer can be used to set some other filter value that should be included in queries sent to the data provider. It is only valid until another data provider is set.

      This method also sets the data communicator to defined size - meaning that the given data provider is queried for size and previous size estimates are discarded.

      This method allows to define whether the data communicator notifies about changing of item count when it changes due to filtering.

      Type Parameters:
      F - the filter type
      Parameters:
      dataProvider - the data provider to set, not null
      initialFilter - the initial filter value to use, or null to not use any initial filter value
      notifiesOnChange - if true, then the data communicator will fire the item count change event as soon as filter change modifies the item count. If false, the item count change event won't be fired, even if the item count will be changed as a result of filtering.
      Returns:
      a consumer that accepts a new filter value to use
    • setDataProvider

      public <F> SerializableConsumer<F> setDataProvider(DataProvider<T,F> dataProvider, F initialFilter)
      Sets the current data provider for this DataCommunicator.

      The returned consumer can be used to set some other filter value that should be included in queries sent to the data provider. It is only valid until another data provider is set.

      This method also sets the data communicator to defined size - meaning that the given data provider is queried for size and previous size estimates are discarded.

      Type Parameters:
      F - the filter type
      Parameters:
      dataProvider - the data provider to set, not null
      initialFilter - the initial filter value to use, or null to not use any initial filter value
      Returns:
      a consumer that accepts a new filter value to use
    • getItemCount

      public int getItemCount()
      This is the latest DataProvider item count informed to the client or fetched from the DataProvider if client data has not been sent.
      Returns:
      count of available items
    • isItemActive

      public boolean isItemActive(T item)
      Returns whether the given item is part of the active items.
      Parameters:
      item - the item to check, not null
      Returns:
      true if item is active, false if not
    • 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.

      Call to the backend is triggered if the item for a requested index is not present in the cached active items.

      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
    • buildQuery

      public Query buildQuery(int offset, int limit)
      Generate a data query with component sorting and filtering.
      Parameters:
      offset - first index to fetch
      limit - fetched item count
      Returns:
      Query for component state
    • setPageSize

      public void setPageSize(int pageSize)
      Sets the page size that is used to fetch items. The queries to data provider are a multiple of the page size.
      Parameters:
      pageSize - the page size to set
    • getPageSize

      public int getPageSize()
      Returns the page size set to fetch items.
      Returns:
      the page size
    • setCountCallback

      public void setCountCallback(CallbackDataProvider.CountCallback<T,?> countCallback)
      Sets the size callback to be used and switches the component to exact row count. The new count will be used after this roundtrip.
      Parameters:
      countCallback - the size callback to use
    • setItemCountEstimate

      public void setItemCountEstimate(int itemCountEstimate)
      Sets the item count estimate to use and switches component to undefined size. Any previously set count callback is cleared. The new estimate is applied if the actual count has not been discovered and if the estimate is greater than the number of requested items. Otherwise it is not applied until there has been a reset.

      NOTE: setting item count estimate that is less than two pages (set with setPageSize(int)) can cause extra requests initially or after a reset.

      Parameters:
      itemCountEstimate - the item count estimate to be used
    • getItemCountEstimate

      public int getItemCountEstimate()
      Gets the item count estimate used.
      Returns:
      the item count estimate used
    • setItemCountEstimateIncrease

      public void setItemCountEstimateIncrease(int itemCountEstimateIncrease)
      Sets the item count estimate increase to use and switches the component to undefined size if not yet used. Any previously set count callback is cleared. The step is used the next time that the count is adjusted. NOTE: the increase should be greater than the setPageSize(int) or it may cause bad performance.
      Parameters:
      itemCountEstimateIncrease - the item count estimate step to use
    • getItemCountEstimateIncrease

      public int getItemCountEstimateIncrease()
      Gets the item count estimate increase used.
      Returns:
      the item count estimate increase
    • setDefinedSize

      public void setDefinedSize(boolean definedSize)
      Changes between defined and undefined size and clears any previously set count callback. Calling with value true will use the DataProvider.size(Query) for getting the size. Calling with false will use whatever has been set with setItemCountEstimate(int) and increase the count when needed with setItemCountEstimateIncrease(int).
      Parameters:
      definedSize - true for defined size, false for undefined size
    • isDefinedSize

      public boolean isDefinedSize()
      Returns whether defined or undefined size is used.
      Returns:
      true for defined size, false for undefined size
    • getKeyMapper

      public DataKeyMapper<T> getKeyMapper()
      Gets the DataKeyMapper used by this DataCommunicator. Key mapper can be used to map keys sent to the client-side back to their respective data objects.
      Returns:
      key mapper
    • setKeyMapper

      protected void setKeyMapper(DataKeyMapper<T> keyMapper)
      Sets the DataKeyMapper used in this DataCommunicator. Key mapper can be used to map keys sent to the client-side back to their respective data objects.
      Parameters:
      keyMapper - the keyMapper
    • setInMemorySorting

      public void setInMemorySorting(SerializableComparator<T> comparator)
      Sets the Comparator to use with in-memory sorting.
      Parameters:
      comparator - comparator used to sort data
    • getInMemorySorting

      public SerializableComparator<T> getInMemorySorting()
      Returns the Comparator to use with in-memory sorting.
      Returns:
      comparator used to sort data
    • setBackEndSorting

      public void setBackEndSorting(List<QuerySortOrder> sortOrder)
      Sets the QuerySortOrders to use with backend sorting.
      Parameters:
      sortOrder - list of sort order information to pass to a query
    • getBackEndSorting

      public List<QuerySortOrder> getBackEndSorting()
      Returns the QuerySortOrder to use with backend sorting.
      Returns:
      an unmodifiable list of sort order information to pass to a query
    • isPagingEnabled

      public boolean isPagingEnabled()
      Returns whether paged queries are enabled or not.

      When the paged queries are supported, the Query.getPage() and Query.getPageSize() can be used to fetch items from the paged repositories. Otherwise, one should use Query.getOffset() and Query.getLimit(). Paged queries are enabled by default.

      Returns:
      true for paged queries, false for offset/limit queries
      See Also:
    • setPagingEnabled

      public void setPagingEnabled(boolean pagingEnabled)
      Sets whether paged queries or offset/limit queries will be used.
      Parameters:
      pagingEnabled - true for paged queries, false for offset/limit queries
    • isFetchEnabled

      public boolean isFetchEnabled()
      Returns whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.
      Returns:
      true if the calls to data provider are enabled, false otherwise
    • setFetchEnabled

      public void setFetchEnabled(boolean fetchEnabled)
      Sets whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.

      One may need to disable the data provider calls in order to configure the data communicator and to postpone these calls until some event, i.e. dropdown open event of the combo box.

      This sets to true by default.

      Parameters:
      fetchEnabled - if true then the calls to data provider are enabled, otherwise the data provider won't be called to fetch the items.
    • getDataProviderSize

      public int getDataProviderSize()
      Getter method for determining the item count of the data.

      This method should be used only with defined size, i.e. when isDefinedSize() returns true.

      Can be overridden by a subclass that uses a specific type of DataProvider and/or query.

      Returns:
      the size of data provider with current filter
    • getFilter

      protected Object getFilter()
      Get the object used for filtering in this data communicator.
      Returns:
      the filter object of this data communicator
    • fetchFromProvider

      protected Stream<T> fetchFromProvider(int offset, int limit)
      Fetches a list of items from the DataProvider.

      NOTE: the limit parameter shows how many items the client wants to fetch, but the actual number of results may be greater, and vary from 0 to pages * pageSize.

      Parameters:
      offset - the starting index of the range
      limit - the desired number of results
      Returns:
      the list of items in given range
    • handleDataRefreshEvent

      protected void handleDataRefreshEvent(DataChangeEvent.DataRefreshEvent<T> event)
    • getPassivatedKeys

      protected Set<String> getPassivatedKeys(Set<String> oldActive)