com.link_intersystems.util.Iterators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lis-commons-util Show documentation
Show all versions of lis-commons-util Show documentation
Link Intersystems Commons Util (lis-commons-util) is collection of reusable software components
that extend the standard java.util components.
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();
}
}