1 /*
2  * Copyright (C) 2015 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.tv.common.util;
18 
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.List;
25 
26 /** Static utilities for collections */
27 public class CollectionUtils {
28 
29     /**
30      * Returns an array with the arrays concatenated together.
31      *
32      * @see <a href="http://stackoverflow.com/a/784842/1122089">Stackoverflow answer</a> by <a
33      *     href="http://stackoverflow.com/users/40342/joachim-sauer">Joachim Sauer</a>
34      */
concatAll(T[] first, T[]... rest)35     public static <T> T[] concatAll(T[] first, T[]... rest) {
36         int totalLength = first.length;
37         for (T[] array : rest) {
38             totalLength += array.length;
39         }
40         T[] result = Arrays.copyOf(first, totalLength);
41         int offset = first.length;
42         for (T[] array : rest) {
43             System.arraycopy(array, 0, result, offset, array.length);
44             offset += array.length;
45         }
46         return result;
47     }
48 
49     /**
50      * Unions the two collections and returns the unified list.
51      *
52      * <p>The elements is not compared with hashcode() or equals(). Comparator is used for the
53      * equality check.
54      */
union( Collection<T> originals, Collection<T> toAdds, Comparator<T> comparator)55     public static <T> List<T> union(
56             Collection<T> originals, Collection<T> toAdds, Comparator<T> comparator) {
57         List<T> result = new ArrayList<>(originals);
58         Collections.sort(result, comparator);
59         List<T> resultToAdd = new ArrayList<>();
60         for (T toAdd : toAdds) {
61             if (Collections.binarySearch(result, toAdd, comparator) < 0) {
62                 resultToAdd.add(toAdd);
63             }
64         }
65         result.addAll(resultToAdd);
66         return result;
67     }
68 
69     /** Subtracts the elements from the original collection. */
subtract( Collection<T> originals, T[] toSubtracts, Comparator<T> comparator)70     public static <T> List<T> subtract(
71             Collection<T> originals, T[] toSubtracts, Comparator<T> comparator) {
72         List<T> result = new ArrayList<>(originals);
73         Collections.sort(result, comparator);
74         for (T toSubtract : toSubtracts) {
75             int index = Collections.binarySearch(result, toSubtract, comparator);
76             if (index >= 0) {
77                 result.remove(index);
78             }
79         }
80         return result;
81     }
82 
83     /** Returns {@code true} if the two specified collections have common elements. */
containsAny( Collection<T> c1, Collection<T> c2, Comparator<T> comparator)84     public static <T> boolean containsAny(
85             Collection<T> c1, Collection<T> c2, Comparator<T> comparator) {
86         List<T> contains = new ArrayList<>(c1);
87         Collections.sort(contains, comparator);
88         for (T iterate : c2) {
89             if (Collections.binarySearch(contains, iterate, comparator) >= 0) {
90                 return true;
91             }
92         }
93         return false;
94     }
95 }
96