
org.davidmoten.kool.internal.operators.stream.ToSingle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kool Show documentation
Show all versions of kool Show documentation
Streaming library supporting reuse and many operators
package org.davidmoten.kool.internal.operators.stream;
import java.util.NoSuchElementException;
import org.davidmoten.kool.Single;
import org.davidmoten.kool.Stream;
import org.davidmoten.kool.StreamIterator;
public class ToSingle implements Single {
private final Stream stream;
public ToSingle(Stream stream) {
this.stream = stream;
}
@Override
public T get() {
StreamIterator it = stream.iteratorNullChecked();
try {
if (it.hasNext()) {
T v = it.nextNullChecked();
if (it.hasNext()) {
throw new IllegalArgumentException("stream must only have one element but has more");
} else {
return v;
}
} else {
throw new NoSuchElementException();
}
} finally {
it.dispose();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy