You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Andy Seaborne (JIRA)" <ji...@apache.org> on 2013/05/06 13:42:16 UTC
[jira] [Comment Edited] (COLLECTIONS-442) A set of enhanced
iterator classes donated by the Apache Jena project
[ https://issues.apache.org/jira/browse/COLLECTIONS-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13649668#comment-13649668 ]
Andy Seaborne edited comment on COLLECTIONS-442 at 5/6/13 11:41 AM:
--------------------------------------------------------------------
I like the style. Attached is another take on this.
The main class is {{Iter}} that provides two styles:
* A style like the FluentIterator style of method chaining.
* Static methods to provide short sequences to that one-step operations can be applied to regular iterators and iterables
Also includes a "PeekIterator" for looking one step ahead.
The function-application style is useful for short sequences; the chainign is better for longer sequences.
{noformat}
iter = Iter.removeNulls(iter) ;
{noformat}
Example of each style: (example.IterExample.java):
{noformat}
List<Integer> x = Arrays.asList(1,2,3,2,3) ;
// Chaining style
Iter<String> iter = Iter.iter(x)
.filter(new Filter<Integer>() {
@Override
public boolean accept(Integer item)
{
return item.intValue() >= 2 ;
}})
.distinct()
.append(x.iterator())
.map(new Transform<Integer,String>() {
@Override
public String convert(Integer item)
{
return "["+String.valueOf(item)+"]" ;
}}) ;
System.out.println(iter.toList());
{noformat}
and
{noformat}
List<Integer> x = Arrays.asList(1,2,3,2,3) ;
// Function application style.
Iterator<Integer> it = Iter.filter(x, new Filter<Integer>() {
@Override
public boolean accept(Integer item)
{
return item.intValue() >= 2 ;
}}) ;
it = Iter.distinct(it) ;
Iterator<String> its = Iter.map(it, new Transform<Integer,String>() {
@Override
public String convert(Integer item)
{
return "["+String.valueOf(item)+"]" ;
}}) ;
List<String> y = Iter.toList(its) ;
System.out.println(y);
}
}
{noformat}
was (Author: andy.seaborne):
I like the style. Attached is another take on this.
The main class is Iter that provides two styles:
* A style like the FluentIterator style of method chaining.
* Static methods to provide short sequences to that one-step operations can be applied to regular iterators and iterables
Also includes a "PeekInterator" for looking oen step ahead.
The function-application style is useful for short sequences; the chainign is better for longer sequences.
{noformat}
iter = Iter.removeNulls(iter) ;
{noformat}
Example of each style: (example.IterExample.java):
{noformat}
public class IterExample
{
public static void main(String ... args)
{
List<Integer> x = Arrays.asList(1,2,3,2,3) ;
// Chaining style
Iter<String> iter = Iter.iter(x)
.filter(new Filter<Integer>() {
@Override
public boolean accept(Integer item)
{
return item.intValue() >= 2 ;
}})
.distinct()
.append(x.iterator())
.map(new Transform<Integer,String>() {
@Override
public String convert(Integer item)
{
return "["+String.valueOf(item)+"]" ;
}}) ;
System.out.println(iter.toList());
// Function application style.
Iterator<Integer> it = Iter.filter(x, new Filter<Integer>() {
@Override
public boolean accept(Integer item)
{
return item.intValue() >= 2 ;
}}) ;
it = Iter.distinct(it) ;
Iterator<String> its = Iter.map(it, new Transform<Integer,String>() {
@Override
public String convert(Integer item)
{
return "["+String.valueOf(item)+"]" ;
}}) ;
List<String> y = Iter.toList(its) ;
System.out.println(y);
}
}
{noformat}
> 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, iter-src.zip
>
>
> 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