1 /*
2  * Copyright (C) 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 com.android.packageinstaller.permission.utils;
18 
19 import android.util.ArraySet;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 
28 /**
29  * Utility methods for dealing with {@link java.util.Collection}s.
30  */
31 public final class CollectionUtils {
32 
CollectionUtils()33     private CollectionUtils() {}
34 
35     /**
36      * Check whether a collection is {@code null} or empty.
37      *
38      * @param collection the collection to check
39      *
40      * @return whether the collection is {@code null} or empty
41      */
isEmpty(@ullable Collection<?> collection)42     public static boolean isEmpty(@Nullable Collection<?> collection) {
43         return collection == null || collection.isEmpty();
44     }
45 
46     /**
47      * Return the first element of a list, or {@code null} if the list is {@code null} or empty.
48      *
49      * @param <T> the class of the elements of the list
50      * @param list the list to get the first element
51      *
52      * @return the first element of the list, or {@code null} if the list is {@code null} or empty
53      */
54     @Nullable
firstOrNull(@ullable List<T> list)55     public static <T> T firstOrNull(@Nullable List<T> list) {
56         return !isEmpty(list) ? list.get(0) : null;
57     }
58 
59     /**
60      * Remove all values in the array set that do <b>not</b> exist in the given collection.
61      *
62      * @param <T> the class of the elements to retain and of the {@code ArraySet}
63      * @param arraySet the {@code ArraySet} whose elements are to be removed or retained
64      * @param valuesToRetain the values to be used to determine which elements to retain
65      *
66      * @return {@code true} if any values were removed from the array set, {@code false} otherwise.
67      *
68      * @see ArraySet#retainAll(java.util.Collection)
69      */
70     @SafeVarargs
retainAll(ArraySet<T> arraySet, T... valuesToRetain)71     public static <T> boolean retainAll(ArraySet<T> arraySet, T... valuesToRetain) {
72         boolean removed = false;
73         for (int i = arraySet.size() - 1; i >= 0; i--) {
74             if (!ArrayUtils.contains(valuesToRetain, arraySet.valueAt(i))) {
75                 arraySet.removeAt(i);
76                 removed = true;
77             }
78         }
79         return removed;
80     }
81 
82     /**
83      * Return a singleton list containing the element, or an empty list if the element is
84      * {@code null}.
85      *
86      * @param <T> the class of the element
87      * @param element the element to be put into the list
88      *
89      * @return a singleton list containing the element, or an empty list if the element is
90      *         {@code null}.
91      */
92     @NonNull
singletonOrEmpty(@ullable T element)93     public static <T> List<T> singletonOrEmpty(@Nullable T element) {
94         return element != null ? Collections.singletonList(element) : Collections.emptyList();
95     }
96 }
97