aQute.lib.collections.EnumerationIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.aries.spifly.dynamic.framework.extension Show documentation
Show all versions of org.apache.aries.spifly.dynamic.framework.extension Show documentation
This framework extension fragment contains an extender that facilitates
the use of JRE SPI providers (components typically plugged in to the
JRE through META-INF/services resources).
package aQute.lib.collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Simple facade for enumerators so they can be used in for loops.
*
* @param
*/
public class EnumerationIterator implements Iterable, Iterator {
public static EnumerationIterator iterator(Enumeration extends T> e) {
return new EnumerationIterator<>(e);
}
private final Enumeration extends T> enumerator;
private final AtomicBoolean done = new AtomicBoolean();
public EnumerationIterator(Enumeration extends T> e) {
enumerator = e;
}
@Override
public Iterator iterator() {
if (!done.compareAndSet(false, true))
throw new IllegalStateException("Can only be used once");
return this;
}
@Override
public boolean hasNext() {
return enumerator.hasMoreElements();
}
@Override
public T next() {
return enumerator.nextElement();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Does not support removes");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy