| /* |
| * Copyright 2018 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package androidx.paging; |
| |
| import androidx.annotation.NonNull; |
| import androidx.annotation.RestrictTo; |
| import androidx.annotation.WorkerThread; |
| |
| import java.util.Collections; |
| import java.util.List; |
| |
| // NOTE: Room 1.0 depends on this class, so it should not be removed until |
| // we can require a version of Room that uses PositionalDataSource directly |
| /** |
| * @param <T> Type loaded by the TiledDataSource. |
| * |
| * @deprecated Use {@link PositionalDataSource} |
| * @hide |
| */ |
| @SuppressWarnings("DeprecatedIsStillUsed") |
| @Deprecated |
| @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| public abstract class TiledDataSource<T> extends PositionalDataSource<T> { |
| |
| @WorkerThread |
| public abstract int countItems(); |
| |
| @Override |
| boolean isContiguous() { |
| return false; |
| } |
| |
| @WorkerThread |
| public abstract List<T> loadRange(int startPosition, int count); |
| |
| @Override |
| public void loadInitial(@NonNull LoadInitialParams params, |
| @NonNull LoadInitialCallback<T> callback) { |
| int totalCount = countItems(); |
| if (totalCount == 0) { |
| callback.onResult(Collections.<T>emptyList(), 0, 0); |
| return; |
| } |
| |
| // bound the size requested, based on known count |
| final int firstLoadPosition = computeInitialLoadPosition(params, totalCount); |
| final int firstLoadSize = computeInitialLoadSize(params, firstLoadPosition, totalCount); |
| |
| // convert from legacy behavior |
| List<T> list = loadRange(firstLoadPosition, firstLoadSize); |
| if (list != null && list.size() == firstLoadSize) { |
| callback.onResult(list, firstLoadPosition, totalCount); |
| } else { |
| // null list, or size doesn't match request |
| // The size check is a WAR for Room 1.0, subsequent versions do the check in Room |
| invalidate(); |
| } |
| } |
| |
| @Override |
| public void loadRange(@NonNull LoadRangeParams params, |
| @NonNull LoadRangeCallback<T> callback) { |
| List<T> list = loadRange(params.startPosition, params.loadSize); |
| if (list != null) { |
| callback.onResult(list); |
| } else { |
| invalidate(); |
| } |
| } |
| } |