bug-gnu-electric
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Iterator-to-Iterable adapter method: best class to put it in?


From: Dmitry . Nadezhin
Subject: Re: Iterator-to-Iterable adapter method: best class to put it in?
Date: Sun, 12 Jul 2009 13:12:48 +0400

com.sun.electric.database.text.ArrayIterator ?

  -Dima

----- Original Message -----
From: Adam Megacz <address@hidden>
Date: Sunday, July 12, 2009 7:20 am
Subject: Iterator-to-Iterable adapter method: best class to put it in?

> 
> I see that many of the Electric methods return Iterator<T>'s rather
> than Iterable<T>'s.  Unfortunately, this means that they cannot be
> used with the Java5 "enhanced for() loop" [*].  To work around 
> this, I
> wrote the following polymorphic method:
> 
>    /** Turns an Iterator<T> into an Iterable<T> so I can use 
> Java5's enhanced for() */
>    public static <T> Iterable<T> i2i(final Iterator<T> iterator) {
>        return new Iterable<T>() {
>            boolean used = false;
>            public Iterator<T> iterator() {
>                if (used) throw new RuntimeException("i2i() 
> produces single-use Iterables!");
>                used = true;
>                return iterator;
>            }
>        };
>    }
> 
> Using i2i(), I can write code like this:
> 
>   for(ImmutableExport e : i2i(m.getExports(n.nodeId))) {
>     // ..
>   }
> 
> It seems like this i2i() method should go in some sort of central
> location rather than being repeated in each class where it is used.
> Can you suggest the best place to put it?
> 
> Thanks,
> 
>  - a
> 
> [*] Personally, I wish javac would let you use an Iterator in a for()
>    loop... I can't see why that isn't allowed (or static anonymous
>    classes, for that matter -- they serialize better)...
> 
> 
> 
> _______________________________________________
> Bug-gnu-electric mailing list
> address@hidden
> http://lists.gnu.org/mailman/listinfo/bug-gnu-electric
> 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]