paging/common/src/main/java/androidx/paging/PositionalDataSource.java
public static int computeInitialLoadPosition(@NonNull LoadInitialParams params,
            int totalCount) {
        int position = params.requestedStartPosition;
        int initialLoadSize = params.requestedLoadSize;
        int pageSize = params.pageSize;

        int roundedPageStart = Math.round(position / pageSize) * pageSize;

        // maximum start pos is that which will encompass end of list
        int maximumLoadPage = ((totalCount - initialLoadSize + pageSize - 1) / pageSize) * pageSize;
        roundedPageStart = Math.min(maximumLoadPage, roundedPageStart);

        // minimum start position is 0
        roundedPageStart = Math.max(0, roundedPageStart);

        return roundedPageStart;
    }
/**
     * Helper for computing an initial position in
     * {@link #loadInitial(LoadInitialParams, LoadInitialCallback)} when total data set size can be
     * computed ahead of loading.
     * <p>
     * The value computed by this function will do bounds checking, page alignment, and positioning
     * based on initial load size requested.
     * <p>
     * Example usage in a PositionalDataSource subclass:
     * <pre>
     * class ItemDataSource extends PositionalDataSource&lt;Item> {
     *     private int computeCount() {
     *         // actual count code here
     *     }
     *
     *     private List&lt;Item> loadRangeInternal(int startPosition, int loadCount) {
     *         // actual load code here
     *     }
     *
     *     {@literal @}Override
     *     public void loadInitial({@literal @}NonNull LoadInitialParams params,
     *             {@literal @}NonNull LoadInitialCallback&lt;Item> callback) {
     *         int totalCount = computeCount();
     *         int position = computeInitialLoadPosition(params, totalCount);
     *         int loadSize = computeInitialLoadSize(params, position, totalCount);
     *         callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
     *     }
     *
     *     {@literal @}Override
     *     public void loadRange({@literal @}NonNull LoadRangeParams params,
     *             {@literal @}NonNull LoadRangeCallback&lt;Item> callback) {
     *         callback.onResult(loadRangeInternal(params.startPosition, params.loadSize));
     *     }
     * }</pre>
     *
     * @param params Params passed to {@link #loadInitial(LoadInitialParams, LoadInitialCallback)},
     *               including page size, and requested start/loadSize.
     * @param totalCount Total size of the data set.
     * @return Position to start loading at.
     *
     * @see #computeInitialLoadSize(LoadInitialParams, int, int)
     */
Helper for computing an initial position in
#loadInitial(LoadInitialParams, LoadInitialCallback) when total data set size can be
computed ahead of loading.

The value computed by this function will do bounds checking, page alignment, and positioning
based on initial load size requested.

Example usage in a PositionalDataSource subclass:

class ItemDataSource extends PositionalDataSource<Item> {
private int computeCount() {
actual count code here
}

private List<Item> loadRangeInternal(int startPosition, int loadCount) {
actual load code here
}

Override
public void loadInitial( NonNull LoadInitialParams params,
NonNull LoadInitialCallback<Item> callback) {
int totalCount = computeCount();
int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);
callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
}



@param params Params passed to  #loadInitial(LoadInitialParams, LoadInitialCallback),
including page size, and requested start/loadSize.
@param totalCount Total size of the data set.
@return Position to start loading at.


Helper for computing an initial position in #loadInitial ( LoadInitialParams LoadInitialCallback ) when total data set size can be computed ahead of loading . The value computed by this function will do bounds checking page alignment and positioning based on initial load size requested . Example usage in a PositionalDataSource subclass : class ItemDataSource extends PositionalDataSource<Item > { private int computeCount () { actual count code here } private List<Item > loadRangeInternal ( int startPosition int loadCount ) { actual load code here } Override public void loadInitial ( NonNull LoadInitialParams params NonNull LoadInitialCallback<Item > callback ) { int totalCount = computeCount () ; int position = computeInitialLoadPosition ( params totalCount ) ; int loadSize = computeInitialLoadSize ( params position totalCount ) ; callback . onResult ( loadRangeInternal ( position loadSize ) position totalCount ) ; } @param params Params passed to #loadInitial ( LoadInitialParams LoadInitialCallback ) including page size and requested start / loadSize . @param totalCount Total size of the data set . @return Position to start loading at .
public static int computeInitialLoadPosition(@NonNull LoadInitialParams params,
            int totalCount) {
        int position = params.requestedStartPosition;
        int initialLoadSize = params.requestedLoadSize;
        int pageSize = params.pageSize;
        int roundedPageStart = Math.round(position / pageSize) * pageSize;
        int maximumLoadPage = ((totalCount - initialLoadSize + pageSize - 1) / pageSize) * pageSize;
        roundedPageStart = Math.min(maximumLoadPage, roundedPageStart);
        roundedPageStart = Math.max(0, roundedPageStart);
        return roundedPageStart;
    }
public static int computeInitialLoadPosition ( @ NonNull LoadInitialParams params , int totalCount ) { int position = params . requestedStartPosition ; int initialLoadSize = params . requestedLoadSize ; int pageSize = params . pageSize ; int roundedPageStart = Math . round ( position / pageSize ) * pageSize ; int maximumLoadPage = ( ( totalCount - initialLoadSize + pageSize - 1 ) / pageSize ) * pageSize ; roundedPageStart = Math . min ( maximumLoadPage , roundedPageStart ) ; roundedPageStart = Math . max ( 0 , roundedPageStart ) ; return roundedPageStart ; }
Helper for computing an initial position in
 {@link #loadInitial(LoadInitialParams, LoadInitialCallback)} when total data set size can be
 computed ahead of loading.
Helper for computing an initial position in { @link #loadInitial ( LoadInitialParams LoadInitialCallback ) } when total data set size can be computed ahead of loading .
// maximum start pos is that which will encompass end of list // minimum start position is 0