com.sun.electric.database.text.ArrayIterator ? -Dima
----- Original Message ----- From: Adam Megacz <[email protected]> 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 > [email protected] > http://lists.gnu.org/mailman/listinfo/bug-gnu-electric > _______________________________________________ Bug-gnu-electric mailing list [email protected] http://lists.gnu.org/mailman/listinfo/bug-gnu-electric
