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 
17 package androidx.paging;
18 
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import androidx.annotation.IntDef;
22 import androidx.annotation.MainThread;
23 import androidx.annotation.NonNull;
24 
25 import java.lang.annotation.Retention;
26 import java.util.Collections;
27 import java.util.List;
28 
29 class PageResult<T> {
30     @SuppressWarnings("unchecked")
31     private static final PageResult INVALID_RESULT =
32             new PageResult(Collections.EMPTY_LIST, 0);
33 
34     @SuppressWarnings("unchecked")
getInvalidResult()35     static <T> PageResult<T> getInvalidResult() {
36         return INVALID_RESULT;
37     }
38 
39 
40     @Retention(SOURCE)
41     @IntDef({INIT, APPEND, PREPEND, TILE})
42     @interface ResultType {}
43 
44     static final int INIT = 0;
45 
46     // contiguous results
47     static final int APPEND = 1;
48     static final int PREPEND = 2;
49 
50     // non-contiguous, tile result
51     static final int TILE = 3;
52 
53     @NonNull
54     public final List<T> page;
55     @SuppressWarnings("WeakerAccess")
56     public final int leadingNulls;
57     @SuppressWarnings("WeakerAccess")
58     public final int trailingNulls;
59     @SuppressWarnings("WeakerAccess")
60     public final int positionOffset;
61 
PageResult(@onNull List<T> list, int leadingNulls, int trailingNulls, int positionOffset)62     PageResult(@NonNull List<T> list, int leadingNulls, int trailingNulls, int positionOffset) {
63         this.page = list;
64         this.leadingNulls = leadingNulls;
65         this.trailingNulls = trailingNulls;
66         this.positionOffset = positionOffset;
67     }
68 
PageResult(@onNull List<T> list, int positionOffset)69     PageResult(@NonNull List<T> list, int positionOffset) {
70         this.page = list;
71         this.leadingNulls = 0;
72         this.trailingNulls = 0;
73         this.positionOffset = positionOffset;
74     }
75 
76     @Override
toString()77     public String toString() {
78         return "Result " + leadingNulls
79                 + ", " + page
80                 + ", " + trailingNulls
81                 + ", offset " + positionOffset;
82     }
83 
isInvalid()84     public boolean isInvalid() {
85         return this == INVALID_RESULT;
86     }
87 
88     abstract static class Receiver<T> {
89         @MainThread
onPageResult(@esultType int type, @NonNull PageResult<T> pageResult)90         public abstract void onPageResult(@ResultType int type, @NonNull PageResult<T> pageResult);
91     }
92 }
93