mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +01:00
Improve StringUtils.collectionToString()
Use generics, and iterator and append(char) instead of append(String).
This commit is contained in:
parent
57cc3aa3f8
commit
734695c80e
1 changed files with 16 additions and 9 deletions
|
@ -19,6 +19,7 @@ package org.jivesoftware.smack.util;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -221,16 +222,22 @@ public class StringUtils {
|
||||||
return cs.length() == 0;
|
return cs.length() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String collectionToString(Collection<String> collection) {
|
/**
|
||||||
StringBuilder sb = new StringBuilder();
|
* Transform a collection of CharSequences to a whitespace delimited String.
|
||||||
for (String s : collection) {
|
*
|
||||||
sb.append(s);
|
* @param collection the collection to transform.
|
||||||
sb.append(" ");
|
* @return a String with all the elements of the collection.
|
||||||
|
*/
|
||||||
|
public static String collectionToString(Collection<? extends CharSequence> collection) {
|
||||||
|
StringBuilder sb = new StringBuilder(collection.size() * 20);
|
||||||
|
for (Iterator<? extends CharSequence> it = collection.iterator(); it.hasNext();) {
|
||||||
|
CharSequence cs = it.next();
|
||||||
|
sb.append(cs);
|
||||||
|
if (it.hasNext()) {
|
||||||
|
sb.append(' ');
|
||||||
}
|
}
|
||||||
String res = sb.toString();
|
}
|
||||||
// Remove the trailing whitespace
|
return sb.toString();
|
||||||
res = res.substring(0, res.length() - 1);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String returnIfNotEmptyTrimmed(String string) {
|
public static String returnIfNotEmptyTrimmed(String string) {
|
||||||
|
|
Loading…
Reference in a new issue