<div>One better way to handle this in Java 8 would be to have a utility method that takes a Supplier<Enumeration<E>> SAM argument (with a no-arg method that returns an Enumeration<E>) and returns an Iterable<E> that gets a new Enumeration from the supplier each time iterator() is called. It could then be used with method references:</div>
<div><br></div><div>Iterable<E> iterable = Util.enumerationIterable(#object.getEnumeration);</div><br clear="all"><div>-- </div>Colin<br>
<br><br><div class="gmail_quote">On Wed, Aug 3, 2011 at 5:14 PM, Alexandre Boulgakov <span dir="ltr"><<a href="mailto:alexandre.boulgakov@oracle.com">alexandre.boulgakov@oracle.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Users of Iterable expect to call Iterable.iterator() multiple times, receiving a fresh iterator pointing to the beginning of the Iterable each time. This would not be possible to do with an Enumeration wrapper since Enumerations are read-once.<br>
<br>
I don't know if this is a strong enough reason not to include such a utility class, but it could certainly be confusing.<br>
<br>
-Sasha<div><div></div><div class="h5"><br>
<br>
On 8/3/2011 2:09 PM, Neil Richards wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Wed, 2011-08-03 at 11:03 -0700, Alexandre Boulgakov wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Please see my responses inline.<br>
<br>
Thanks!<br>
-Sasha<br>
<br>
On 8/2/2011 9:13 PM, Xuelei Fan wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
. com/sun/jndi/toolkit/dir/<u></u>SearchFilter.java<br>
451 for (NamingEnumeration<?> ve = attr.getAll();<br>
452 ve.hasMore();<br>
453 ) {<br>
<br>
The update is OK. But the coding style looks uncomfortable. Would you<br>
mind change it to use for-each style?<br>
</blockquote>
For-each only works with Iterables. There doesn't seem to be a way to<br>
get an Iterable out of an Attribute instance, so the only way to use a<br>
for-each here would be to wrap the Enumeration in an ArrayList using<br>
Collections.list(). While this might look neater, the contents of the<br>
Enumeration would have to be copied over, using time and increasing the<br>
memory footprint. Changing Attribute to implement Iterable would require<br>
a spec change, and would be beyond the scope of this fix.<br>
</blockquote>
Would it be useful to have a utility object to convert an Enumeration<br>
so it can be used in for-each constructs? - something like:<br>
----<br>
import java.util.Enumeration;<br>
import java.util.Iterator;<br>
<br>
/**<br>
* Utility class to transform an Enumeration object such that it can be used in<br>
* the for-each construct.<br>
*/<br>
public class IterableEnumerationHolder<E> implements Iterable<E>, Iterator<E> {<br>
private final Enumeration<E> e;<br>
<br>
public IterableEnumerationHolder(<u></u>final Enumeration<E> e) {<br>
this.e = e;<br>
}<br>
<br>
@Override<br>
public Iterator<E> iterator() {<br>
return this;<br>
}<br>
<br>
@Override<br>
public boolean hasNext() {<br>
return e.hasMoreElements();<br>
}<br>
<br>
@Override<br>
public E next() {<br>
return e.nextElement();<br>
}<br>
<br>
@Override<br>
public void remove() {<br>
throw new UnsupportedOperationException(<u></u>);<br>
}<br>
}<br>
----<br>
<br>
If it would, perhaps it might be considered for Java 8?<br>
<br>
Regards,<br>
Neil<br>
<br>
</blockquote>
</div></div></blockquote></div><br>