dev.marksman.enhancediterables.EnhancedIterables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enhanced-iterables Show documentation
Show all versions of enhanced-iterables Show documentation
Iterables with additional methods
package dev.marksman.enhancediterables;
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
import com.jnape.palatable.lambda.functions.builtin.fn1.Uncons;
import static dev.marksman.enhancediterables.ProtectedIterator.protectedIterator;
import static java.util.Arrays.asList;
final class EnhancedIterables {
private EnhancedIterables() {
}
static FiniteIterable finiteIterable(Iterable underlying) {
if (underlying instanceof FiniteIterable>) {
return (FiniteIterable) underlying;
} else {
return () -> protectedIterator(underlying.iterator());
}
}
static ImmutableIterable immutableIterable(Iterable underlying) {
if (underlying instanceof ImmutableIterable>) {
return (ImmutableIterable) underlying;
} else {
return () -> protectedIterator(underlying.iterator());
}
}
static ImmutableFiniteIterable immutableFiniteIterable(Iterable underlying) {
if (underlying instanceof ImmutableFiniteIterable>) {
return (ImmutableFiniteIterable) underlying;
} else {
return () -> protectedIterator(underlying.iterator());
}
}
static NonEmptyIterable nonEmptyIterableOrThrow(Iterable underlying) {
if (underlying instanceof NonEmptyIterable>) {
return (NonEmptyIterable) underlying;
} else {
Tuple2> headTail = unconsOrThrow(underlying);
return NonEmptyIterable.nonEmptyIterable(headTail._1(), headTail._2());
}
}
static ImmutableNonEmptyIterable immutableNonEmptyIterableOrThrow(Iterable underlying) {
if (underlying instanceof ImmutableNonEmptyIterable>) {
return (ImmutableNonEmptyIterable) underlying;
} else {
Tuple2> headTail = unconsOrThrow(underlying);
return ImmutableNonEmptyIterable.immutableNonEmptyIterable(headTail._1(), immutableIterable(headTail._2()));
}
}
static NonEmptyFiniteIterable nonEmptyFiniteIterableOrThrow(Iterable underlying) {
if (underlying instanceof NonEmptyFiniteIterable>) {
return (NonEmptyFiniteIterable) underlying;
} else {
Tuple2> headTail = unconsOrThrow(underlying);
return NonEmptyFiniteIterable.nonEmptyFiniteIterable(headTail._1(), finiteIterable(headTail._2()));
}
}
static ImmutableNonEmptyFiniteIterable immutableNonEmptyFiniteIterableOrThrow(Iterable underlying) {
if (underlying instanceof ImmutableNonEmptyFiniteIterable>) {
return (ImmutableNonEmptyFiniteIterable) underlying;
} else {
Tuple2> headTail = unconsOrThrow(underlying);
return ImmutableNonEmptyFiniteIterable.immutableNonEmptyFiniteIterable(headTail._1(),
immutableFiniteIterable(headTail._2()));
}
}
@SafeVarargs
static ImmutableNonEmptyFiniteIterable of(A first, A... more) {
ImmutableFiniteIterable tail = immutableFiniteIterable(asList(more));
return new ImmutableNonEmptyFiniteIterable() {
@Override
public A head() {
return first;
}
@Override
public ImmutableFiniteIterable tail() {
return tail;
}
};
}
private static Tuple2> unconsOrThrow(Iterable iterable) {
return Uncons.uncons(iterable)
.orElseThrow(() -> new IllegalArgumentException("iterable is empty"));
}
}