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