Details
-
New Feature
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
4.0
Description
Create an utility method that returns an arbitrary String representation of a given Iterable, where for each Iterable element one may require a String representation that is different from the element's own toString() result.
Additionally the client may also provide an optional delimiter, where the default one could be the common ", ".
The transformation of each Iterable element into an arbitrary String could be implemented by the means of a Transformer.
Example (illustrative method in CollectionUtils):
static <C> String toString(Iterable<C> iterable, Transformer<C, String> transformer);
Consider the following illustrative class:
class SomeClass { private final String propertyOne; private final String propertyTwo; public SomeClass(String propertyOne, String propertyTwo) { this.propertyOne = propertyOne; this.propertyTwo = propertyTwo; } public String getPropertyOne() { return propertyOne; } public String getPropertyTwo() { return propertyTwo; } @Override public String toString() { return propertyOne; } }
One could transform an Iterable containing elements of type SomeClass into a client provided String representation by calling:
// list contains elements of type SomeClass String result = CollectionUtils.toString(list, new Transformer<SomeClass, String>() { @Override public String transform(SomeClass someClass) { return someClass.getPropertyTwo(); } }); // Will print "propertyTwoA, propertyTwoB" System.out.println(result); result = CollectionUtils.toString(list, new Transformer<SomeClass, String>() { @Override public String transform(SomeClass someClass) { return someClass.getPropertyOne() + someClass.getPropertyTwo(); } }); // Will print propertyOneApropertyTwoA, propertyOneBpropertyTwoB System.out.println(result);