diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java index 119c041570..3759a12820 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java @@ -44,6 +44,7 @@ package org.eclipse.jgit.util; import java.text.MessageFormat; +import java.util.Collection; import org.eclipse.jgit.JGitText; @@ -158,6 +159,49 @@ public final class StringUtils { } } + /** + * Join a collection of Strings together using the specified separator. + * + * @param parts + * Strings to join + * @param separator + * used to join + * @return a String with all the joined parts + */ + public static String join(Collection<String> parts, String separator) { + return StringUtils.join(parts, separator, separator); + } + + /** + * Join a collection of Strings together using the specified separator and a + * lastSeparator which is used for joining the second last and the last + * part. + * + * @param parts + * Strings to join + * @param separator + * separator used to join all but the two last elements + * @param lastSeparator + * separator to use for joining the last two elements + * @return a String with all the joined parts + */ + public static String join(Collection<String> parts, String separator, + String lastSeparator) { + StringBuilder sb = new StringBuilder(); + int i = 0; + int lastIndex = parts.size() - 1; + for (String part : parts) { + sb.append(part); + if (i == lastIndex - 1) { + sb.append(lastSeparator); + } else if (i != lastIndex) { + sb.append(separator); + } + i++; + } + return sb.toString(); + } + private StringUtils() { // Do not create instances } |