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