• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.role.controller.util;
18 
19 import android.util.ArraySet;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 
24 import java.util.Arrays;
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      * Return the first element of a list, or {@code null} if the list is {@code null} or empty.
37      *
38      * @param <T> the class of the elements of the list
39      * @param list the list to get the first element
40      *
41      * @return the first element of the list, or {@code null} if the list is {@code null} or empty
42      */
43     @Nullable
firstOrNull(@ullable List<T> list)44     public static <T> T firstOrNull(@Nullable List<T> list) {
45         if (list == null || list.isEmpty()) {
46             return null;
47         }
48         return list.get(0);
49     }
50 
51     /**
52      * Remove all values in the array set that do <b>not</b> exist in the given collection.
53      *
54      * @param <T> the class of the elements to retain and of the {@code ArraySet}
55      * @param arraySet the {@code ArraySet} whose elements are to be removed or retained
56      * @param valuesToRetain the values to be used to determine which elements to retain
57      *
58      * @return {@code true} if any values were removed from the array set, {@code false} otherwise.
59      *
60      * @see ArraySet#retainAll(java.util.Collection)
61      */
62     @SafeVarargs
retainAll(ArraySet<T> arraySet, T... valuesToRetain)63     public static <T> boolean retainAll(ArraySet<T> arraySet, T... valuesToRetain) {
64         boolean removed = false;
65         List<T> valuesToRetainList = Arrays.asList(valuesToRetain);
66         for (int i = arraySet.size() - 1; i >= 0; i--) {
67             if (!valuesToRetainList.contains(arraySet.valueAt(i))) {
68                 arraySet.removeAt(i);
69                 removed = true;
70             }
71         }
72         return removed;
73     }
74 
75     /**
76      * Return a singleton list containing the element, or an empty list if the element is
77      * {@code null}.
78      *
79      * @param <T> the class of the element
80      * @param element the element to be put into the list
81      *
82      * @return a singleton list containing the element, or an empty list if the element is
83      *         {@code null}.
84      */
85     @NonNull
singletonOrEmpty(@ullable T element)86     public static <T> List<T> singletonOrEmpty(@Nullable T element) {
87         return element != null ? Collections.singletonList(element) : Collections.emptyList();
88     }
89 }
90