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.permissioncontroller.permission.utils;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Set;
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      * Return a singleton list containing the element, or an empty list if the element is
61      * {@code null}.
62      *
63      * @param <T> the class of the element
64      * @param element the element to be put into the list
65      *
66      * @return a singleton list containing the element, or an empty list if the element is
67      *         {@code null}.
68      */
69     @NonNull
singletonOrEmpty(@ullable T element)70     public static <T> List<T> singletonOrEmpty(@Nullable T element) {
71         return element != null ? Collections.singletonList(element) : Collections.emptyList();
72     }
73 
74     /**
75      * Returns whether a byte array is contained within a {@link Set} of byte arrays. Equality is
76      * not compared by reference, but by comparing the elements contained in the arrays.
77      *
78      * @param byteArrays a {@link Set} of byte arrays that will be searched
79      * @param otherByteArray byte array to be searched
80      * @return {@code true} if {@code byteArrays} contains a byte array with identical elements as
81      *         {@code otherByteArray}.
82      */
contains(@onNull Set<byte[]> byteArrays, @NonNull byte[] otherByteArray)83     public static boolean contains(@NonNull Set<byte[]> byteArrays,
84             @NonNull byte[] otherByteArray) {
85         for (byte[] byteArray : byteArrays) {
86             if (Arrays.equals(byteArray, otherByteArray)) {
87                 return true;
88             }
89         }
90         return false;
91     }
92 
93     /**
94      * Returns whether a {@link Set} of byte arrays is contained within a {@link Set} of byte arrays
95      * as a subset. Equality for arrays is not compared by reference, but by comparing the elements
96      * contained in the arrays.
97      *
98      * @param byteArrays a {@link Set} of byte arrays which will be checked as a superset
99      * @param otherByteArrays a {@link Set} of byte arrays which be checked as a subset
100      * @return {@code true} if {@code byteArrays} contains all the arrays in {@code
101      *     otherByteArrays}.
102      */
containsSubset( @onNull Set<byte[]> byteArrays, @NonNull Set<byte[]> otherByteArrays)103     public static boolean containsSubset(
104             @NonNull Set<byte[]> byteArrays, @NonNull Set<byte[]> otherByteArrays) {
105         for (byte[] byteArray : otherByteArrays) {
106             if (!contains(byteArrays, byteArray)) {
107                 return false;
108             }
109         }
110         return true;
111     }
112 }
113