| 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.recyclerview.widget; |
| 18 | |
| 19 | import androidx.annotation.NonNull; |
| 20 | import androidx.annotation.Nullable; |
| 21 | import androidx.annotation.RestrictTo; |
| 22 | |
| 23 | import java.util.concurrent.Executor; |
| 24 | import java.util.concurrent.Executors; |
| 25 | |
| 26 | /** |
| 27 | * Configuration object for {@link ListAdapter}, {@link AsyncListDiffer}, and similar |
| 28 | * background-thread list diffing adapter logic. |
| 29 | * <p> |
| 30 | * At minimum, defines item diffing behavior with a {@link DiffUtil.ItemCallback}, used to compute |
| 31 | * item differences to pass to a RecyclerView adapter. |
| 32 | * |
| 33 | * @param <T> Type of items in the lists, and being compared. |
| 34 | */ |
| 35 | public final class AsyncDifferConfig<T> { |
| 36 | @Nullable |
| 37 | private final Executor mMainThreadExecutor; |
| 38 | @NonNull |
| 39 | private final Executor mBackgroundThreadExecutor; |
| 40 | @NonNull |
| 41 | private final DiffUtil.ItemCallback<T> mDiffCallback; |
| 42 | |
| 43 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
| 44 | AsyncDifferConfig( |
| 45 | @Nullable Executor mainThreadExecutor, |
| 46 | @NonNull Executor backgroundThreadExecutor, |
| 47 | @NonNull DiffUtil.ItemCallback<T> diffCallback) { |
| 48 | mMainThreadExecutor = mainThreadExecutor; |
| 49 | mBackgroundThreadExecutor = backgroundThreadExecutor; |
| 50 | mDiffCallback = diffCallback; |
| 51 | } |
| 52 | |
| 53 | /** @hide */ |
| 54 | @SuppressWarnings("WeakerAccess") |
| 55 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) |
| 56 | @Nullable |
| 57 | public Executor getMainThreadExecutor() { |
| 58 | return mMainThreadExecutor; |
| 59 | } |
| 60 | |
| 61 | @SuppressWarnings("WeakerAccess") |
| 62 | @NonNull |
| 63 | public Executor getBackgroundThreadExecutor() { |
| 64 | return mBackgroundThreadExecutor; |
| 65 | } |
| 66 | |
| 67 | @SuppressWarnings("WeakerAccess") |
| 68 | @NonNull |
| 69 | public DiffUtil.ItemCallback<T> getDiffCallback() { |
| 70 | return mDiffCallback; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Builder class for {@link AsyncDifferConfig}. |
| 75 | * |
| 76 | * @param <T> |
| 77 | */ |
| 78 | public static final class Builder<T> { |
| 79 | @Nullable |
| 80 | private Executor mMainThreadExecutor; |
| 81 | private Executor mBackgroundThreadExecutor; |
| 82 | private final DiffUtil.ItemCallback<T> mDiffCallback; |
| 83 | |
| 84 | public Builder(@NonNull DiffUtil.ItemCallback<T> diffCallback) { |
| 85 | mDiffCallback = diffCallback; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * If provided, defines the main thread executor used to dispatch adapter update |
| 90 | * notifications on the main thread. |
| 91 | * <p> |
| 92 | * If not provided, it will default to the main thread. |
| 93 | * |
| 94 | * @param executor The executor which can run tasks in the UI thread. |
| 95 | * @return this |
| 96 | * |
| 97 | * @hide |
| 98 | */ |
| 99 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) |
| 100 | @NonNull |
| 101 | public Builder<T> setMainThreadExecutor(Executor executor) { |
| 102 | mMainThreadExecutor = executor; |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * If provided, defines the background executor used to calculate the diff between an old |
| 108 | * and a new list. |
| 109 | * <p> |
| 110 | * If not provided, defaults to two thread pool executor, shared by all ListAdapterConfigs. |
| 111 | * |
| 112 | * @param executor The background executor to run list diffing. |
| 113 | * @return this |
| 114 | */ |
| 115 | @SuppressWarnings({"unused", "WeakerAccess"}) |
| 116 | @NonNull |
| 117 | public Builder<T> setBackgroundThreadExecutor(Executor executor) { |
| 118 | mBackgroundThreadExecutor = executor; |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Creates a {@link AsyncListDiffer} with the given parameters. |
| 124 | * |
| 125 | * @return A new AsyncDifferConfig. |
| 126 | */ |
| 127 | @NonNull |
| 128 | public AsyncDifferConfig<T> build() { |
| 129 | if (mBackgroundThreadExecutor == null) { |
| 130 | synchronized (sExecutorLock) { |
| 131 | if (sDiffExecutor == null) { |
| 132 | sDiffExecutor = Executors.newFixedThreadPool(2); |
| 133 | } |
| 134 | } |
| 135 | mBackgroundThreadExecutor = sDiffExecutor; |
| 136 | } |
| 137 | return new AsyncDifferConfig<>( |
| 138 | mMainThreadExecutor, |
| 139 | mBackgroundThreadExecutor, |
| 140 | mDiffCallback); |
| 141 | } |
| 142 | |
| 143 | // TODO: remove the below once supportlib has its own appropriate executors |
| 144 | private static final Object sExecutorLock = new Object(); |
| 145 | private static Executor sDiffExecutor = null; |
| 146 | } |
| 147 | } |
| 148 | |