de.scravy.bedrock.ForEach Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bedrock Show documentation
Show all versions of bedrock Show documentation
Essential utilities for modern Java.
The newest version!
package de.scravy.bedrock;
import lombok.experimental.UtilityClass;
import javax.annotation.Nonnull;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@UtilityClass
public class ForEach {
static void forEach(
@Nonnull final Iterable seq,
@Nonnull final Consumer super A> consumer
) {
Objects.requireNonNull(seq);
Objects.requireNonNull(consumer);
seq.forEach(consumer);
}
static void forEach(
@Nonnull final Iterator seq,
@Nonnull final Consumer super A> consumer
) {
Objects.requireNonNull(seq);
Objects.requireNonNull(consumer);
seq.forEachRemaining(consumer);
}
static void forEach(
@Nonnull final Iterable extends Tuple2> seq,
@Nonnull final BiConsumer super A, ? super B> consumer
) {
Objects.requireNonNull(seq);
Objects.requireNonNull(consumer);
seq.forEach(tuple -> consumer.accept(tuple.getFirst(), tuple.getSecond()));
}
@FunctionalInterface
interface TriConsumer {
void accept(final A a, final B b, final C c);
}
static void forEach(
@Nonnull final Iterable extends Tuple3> seq,
@Nonnull final TriConsumer super A, ? super B, ? super C> consumer
) {
Objects.requireNonNull(seq);
Objects.requireNonNull(consumer);
seq.forEach(triple -> consumer.accept(triple.getFirst(), triple.getSecond(), triple.getThird()));
}
static void forEach(
@Nonnull final A[] as,
@Nonnull final Consumer super A> consumer
) {
Objects.requireNonNull(as);
Objects.requireNonNull(consumer);
for (final A a : as) {
consumer.accept(a);
}
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
static void forEach(
@Nonnull final Optional optional,
@Nonnull final Consumer super A> consumer
) {
Objects.requireNonNull(optional);
Objects.requireNonNull(consumer);
optional.ifPresent(consumer);
}
}