paging/runtime/src/main/java/androidx/paging/PagedStorageDiffHelper.java
static <T> void dispatchDiff(ListUpdateCallback callback,
            final PagedStorage<T> oldList,
            final PagedStorage<T> newList,
            final DiffUtil.DiffResult diffResult) {

        final int trailingOld = oldList.computeTrailingNulls();
        final int trailingNew = newList.computeTrailingNulls();
        final int leadingOld = oldList.computeLeadingNulls();
        final int leadingNew = newList.computeLeadingNulls();

        if (trailingOld == 0
                && trailingNew == 0
                && leadingOld == 0
                && leadingNew == 0) {
            // Simple case, dispatch & return
            diffResult.dispatchUpdatesTo(callback);
            return;
        }

        // First, remove or insert trailing nulls
        if (trailingOld > trailingNew) {
            int count = trailingOld - trailingNew;
            callback.onRemoved(oldList.size() - count, count);
        } else if (trailingOld < trailingNew) {
            callback.onInserted(oldList.size(), trailingNew - trailingOld);
        }

        // Second, remove or insert leading nulls
        if (leadingOld > leadingNew) {
            callback.onRemoved(0, leadingOld - leadingNew);
        } else if (leadingOld < leadingNew) {
            callback.onInserted(0, leadingNew - leadingOld);
        }

        // apply the diff, with an offset if needed
        if (leadingNew != 0) {
            diffResult.dispatchUpdatesTo(new OffsettingListUpdateCallback(leadingNew, callback));
        } else {
            diffResult.dispatchUpdatesTo(callback);
        }
    }
/**
     * TODO: improve diffing logic
     *
     * This function currently does a naive diff, assuming null does not become an item, and vice
     * versa (so it won't dispatch onChange events for these). It's similar to passing a list with
     * leading/trailing nulls in the beginning / end to DiffUtil, but dispatches the remove/insert
     * for changed nulls at the beginning / end of the list.
     *
     * Note: if lists mutate between diffing the snapshot and dispatching the diff here, then we
     * handle this by passing the snapshot to the callback, and dispatching those changes
     * immediately after dispatching this diff.
     */
improve diffing logic
This function currently does a naive diff, assuming null does not become an item, and vice
versa (so it won't dispatch onChange events for these). It's similar to passing a list with
leading/trailing nulls in the beginning / end to DiffUtil, but dispatches the remove/insert
for changed nulls at the beginning / end of the list.

if lists mutate between diffing the snapshot and dispatching the diff here, then we
handle this by passing the snapshot to the callback, and dispatching those changes
immediately after dispatching this diff.
improve diffing logic This function currently does a naive diff assuming null does not become an item and vice versa ( so it won ' t dispatch onChange events for these ) . It ' s similar to passing a list with leading / trailing nulls in the beginning / end to DiffUtil but dispatches the remove / insert for changed nulls at the beginning / end of the list . if lists mutate between diffing the snapshot and dispatching the diff here then we handle this by passing the snapshot to the callback and dispatching those changes immediately after dispatching this diff .
static <T> void dispatchDiff(ListUpdateCallback callback,
            final PagedStorage<T> oldList,
            final PagedStorage<T> newList,
            final DiffUtil.DiffResult diffResult) {
        final int trailingOld = oldList.computeTrailingNulls();
        final int trailingNew = newList.computeTrailingNulls();
        final int leadingOld = oldList.computeLeadingNulls();
        final int leadingNew = newList.computeLeadingNulls();
        if (trailingOld == 0
                && trailingNew == 0
                && leadingOld == 0
                && leadingNew == 0) {
            diffResult.dispatchUpdatesTo(callback);
            return;
        }
        if (trailingOld > trailingNew) {
            int count = trailingOld - trailingNew;
            callback.onRemoved(oldList.size() - count, count);
        } else if (trailingOld < trailingNew) {
            callback.onInserted(oldList.size(), trailingNew - trailingOld);
        }
        if (leadingOld > leadingNew) {
            callback.onRemoved(0, leadingOld - leadingNew);
        } else if (leadingOld < leadingNew) {
            callback.onInserted(0, leadingNew - leadingOld);
        }
        if (leadingNew != 0) {
            diffResult.dispatchUpdatesTo(new OffsettingListUpdateCallback(leadingNew, callback));
        } else {
            diffResult.dispatchUpdatesTo(callback);
        }
    }
static < T > void dispatchDiff ( ListUpdateCallback callback , final PagedStorage < T > oldList , final PagedStorage < T > newList , final DiffUtil . DiffResult diffResult ) { final int trailingOld = oldList . computeTrailingNulls ( ) ; final int trailingNew = newList . computeTrailingNulls ( ) ; final int leadingOld = oldList . computeLeadingNulls ( ) ; final int leadingNew = newList . computeLeadingNulls ( ) ; if ( trailingOld == 0 && trailingNew == 0 && leadingOld == 0 && leadingNew == 0 ) { diffResult . dispatchUpdatesTo ( callback ) ; return ; } if ( trailingOld > trailingNew ) { int count = trailingOld - trailingNew ; callback . onRemoved ( oldList . size ( ) - count , count ) ; } else if ( trailingOld < trailingNew ) { callback . onInserted ( oldList . size ( ) , trailingNew - trailingOld ) ; } if ( leadingOld > leadingNew ) { callback . onRemoved ( 0 , leadingOld - leadingNew ) ; } else if ( leadingOld < leadingNew ) { callback . onInserted ( 0 , leadingNew - leadingOld ) ; } if ( leadingNew != 0 ) { diffResult . dispatchUpdatesTo ( new OffsettingListUpdateCallback ( leadingNew , callback ) ) ; } else { diffResult . dispatchUpdatesTo ( callback ) ; } }
TODO: improve diffing logic
 This function currently does a naive diff, assuming null does not become an item, and vice
 versa (so it won't dispatch onChange events for these).
TODO : improve diffing logic This function currently does a naive diff assuming null does not become an item and vice versa ( so it won ' t dispatch onChange events for these ) .
// Simple case, dispatch & return // First, remove or insert trailing nulls // Second, remove or insert leading nulls // apply the diff, with an offset if needed