Java Collection Join join(final Collection collection)

Here you can find the source of join(final Collection collection)

Description

This static method returns a String representing the the concatenation of the input Collection .

License

Open Source License

Parameter

Parameter Description
T the type of the Collection items to join
collection the Collection to concatenated into a String

Return

a representing the the concatenation of the input

Declaration

public static <T> String join(final Collection<T> collection) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**//  w w w .  j av a  2  s. c o m
     * This constant {@link String} represents the error message to use then an {@link IllegalArgumentException}
     * is thrown because the assert on <code>null</code> value check fails. 
     */
    private static final String ILLEGAL_ARGUMENT_EXCEPTION_MSG = "the paramter cannot be null: ";

    /**
     * This static method returns a {@link String} representing the the concatenation of the input
     * {@link Collection}.
     * 
     * @param <T> the type of the {@link Collection} items to join
     * @param collection the {@link Collection} to concatenated into a {@link String}
     * 
     * @return a {@link String} representing the the concatenation of the input {@link Collection}
     */
    public static <T> String join(final Collection<T> collection) {
        return join(collection, "");
    }

    /**
    * This static method returns a {@link String} representing the the concatenation of the input
    * {@link Collection} using the separator provided.
    * 
    * @param <T> the type of the {@link Collection} items to join
    * @param collection the {@link Collection} to concatenated into a {@link String}
    * @param separator the {@link String} representing the separator used to concatenate the items
    * of the input {@link Collection}
    * 
    * @return a {@link String} representing the the concatenation of the input {@link Collection} using the separator provided
    */
    public static <T> String join(final Collection<T> collection, final String separator) {
        assertNotNull(collection, "collection");
        assertNotNull(separator, "separator");

        StringBuilder b = new StringBuilder();

        boolean isFirst = true;
        for (T obj : collection) {
            if (!isFirst) {
                b.append(separator);
            } else {
                isFirst = false;
            }

            b.append(obj.toString());
        }

        return b.toString();
    }

    /**
     * This method verifies if the input argument is <code>null</code> or not.
     * If it's <code>null</code> an {@link IllegalArgumentException} is thrown.
     * 
     * @param value the value to check
     * @param param the name of the attribute storing the value to check
     * 
     * @throws IllegalArgumentException
     */
    public static <T> void assertNotNull(final T value, final String param) throws IllegalArgumentException {
        if (null == value) {
            throw new IllegalArgumentException(
                    ILLEGAL_ARGUMENT_EXCEPTION_MSG + ((null != param) ? param : "unknown"));
        }
    }
}

Related

  1. join(final Collection seq, final String delimiter)
  2. join(final Collection strs, final String delimiter)
  3. join(final Collection toJoin)
  4. join(final Collection values)
  5. join(final Collection values, final char separator)
  6. join(final Collection collection, final String delimiter)
  7. join(final Collection objs, final String delimiter)
  8. join(final Collection objs, String delimiter)
  9. join(final String d, final Collection c)