com.link_intersystems.util.Iterators Maven / Gradle / Ivy
package com.link_intersystems.util;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* @author René Link {@literal }
*/
public class Iterators {
public static Stream toStream(Iterator iterator) {
Iterable iterable = () -> iterator;
return toStream(iterable);
}
public static Stream toStream(Iterable iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}
public static List toList(Iterable iterable) {
return toList(iterable.iterator());
}
public static List toList(Iterator iterator) {
return toStream(iterator).collect(Collectors.toList());
}
public static Iterator filtered(Iterator iterator, Predicate filterPredicate) {
return toStream(iterator).filter(filterPredicate).iterator();
}
public static Iterator chained(Iterator... iterators) {
return chained(Arrays.asList(iterators));
}
public static Iterator chained(List> iterators) {
return iterators.stream().map(Iterators::toStream).flatMap(Function.identity()).iterator();
}
public static Iterator transformed(Iterator iterator, Function transformer) {
return toStream(iterator).map(transformer).iterator();
}
}