1/*
2 * Copyright (C) 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 */
16package android.support.media.tv;
17
18import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
19
20import android.support.annotation.RestrictTo;
21
22import java.util.Arrays;
23
24/**
25 * Static utilities for collections
26 * @hide
27 */
28@RestrictTo(LIBRARY_GROUP)
29public class CollectionUtils {
30    /**
31     * Returns an array with the arrays concatenated together.
32     *
33     * @see <a href="http://stackoverflow.com/a/784842/1122089">Stackoverflow answer</a> by
34     *      <a href="http://stackoverflow.com/users/40342/joachim-sauer">Joachim Sauer</a>
35     */
36    public static <T> T[] concatAll(T[] first, T[]... rest) {
37        int totalLength = first.length;
38        for (T[] array : rest) {
39            totalLength += array.length;
40        }
41        T[] result = Arrays.copyOf(first, totalLength);
42        int offset = first.length;
43        for (T[] array : rest) {
44            System.arraycopy(array, 0, result, offset, array.length);
45            offset += array.length;
46        }
47        return result;
48    }
49}
50