Joins the elements of the provided Collection
into
* a single String containing the provided elements.
No delimiter is added before or after the list. Null objects or empty * strings within the iteration are represented by empty strings.
* *See the examples here: {@link #join(Object[],char)}.
* * @param collection theCollection
of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, null
if null iterator input
* @since 2.3
*/
public static String join(Collection collection, char separator) {
if (collection == null) {
return null;
}
return join(collection.iterator(), separator);
}
/**
* Joins the elements of the provided Iterator
into
* a single String containing the provided elements.
No delimiter is added before or after the list. Null objects or empty * strings within the iteration are represented by empty strings.
* *See the examples here: {@link #join(Object[],char)}.
* * @param iterator theIterator
of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, null
if null iterator input
* @since 2.0
*/
public static String join(Iterator iterator, char separator) {
// handle null, zero and one elements before building a buffer
if (iterator == null) {
return null;
}
if (!iterator.hasNext()) {
return "";
}
Object first = iterator.next();
if (!iterator.hasNext()) {
return toString(first);
}
// two or more elements
StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
if (first != null) {
buf.append(first);
}
while (iterator.hasNext()) {
buf.append(separator);
Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
}
return buf.toString();
}
// ToString
//-----------------------------------------------------------------------
/**
* Gets the toString
of an Object
returning
* an empty string ("") if null
input.
* ObjectUtils.toString(null) = "" * ObjectUtils.toString("") = "" * ObjectUtils.toString("bat") = "bat" * ObjectUtils.toString(Boolean.TRUE) = "true" ** * @see StringUtils#defaultString(String) * @see String#valueOf(Object) * @param obj the Object to
toString
, may be null
* @return the passed in Object's toString, or nullStr if null
input
* @since 2.0
*/
public static String toString(Object obj) {
return obj == null ? "" : obj.toString();
}
}