1/*
2 * Copyright 2018 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
17package androidx.paging;
18
19import androidx.annotation.NonNull;
20import androidx.annotation.RestrictTo;
21import androidx.annotation.WorkerThread;
22
23import java.util.Collections;
24import java.util.List;
25
26// NOTE: Room 1.0 depends on this class, so it should not be removed until
27// we can require a version of Room that uses PositionalDataSource directly
28/**
29 * @param <T> Type loaded by the TiledDataSource.
30 *
31 * @deprecated Use {@link PositionalDataSource}
32 * @hide
33 */
34@SuppressWarnings("DeprecatedIsStillUsed")
35@Deprecated
36@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
37public abstract class TiledDataSource<T> extends PositionalDataSource<T> {
38
39    @WorkerThread
40    public abstract int countItems();
41
42    @Override
43    boolean isContiguous() {
44        return false;
45    }
46
47    @WorkerThread
48    public abstract List<T> loadRange(int startPosition, int count);
49
50    @Override
51    public void loadInitial(@NonNull LoadInitialParams params,
52            @NonNull LoadInitialCallback<T> callback) {
53        int totalCount = countItems();
54        if (totalCount == 0) {
55            callback.onResult(Collections.<T>emptyList(), 0, 0);
56            return;
57        }
58
59        // bound the size requested, based on known count
60        final int firstLoadPosition = computeInitialLoadPosition(params, totalCount);
61        final int firstLoadSize = computeInitialLoadSize(params, firstLoadPosition, totalCount);
62
63        // convert from legacy behavior
64        List<T> list = loadRange(firstLoadPosition, firstLoadSize);
65        if (list != null && list.size() == firstLoadSize) {
66            callback.onResult(list, firstLoadPosition, totalCount);
67        } else {
68            // null list, or size doesn't match request
69            // The size check is a WAR for Room 1.0, subsequent versions do the check in Room
70            invalidate();
71        }
72    }
73
74    @Override
75    public void loadRange(@NonNull LoadRangeParams params,
76            @NonNull LoadRangeCallback<T> callback) {
77        List<T> list = loadRange(params.startPosition, params.loadSize);
78        if (list != null) {
79            callback.onResult(list);
80        } else {
81            invalidate();
82        }
83    }
84}
85