aQute.lib.collections.Enumerations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.aries.spifly.dynamic.framework.extension Show documentation
Show all versions of org.apache.aries.spifly.dynamic.framework.extension Show documentation
This framework extension fragment contains an extender that facilitates
the use of JRE SPI providers (components typically plugged in to the
JRE through META-INF/services resources).
package aQute.lib.collections;
import static java.util.Objects.requireNonNull;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.function.Predicate;
public class Enumerations {
private Enumerations() {}
private static class EnumerationSpliterator implements Enumeration {
private final Spliterator extends T> spliterator;
private final Function super T, ? extends R> mapper;
private final Predicate super R> filter;
private boolean hasNext = false;
private R next;
EnumerationSpliterator(Spliterator extends T> spliterator, Function super T, ? extends R> mapper,
Predicate super R> filter) {
this.spliterator = spliterator != null ? spliterator : Spliterators.emptySpliterator();
this.mapper = requireNonNull(mapper);
this.filter = requireNonNull(filter);
}
@Override
public boolean hasMoreElements() {
do {
if (hasNext) {
return true;
}
} while (spliterator.tryAdvance((T t) -> {
R r = mapper.apply(t);
if (filter.test(r)) {
next = r;
hasNext = true;
}
}));
return false;
}
@Override
public R nextElement() {
if (hasMoreElements()) {
hasNext = false;
return next;
}
throw new NoSuchElementException();
}
}
public static Enumeration enumeration(Spliterator extends T> spliterator) {
return new EnumerationSpliterator<>(spliterator, t -> t, t -> true);
}
public static Enumeration enumeration(Spliterator extends T> spliterator,
Function super T, ? extends R> mapper) {
return new EnumerationSpliterator<>(spliterator, mapper, r -> true);
}
public static Enumeration enumeration(Spliterator extends T> spliterator,
Function super T, ? extends R> mapper, Predicate super R> filter) {
return new EnumerationSpliterator<>(spliterator, mapper, filter);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy