T
- the row typepublic abstract class AbstractRemoteDataSource<T> extends Object implements DataSource<T>
requestRows(int, int, RequestRowsCallback)
to trigger asynchronously
loading of data and then pass the loaded data into the provided callback.Modifier and Type | Class and Description |
---|---|
static class |
AbstractRemoteDataSource.RequestRowsCallback<T>
Callback used by
requestRows(int, int, RequestRowsCallback)
to pass data to the underlying implementation when data has been fetched. |
protected class |
AbstractRemoteDataSource.RowHandleImpl |
DataSource.RowHandle<T>
Constructor and Description |
---|
AbstractRemoteDataSource() |
Modifier and Type | Method and Description |
---|---|
Registration |
addDataChangeHandler(DataChangeHandler dataChangeHandler)
Sets a data change handler to inform when data is updated, added or
removed.
|
protected boolean |
canFetchData()
Checks if it is possible to currently fetch data from the remote data
source.
|
protected void |
dropFromCache(Range range)
Drop the given range of rows from this data source's cache.
|
void |
ensureAvailability(int firstRowIndex,
int numberOfRows)
Informs the data source that data for the given range is needed.
|
Range |
getCachedRange()
Gets the current range of cached rows.
|
DataSource.RowHandle<T> |
getHandle(T row)
Gets a
DataSource.RowHandle of a row object in the cache. |
Range |
getRequestedAvailability()
Gets the row index range that was requested by the previous call to
ensureAvailability(int, int) . |
T |
getRow(int rowIndex)
Retrieves the data for the row at the given index.
|
abstract Object |
getRowKey(T row)
Gets a stable key for the row object.
|
int |
indexOf(T row)
Retrieves the index for given row object.
|
protected int |
indexOfKey(Object rowKey) |
protected void |
insertRowData(int firstRowIndex,
int count)
Informs this data source that new data has been inserted from the server.
|
protected boolean |
isPinned(T row) |
boolean |
isWaitingForData()
Checks whether this data source is currently waiting for more rows to
become available.
|
protected void |
onDropFromCache(int rowIndex)
Deprecated.
replaced by
onDropFromCache(int, Object) |
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.
|
protected void |
pinHandle(AbstractRemoteDataSource.RowHandleImpl handle)
Pins a row with given handle.
|
protected void |
removeRowData(int firstRowIndex,
int count)
Informs this data source that the server has removed data.
|
protected abstract void |
requestRows(int firstRowIndex,
int numberOfRows,
AbstractRemoteDataSource.RequestRowsCallback<T> callback)
Triggers fetching rows from the remote data source.
|
protected void |
resetDataAndSize(int newSize)
Updates the size, discarding all cached data.
|
void |
setCacheStrategy(CacheStrategy cacheStrategy)
Sets the cache strategy that is used to determine how much data is
fetched and cached.
|
protected void |
setRowData(int firstRowIndex,
List<T> rowData)
Informs this data source that updated data has been sent from the server.
|
void |
setTrackInvalidatedRows(boolean trackInvalidatedRows)
Sets whether or not to track invalidated rows inside
insertRowData(int, int) and use them to fill cache when
{setRowData(int, List) } is called. |
int |
size()
Returns the number of rows in the data source.
|
protected void |
unpinHandle(AbstractRemoteDataSource.RowHandleImpl handle)
Unpins a previously pinned row with given handle.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addDataChangeHandler
protected void pinHandle(AbstractRemoteDataSource.RowHandleImpl handle)
handle
- row handle to pinprotected void unpinHandle(AbstractRemoteDataSource.RowHandleImpl handle) throws IllegalStateException
handle
- row handle to unpinIllegalStateException
- if given row handle has not been pinned beforepublic void ensureAvailability(int firstRowIndex, int numberOfRows)
DataSource
This method triggers lazy loading of data if necessary. The change
handler registered using DataSource.addDataChangeHandler(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.
ensureAvailability
in interface DataSource<T>
firstRowIndex
- the index of the first needed rownumberOfRows
- the number of needed rowspublic Range getRequestedAvailability()
ensureAvailability(int, int)
.public boolean isWaitingForData()
isWaitingForData
in interface DataSource<T>
true
if waiting for data; otherwise
false
protected void dropFromCache(Range range)
range
- the range of rows to drop@Deprecated protected void onDropFromCache(int rowIndex)
onDropFromCache(int, Object)
NOTE: This method has been replaced. Override
onDropFromCache(int, Object)
instead of this method.
rowIndex
- the index of the dropped rowprotected void onDropFromCache(int rowIndex, T removed)
rowIndex
- the index of the dropped rowremoved
- the removed row objectprotected abstract void requestRows(int firstRowIndex, int numberOfRows, AbstractRemoteDataSource.RequestRowsCallback<T> callback)
firstRowIndex
- the index of the first row to fetchnumberOfRows
- the number of rows to fetchcallback
- callback to inform when the requested rows are availablepublic T getRow(int rowIndex)
DataSource
null
.
This method does not trigger loading of unavailable data.
DataSource.ensureAvailability(int, int)
should be used to signal what data
will be needed.
getRow
in interface DataSource<T>
rowIndex
- the index of the row to retrieve data fornull
if no data is availablepublic int indexOf(T row)
Note: This method does not verify that the given row object exists at all in this DataSource.
row
- the row object-1
if row is not availablepublic Registration addDataChangeHandler(DataChangeHandler dataChangeHandler)
DataSource
addDataChangeHandler
in interface DataSource<T>
dataChangeHandler
- the data change handlerprotected void setRowData(int firstRowIndex, List<T> rowData)
firstRowIndex
- the index of the first received rowrowData
- a list of rows, starting from firstRowIndex
protected void removeRowData(int firstRowIndex, int count)
firstRowIndex
- the index of the first removed rowcount
- the number of removed rows, starting from
firstRowIndex
protected void insertRowData(int firstRowIndex, int count)
firstRowIndex
- the destination index of the new row datacount
- the number of rows insertedpublic Range getCachedRange()
public void setCacheStrategy(CacheStrategy cacheStrategy)
The new strategy is immediately used to evaluate whether currently cached rows should be discarded or new rows should be fetched.
cacheStrategy
- a cache strategy implementation, not null
public DataSource.RowHandle<T> getHandle(T row) throws IllegalStateException
DataSource
DataSource.RowHandle
of a row object in the cache.getHandle
in interface DataSource<T>
row
- the row object for which to retrieve a row handlenull
row handle of the given row objectIllegalStateException
public abstract Object getRowKey(T row)
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.
row
- the row object for which to get the keypublic int size()
DataSource
size
in interface DataSource<T>
protected void resetDataAndSize(int newSize)
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.
newSize
- the new size of the containerprotected int indexOfKey(Object rowKey)
protected boolean isPinned(T row)
protected boolean canFetchData()
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.public void setTrackInvalidatedRows(boolean trackInvalidatedRows)
insertRowData(int, int)
and use them to fill cache when
{setRowData(int, List)
} is called.trackInvalidatedRows
- a boolean value specifying if to track invalidated rows or
not, default value true
Copyright © 2019 Vaadin Ltd. All rights reserved.