• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 package com.android.loganalysis.util;
17 
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23 
24 /**
25  * Utility methods for arrays
26  */
27 // TODO: Use libTF once this is copied over.
28 public class ArrayUtil {
29 
ArrayUtil()30     private ArrayUtil() {
31     }
32 
33     /**
34      * Build an array from the provided contents.
35      *
36      * <p>
37      * The resulting array will be the concatenation of <var>arrays</var> input arrays, in their
38      * original order.
39      * </p>
40      *
41      * @param arrays the arrays to concatenate
42      * @return the newly constructed array
43      */
buildArray(String[].... arrays)44     public static String[] buildArray(String[]... arrays) {
45         int length = 0;
46         for (String[] array : arrays) {
47             length += array.length;
48         }
49         String[] newArray = new String[length];
50         int offset = 0;
51         for (String[] array : arrays) {
52             System.arraycopy(array, 0, newArray, offset, array.length);
53             offset += array.length;
54         }
55         return newArray;
56     }
57 
58     /**
59      * Convert a varargs list/array to an {@link List}.  This is useful for building instances of
60      * {@link List} by hand.  Note that this differs from {@link java.util.Arrays#asList} in that
61      * the returned array is mutable.
62      *
63      * @param inputAry an array, or a varargs list
64      * @return a {@link List} instance with the identical contents
65      */
66     @SafeVarargs
list(T... inputAry)67     public static <T> List<T> list(T... inputAry) {
68         List<T> retList = new ArrayList<T>(inputAry.length);
69         for (T item : inputAry) {
70             retList.add(item);
71         }
72         return retList;
73     }
74 
internalJoin(String sep, Collection<Object> pieces)75     private static String internalJoin(String sep, Collection<Object> pieces) {
76         StringBuilder sb = new StringBuilder();
77         boolean skipSep = true;
78         Iterator<Object> iter = pieces.iterator();
79         while (iter.hasNext()) {
80             if (skipSep) {
81                 skipSep = false;
82             } else {
83                 sb.append(sep);
84             }
85 
86             Object obj = iter.next();
87             if (obj == null) {
88                 sb.append("null");
89             } else {
90                 sb.append(obj.toString());
91             }
92         }
93         return sb.toString();
94     }
95 
96     /**
97      * Turns a sequence of objects into a string, delimited by {@code sep}.  If a single
98      * {@code Collection} is passed, it is assumed that the elements of that Collection are to be
99      * joined.  Otherwise, wraps the passed {@link Object}(s) in a {@link List} and joins the
100      * generated list.
101      *
102      * @param sep the string separator to delimit the different output segments.
103      * @param pieces A {@link Collection} or a varargs {@code Array} of objects.
104      */
105     @SuppressWarnings("unchecked")
join(String sep, Object... pieces)106     public static String join(String sep, Object... pieces) {
107         if ((pieces.length == 1) && (pieces[0] instanceof Collection)) {
108             // Don't re-wrap the Collection
109             return internalJoin(sep, (Collection<Object>) pieces[0]);
110         } else {
111             return internalJoin(sep, Arrays.asList(pieces));
112         }
113     }
114 }
115