io.quarkus.runtime.util.EnumerationUtil Maven / Gradle / Ivy
package io.quarkus.runtime.util;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* Transform to "old school" Enumeration from Iterator/Spliterator/Stream
*/
public class EnumerationUtil {
public static Enumeration from(Iterator iterator) {
Objects.requireNonNull(iterator);
return new Enumeration() {
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public T nextElement() {
return iterator.next();
}
};
}
public static Enumeration from(Spliterator spliterator) {
Objects.requireNonNull(spliterator);
class Adapter implements Enumeration, Consumer {
boolean valueReady;
T nextElement;
public void accept(T t) {
this.valueReady = true;
this.nextElement = t;
}
public boolean hasMoreElements() {
if (!this.valueReady) {
spliterator.tryAdvance(this);
}
return this.valueReady;
}
public T nextElement() {
if (!this.valueReady && !this.hasMoreElements()) {
throw new NoSuchElementException();
} else {
this.valueReady = false;
T t = this.nextElement;
this.nextElement = null;
return t;
}
}
}
return new Adapter();
}
public static Enumeration from(Stream stream) {
return from(stream.spliterator());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy