Here you can find the source of join(final Collection
Parameter | Description |
---|---|
T | the type of the Collection items to join |
collection | the Collection to concatenated into a String |
public static <T> String join(final Collection<T> collection)
//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")); } } }