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

Thomas Neidhart updated COLLECTIONS-442:
----------------------------------------

    Attachment: FluentIterator.java

I have reworked the patch a bit, see the attached file. My rationale was as 
follows:

 * reuse existing code as much as possible
 * use real classes instead of interfaces to avoid problems with breaking 
compatibility when extending later on

An example how to use the interface:

{noformat}
public class MyTest {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(2);
        list.add(3);
        
        FluentIterator<String> it =
            FluentIterator.<Integer>empty()
                          .andThen(list.iterator())
                          .dropIf(new Predicate<Integer>() {
                              public boolean evaluate(Integer object) {
                                  return object.intValue() < 2;
                              }
                          })
                          .unique()
                          .andThen(list.iterator())
                          .mapWith(new Transformer<Integer, String>() {
                              public String transform(Integer input) {
                                  return "[" + String.valueOf(input.intValue()) 
+ "]";
                              }
                          });
        
        System.out.println(it.toList());
    }
}
{noformat}

This prints

{noformat}
[[2], [3], [1], [2], [3], [2], [3]]
{noformat}

In the original patch, "andThen" behaved differently to the other composition 
methods in the sense, that only andThen returned the same object. All the other 
methods returned a new object.

This could be error-prone when people do things like this:

{noformat}
  it.andThen(...)
  it.filterKeep(...)
{noformat}

in fact, the "andThen" will update "it", while "filterKeep" will return a new 
object and leave "it" untouched. This is fine as long as you do method chaining 
but can lead to unexpected errors or behavior in other cases. 

So I decided to be consistent and always return the same object. The only 
exception is the "mapWith" method, which will always return a new object as the 
generic return type may change. 

I am not completely happy with the class name, so if somebody has a better idea?

Any comments are welcome, if the API is accepted we can still include it for 
the upcoming 4.0-alpha1 which I plan to release in 1-2 weeks.
                
> A set of enhanced iterator classes donated by the Apache Jena project
> ---------------------------------------------------------------------
>
>                 Key: COLLECTIONS-442
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-442
>             Project: Commons Collections
>          Issue Type: Improvement
>          Components: Iterator
>            Reporter: Claude Warren
>             Fix For: 4.0
>
>         Attachments: COLLECTIONS-442.tar.gz, FluentIterator.java
>
>
> A set of templated (Generic) iterators that add filtering, mapping, and 
> conversion to set or list collections.  Tests included.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to