Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 328330 - Utility method to join a collection of strings
Summary: Utility method to join a collection of strings
Status: RESOLVED FIXED
Alias: None
Product: Riena
Classification: RT
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: 3.0.0   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-21 05:53 EDT by Stephan Mann CLA
Modified: 2011-06-15 08:32 EDT (History)
1 user (show)

See Also:


Attachments
Patch for StringUtils (2.02 KB, patch)
2010-10-21 05:54 EDT, Stephan Mann CLA
steffen.kriese: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Mann CLA 2010-10-21 05:53:18 EDT
I'm missing a vital method in the otherwise very handy StringUtils class. The attached patch adds a join method that joins the string representation of a collection of objects to one continues string which (for me at least) is often needed to represent collections in the UI.
Comment 1 Stephan Mann CLA 2010-10-21 05:54:04 EDT
Created attachment 181377 [details]
Patch for StringUtils
Comment 2 Nobody - feel free to take it CLA 2010-10-21 06:33:25 EDT
I changed the implementation to 

/**
	 * Joins the string representations of an {@link Iterable} collection of
	 * objects to one continues string while separating the entities with the
	 * provided separator.
	 * <p>
	 * The string representation will be retrieved by String.valueOf()
	 * 
	 * @param iterable
	 *            The {@link Iterable} number of objects
	 * @param separator
	 * @return The joined String separated by the given separator
	 */
	public static String join(final Iterable<? extends Object> iterable, final CharSequence separator) {
		Iterator<? extends Object> oIt;
		if (iterable == null || (!(oIt = iterable.iterator()).hasNext())) {
			return ""; //$NON-NLS-1$
		}
		final StringBuilder sb = new StringBuilder(String.valueOf(oIt.next()));
		while (oIt.hasNext()) {
			sb.append(separator).append(oIt.next());
		}
		return sb.toString();
	}
	
	This way we habe null-handling and don´t create the StringBuilder if we don´t need to.
	
	Added StringUtilsTest#testJoin()
Comment 3 Nobody - feel free to take it CLA 2010-10-21 06:33:50 EDT
commit