Gonçalo Marques created COLLECTIONS-550:
-------------------------------------------

             Summary: Provide a simple way for creating an arbitrary String 
representation of a given Iterable
                 Key: COLLECTIONS-550
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-550
             Project: Commons Collections
          Issue Type: New Feature
          Components: Collection
    Affects Versions: 4.0
            Reporter: Gonçalo Marques
            Priority: Minor


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}}):

{code}
static <C> String toString(Iterable<C> iterable, Transformer<C, String> 
transformer);
{code}

Consider the following illustrative class:

{code}
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;
  }

}
{code}

One could transform an {{Iterable}} containing elements of type {{SomeClass}} 
into a client provided {{String}} representation by calling:

{code}
// list contains elements of type SomeClass
String result = CollectionUtils.transform(list, new Transformer<SomeClass, 
String>() {
  @Override
  public String transform(SomeClass someClass) {
    return someClass.getPropertyTwo();
  }
});

// Will print "propertyTwoA, propertyTwoB"
System.out.println(result);

result = CollectionUtils.transform(list, new Transformer<SomeClass, String>() {
  @Override
  public String transform(SomeClass someClass) {
    return someClass.getPropertyOne() + someClass.getPropertyTwo();
  }
});

// Will print propertyOneApropertyTwoA, propertyOneBpropertyTwoB
System.out.println(result);

{code}




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to