Java Collection Join join(String delimiter, Collection parts)

Here you can find the source of join(String delimiter, Collection parts)

Description

join

License

Open Source License

Declaration

public static String join(String delimiter, Collection<?> parts) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from w  w w  . j a v  a2  s .c  o m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Collection;

public class Main {
    private static final String[] EMPTY_ARRAY = new String[0];

    /**
     * Concatenates the raw input of {@link Object}s separated by <code>delimiter</code>. On each object
     * {@link Object#toString()} is invoked.<br />
     * <code>null</code> values or those {@link Object#toString()} is empty are neglected.
     *
     * @param delimiter
     * @param values
     * @return never <code>null</code>, empty String in case no elements are appended
     * @since 3.8.1
     */
    public static String join(String delimiter, Object... parts) {
        if (parts == null || parts.length == 0) {
            return "";
        }
        boolean added = false;
        StringBuilder builder = new StringBuilder();
        for (Object o : parts) {
            if (o == null) {
                continue;
            }
            String s = o.toString();
            if (!isNullOrEmpty(s)) {
                if (added && delimiter != null) {
                    builder.append(delimiter);
                }
                builder.append(s);
                added = true;
            }
        }
        return builder.toString();
    }

    /**
     * @see #join(String, Object...)
     * @since 4.0.0
     */
    public static String join(String delimiter, Long[] parts) {
        return join(delimiter, (Object[]) parts);
    }

    /**
     * @see #join(String, Object...)
     * @since 3.8.1
     */
    public static String join(String delimiter, String[] parts) {
        return join(delimiter, (Object[]) parts);
    }

    /**
     * @see #join(String, Object...)
     * @since 4.0.0
     */
    public static String join(String delimiter, Collection<?> parts) {
        return join(delimiter, parts == null ? EMPTY_ARRAY : parts.toArray());
    }

    /**
     * Checks whether a given string is {@code null} or empty. A string is considered empty if it equals the empty string
     * "" (internally this method checks whether the string length is 0).
     *
     * @param s
     *          the string to be checked
     * @return {@code true} if {@code s} is {@code null} or equals the empty string, {@code false} otherwise
     */
    public static boolean isNullOrEmpty(CharSequence s) {
        return s == null || s.length() == 0;
    }

    public static int length(CharSequence s) {
        if (s == null) {
            return 0;
        } else {
            return s.length();
        }
    }
}

Related

  1. join(String delimiter, Collection objects)
  2. join(String delimiter, Collection objects)
  3. join(String delimiter, Collection a)
  4. join(String delimiter, Collection items)
  5. join(String delimiter, Collection objects)
  6. join(String delimiter, Collection items)
  7. join(String delimiter, Collection stringCollection)
  8. join(String delimiter, Collection strings)
  9. join(String delimiter, Collection values)