1 /*
2  * Copyright 2017 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.selection;
18 
19 import androidx.recyclerview.selection.SelectionTracker.SelectionPredicate;
20 
21 import org.jspecify.annotations.NonNull;
22 
23 /**
24  * Utility class for creating SelectionPredicate instances. Provides default
25  * implementations for common cases like "single selection" and "select anything".
26  */
27 public final class SelectionPredicates {
28 
SelectionPredicates()29     private SelectionPredicates() {}
30 
31     /**
32      * Returns a selection predicate that allows multiples items to be selected, without
33      * any restrictions on which items can be selected.
34      *
35      * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
36      * @return
37      */
createSelectAnything()38     public static <K> @NonNull SelectionPredicate<K> createSelectAnything() {
39         return new SelectionPredicate<K>() {
40             @Override
41             public boolean canSetStateForKey(@NonNull K key, boolean nextState) {
42                 return true;
43             }
44 
45             @Override
46             public boolean canSetStateAtPosition(int position, boolean nextState) {
47                 return true;
48             }
49 
50             @Override
51             public boolean canSelectMultiple() {
52                 return true;
53             }
54         };
55     }
56 
57     /**
58      * Returns a selection predicate that allows a single item to be selected, without
59      * any restrictions on which item can be selected.
60      *
61      * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
62      * @return
63      */
64     public static <K> @NonNull SelectionPredicate<K> createSelectSingleAnything() {
65         return new SelectionPredicate<K>() {
66             @Override
67             public boolean canSetStateForKey(@NonNull K key, boolean nextState) {
68                 return true;
69             }
70 
71             @Override
72             public boolean canSetStateAtPosition(int position, boolean nextState) {
73                 return true;
74             }
75 
76             @Override
77             public boolean canSelectMultiple() {
78                 return false;
79             }
80         };
81     }
82 }
83