1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.paging 18 19 import androidx.annotation.IntRange 20 21 /** 22 * Snapshot of data being presented by a 23 * [AsyncPagingDataDiffer][androidx.pagingAsyncPagingDataDiffer] or a 24 * [PagingDataAdapter][androidx.paging.PagingDataAdapter]. 25 */ 26 public class ItemSnapshotList<T>( 27 /** 28 * Number of placeholders before the presented [items], 0 if 29 * [enablePlaceholders][androidx.paging.PagingConfig.enablePlaceholders] is `false`. 30 */ 31 @IntRange(from = 0) public val placeholdersBefore: Int, 32 /** 33 * Number of placeholders after the presented [items], 0 if 34 * [enablePlaceholders][androidx.paging.PagingConfig.enablePlaceholders] is `false`. 35 */ 36 @IntRange(from = 0) public val placeholdersAfter: Int, 37 /** The presented data, excluding placeholders. */ 38 public val items: List<T> 39 ) : AbstractList<T?>() { 40 41 /** 42 * Size of [ItemSnapshotList] including placeholders. 43 * 44 * To get the size excluding placeholders, use [List.size] on [items] directly. 45 * 46 * @see items 47 */ 48 public override val size: Int 49 get() = placeholdersBefore + items.size + placeholdersAfter 50 51 /** 52 * Returns the item at [index], where [index] includes the position of placeholders. If [index] 53 * points to the position of a placeholder, `null` is returned. 54 * 55 * To get the size using an index excluding placeholders, use [List.size] on [items] directly. 56 * 57 * @throws IndexOutOfBoundsException if [index] < 0 or [index] > [size]. 58 */ getnull59 override fun get(index: Int): T? { 60 return when (index) { 61 in 0 until placeholdersBefore -> null 62 in placeholdersBefore until (placeholdersBefore + items.size) -> { 63 items[index - placeholdersBefore] 64 } 65 in (placeholdersBefore + items.size) until size -> null 66 else -> 67 throw IndexOutOfBoundsException( 68 "Illegal attempt to access index $index in ItemSnapshotList of size $size" 69 ) 70 } 71 } 72 } 73