Here is the javadoc from Guava regarding the conversion of an Enumeration to Iterator (not iterable):
public static UnmodifiableIterator
forEnumeration(Enumeration enumeration)
Adapts an Enumeration to the Iterator interface.
This method has no equivalent in Iterables because viewing an
Enumeration as an Iterable is impossible. However, the contents can be
copied into a collection using
Collections.list(java.util.Enumeration).
Further more apache commons collections current implementation doesn't support Java 5 features and APIs such as Iterable, so there's no chance there.
There are however some methods in those libraries which allow you to change an enumeration to a collection which is iterable and use that (they do implicitly copy your data though).
For instance, transform to a list with EnumerationUtils.toList(enumeration).
Edit: Due to some queries in the question, I'll try and summarize why the makers of guava (and I) don't feel an enumeration can be made into an iterable.
An iterable creates iterator instances, someone reading the code (or API) can assume that each call to iterator() yields a new iterator instance starting the enumeration from the first element. If we do a simple conversion between an iterator (or enumeration) and an iterable then the user of the API needs to know that a call to iterator() changes the state of the object and that 2 consecutive calls might behave oddly. here is an example:
Iterable<String> iter = magicFunction(enumeration);
for (String s : iter) {
if ("hello".equals(s))
break;
}
for (String s : iter) {
if ("world".equals(s))
return true;
}
return false;
If implemented as a simple conversion ( O(1) ) The above method behaves differently for different inputs: ["hello","world"]
would return true
, while ["world","hello"]
would return false
. This is not immediately apparent when looking at the code and can be the cause for many frustrating bugs. Therefore, I believe it makes sense to not have this utility method exist.