All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.launcher.minifw.Enumerations Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.launcher.minifw;

import static java.util.Objects.requireNonNull;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators.AbstractSpliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Enumerations implements Enumeration {
	private final Iterator iterator;

	public static  Enumeration enumeration(Stream stream) {
		return new Enumerations<>(stream.iterator());
	}

	public static  Enumeration enumeration(Iterable iterable) {
		return new Enumerations<>(iterable.iterator());
	}

	public static  Stream stream(Enumeration enumeration) {
		return StreamSupport.stream(new AbstractSpliterator(Long.MAX_VALUE, Spliterator.ORDERED) {
			@Override
			public void forEachRemaining(Consumer action) {
				requireNonNull(action);
				while (enumeration.hasMoreElements()) {
					action.accept(enumeration.nextElement());
				}
			}

			@Override
			public boolean tryAdvance(Consumer action) {
				requireNonNull(action);
				if (enumeration.hasMoreElements()) {
					action.accept(enumeration.nextElement());
					return true;
				}
				return false;
			}
		}, false);
	}

	private Enumerations(Iterator iterator) {
		this.iterator = iterator;
	}

	@Override
	public boolean hasMoreElements() {
		return iterator.hasNext();
	}

	@Override
	public E nextElement() {
		return iterator.next();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy