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