com.vaadin.client.data.

Class AbstractRemoteDataSource<T>

  • Type Parameters:

    T - the row type

    All Implemented Interfaces:

    DataSource<T>

    Direct Known Subclasses:

    RpcDataSourceConnector.RpcDataSource

    public abstract class AbstractRemoteDataSource<T>
    extends Object
    implements DataSource<T>

    Base implementation for data sources that fetch data from a remote system. This class takes care of caching data and communicating with the data source user. An implementation of this class should override requestRows(int, int, RequestRowsCallback) to trigger asynchronously loading of data and then pass the loaded data into the provided callback.

    Since:

    7.4

    Author:

    Vaadin Ltd

    • Constructor Detail

      • AbstractRemoteDataSource

        public AbstractRemoteDataSource()
    • Method Detail

      • pinHandle

        protected void pinHandle​(AbstractRemoteDataSource.RowHandleImpl handle)

        Pins a row with given handle. This function can be overridden to do specific logic related to pinning rows.

        Parameters:

        handle - row handle to pin

      • ensureAvailability

        public void ensureAvailability​(int firstRowIndex,
                                       int numberOfRows)

        Description copied from interface: DataSource

        Informs the data source that data for the given range is needed. A data source only has one active region at a time, so calling this method discards the previously set range.

        This method triggers lazy loading of data if necessary. The change handler registered using DataSource.setDataChangeHandler(DataChangeHandler) is informed when new data has been loaded.

        After any possible lazy loading and updates are done, the change handler is informed that new data is available.

        Specified by:

        ensureAvailability in interface DataSource<T>

        Parameters:

        firstRowIndex - the index of the first needed row

        numberOfRows - the number of needed rows

      • getRequestedAvailability

        public Range getRequestedAvailability()

        Gets the row index range that was requested by the previous call to ensureAvailability(int, int).

        Returns:

        the requested availability range

      • isWaitingForData

        public boolean isWaitingForData()

        Checks whether this data source is currently waiting for more rows to become available.

        Specified by:

        isWaitingForData in interface DataSource<T>

        Returns:

        true if waiting for data; otherwise false

      • onDropFromCache

        @Deprecated
        protected void onDropFromCache​(int rowIndex)
        Deprecated.

        A hook that can be overridden to do something whenever a row has been dropped from the cache. DataSource no longer has anything in the given index.

        NOTE: This method has been replaced. Override onDropFromCache(int, Object) instead of this method.

        Parameters:

        rowIndex - the index of the dropped row

        Since:

        7.5.0

      • onDropFromCache

        protected void onDropFromCache​(int rowIndex,
                                       T removed)

        A hook that can be overridden to do something whenever a row has been dropped from the cache. DataSource no longer has anything in the given index.

        Parameters:

        rowIndex - the index of the dropped row

        removed - the removed row object

        Since:

        7.6

      • requestRows

        protected abstract void requestRows​(int firstRowIndex,
                                            int numberOfRows,
                                            AbstractRemoteDataSource.RequestRowsCallback<T> callback)

        Triggers fetching rows from the remote data source. The provided callback should be informed when the requested rows have been received.

        Parameters:

        firstRowIndex - the index of the first row to fetch

        numberOfRows - the number of rows to fetch

        callback - callback to inform when the requested rows are available

      • getRow

        public T getRow​(int rowIndex)

        Description copied from interface: DataSource

        Retrieves the data for the row at the given index. If the row data is not available, returns null.

        This method does not trigger loading of unavailable data. DataSource.ensureAvailability(int, int) should be used to signal what data will be needed.

        Specified by:

        getRow in interface DataSource<T>

        Parameters:

        rowIndex - the index of the row to retrieve data for

        Returns:

        data for the row; or null if no data is available

      • indexOf

        public int indexOf​(T row)

        Retrieves the index for given row object.

        Note: This method does not verify that the given row object exists at all in this DataSource.

        Parameters:

        row - the row object

        Returns:

        index of the row; or -1 if row is not available

      • setDataChangeHandler

        public void setDataChangeHandler​(DataChangeHandler dataChangeHandler)

        Description copied from interface: DataSource

        Sets a data change handler to inform when data is updated, added or removed.

        Specified by:

        setDataChangeHandler in interface DataSource<T>

        Parameters:

        dataChangeHandler - the data change handler

      • setRowData

        protected void setRowData​(int firstRowIndex,
                                  List<T> rowData)

        Informs this data source that updated data has been sent from the server.

        Parameters:

        firstRowIndex - the index of the first received row

        rowData - a list of rows, starting from firstRowIndex

      • removeRowData

        protected void removeRowData​(int firstRowIndex,
                                     int count)

        Informs this data source that the server has removed data.

        Parameters:

        firstRowIndex - the index of the first removed row

        count - the number of removed rows, starting from firstRowIndex

      • insertRowData

        protected void insertRowData​(int firstRowIndex,
                                     int count)

        Informs this data source that new data has been inserted from the server.

        Parameters:

        firstRowIndex - the destination index of the new row data

        count - the number of rows inserted

      • getCachedRange

        public Range getCachedRange()

        Gets the current range of cached rows

        Returns:

        the range of currently cached rows

      • setCacheStrategy

        public void setCacheStrategy​(CacheStrategy cacheStrategy)

        Sets the cache strategy that is used to determine how much data is fetched and cached.

        The new strategy is immediately used to evaluate whether currently cached rows should be discarded or new rows should be fetched.

        Parameters:

        cacheStrategy - a cache strategy implementation, not null

      • getRowKey

        public abstract Object getRowKey​(T row)

        Gets a stable key for the row object.

        This method is a workaround for the fact that there is no means to force proper implementations for Object.hashCode() and Object.equals(Object) methods.

        Since the same row object will be created several times for the same logical data, the DataSource needs a mechanism to be able to compare two objects, and figure out whether or not they represent the same data. Even if all the fields of an entity would be changed, it still could represent the very same thing (say, a person changes all of her names.)

        A very usual and simple example what this could be, is an unique ID for this object that would also be stored in a database.

        Parameters:

        row - the row object for which to get the key

        Returns:

        a non-null object that uniquely and consistently represents the row object

      • size

        public int size()

        Description copied from interface: DataSource

        Returns the number of rows in the data source.

        Specified by:

        size in interface DataSource<T>

        Returns:

        the current size of the data source

      • resetDataAndSize

        protected void resetDataAndSize​(int newSize)

        Updates the size, discarding all cached data. This method is used when the size of the container is changed without any information about the structure of the change. In this case, all cached data is discarded to avoid cache offset issues.

        If you have information about the structure of the change, use insertRowData(int, int) or removeRowData(int, int) to indicate where the inserted or removed rows are located.

        Parameters:

        newSize - the new size of the container

      • indexOfKey

        protected int indexOfKey​(Object rowKey)
      • isPinned

        protected boolean isPinned​(T row)
      • canFetchData

        protected boolean canFetchData()

        Checks if it is possible to currently fetch data from the remote data source.

        Returns:

        true if it is ok to try to fetch data, false if it is known that fetching data will fail and should not be tried right now.

        Since:

        7.7.2