commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <j...@apache.org>
Subject [jira] [Closed] (COLLECTIONS-550) Provide a simple way for creating an arbitrary String representation of a given Iterable
Date Fri, 27 Nov 2015 21:12:14 GMT

     [ https://issues.apache.org/jira/browse/COLLECTIONS-550?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Thomas Neidhart closed COLLECTIONS-550.
---------------------------------------

> 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
>              Labels: collection, util
>             Fix For: 4.1
>
>
> 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.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);
> {code}



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

Mime
View raw message