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 org.jspecify.annotations.NonNull;
20 
21 /**
22  * Subclass of {@link Selection} exposing public support for mutating the underlying
23  * selection data. This is useful for clients of {@link SelectionTracker} that wish to
24  * manipulate a copy of selection data obtained via
25  * {@link SelectionTracker#copySelection(MutableSelection)}.
26  *
27  * <p>
28  * While the {@link Selection} class is not intrinsically immutable, it is not mutable
29  * by non-library code. Furthermore the value returned from {@link SelectionTracker#getSelection()}
30  * is a live view of the underlying selection, mutable by the library itself.
31  *
32  * <p>
33  * {@link MutableSelection} allows clients to obtain a mutable copy of the Selection
34  * state held by the selection library. This is useful in situations where a stable
35  * snapshot of the selection is required.
36  *
37  *
38  * <p><b>Example</b>
39  *
40  * <p>
41  * <pre>
42  * MutableSelection snapshot = new MutableSelection();
43  * selectionTracker.copySelection(snapshot);
44  *
45  * // Clear the user visible selection.
46  * selectionTracker.clearSelection();
47  * // tracker.getSelection().isEmpty() will be true.
48  * // shapshot has a copy of the previous selection.
49  * </pre>
50  *
51  * @see android.text.Selection
52  *
53  * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
54  */
55 public final class MutableSelection<K> extends Selection<K> {
56 
57     @Override
add(@onNull K key)58     public boolean add(@NonNull K key) {
59         return super.add(key);
60     }
61 
62     @Override
remove(@onNull K key)63     public boolean remove(@NonNull K key) {
64         return super.remove(key);
65     }
66 
67     @Override
copyFrom(@onNull Selection<K> source)68     public void copyFrom(@NonNull Selection<K> source) {
69         super.copyFrom(source);
70     }
71 
72     @Override
clear()73     public void clear() {
74         super.clear();
75     }
76 }
77