001    /*
002     * Copyright (c) 2011, Cloudera, Inc. All Rights Reserved.
003     *
004     * Cloudera, Inc. licenses this file to you under the Apache License,
005     * Version 2.0 (the "License"). You may not use this file except in
006     * compliance with the License. You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
011     * CONDITIONS OF ANY KIND, either express or implied. See the License for
012     * the specific language governing permissions and limitations under the
013     * License.
014     */
015    package com.cloudera.lib.lang;
016    
017    import java.util.Collection;
018    
019    /**
020     * String related utilities.
021     */
022    public abstract class StringUtils {
023    
024      /**
025       * Flattens a collection elements to a string calling
026       * <code>toString()</code> on each element.
027       *
028       * @param collection collection elements to flatten.
029       * @param separator separator to use between elements.
030       * @return the flattened string representation of the collection.
031       */
032      public static String toString(Collection<?> collection, String separator) {
033        String toString = "L[null]";
034        if (collection != null) {
035          StringBuilder sb = new StringBuilder(collection.size() * 20);
036          String sep = "";
037          for (Object obj : collection) {
038            sb.append(sep).append(obj);
039            sep = separator;
040          }
041          toString = sb.toString();
042        }
043        return toString;
044      }
045    
046    }