All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.scravy.bedrock.ForEach Maven / Gradle / Ivy

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 consumer
  ) {
    Objects.requireNonNull(seq);
    Objects.requireNonNull(consumer);
    seq.forEach(consumer);
  }

  static  void forEach(
    @Nonnull final Iterator seq,
    @Nonnull final Consumer consumer
  ) {
    Objects.requireNonNull(seq);
    Objects.requireNonNull(consumer);
    seq.forEachRemaining(consumer);
  }

  static  void forEach(
    @Nonnull final Iterable> seq,
    @Nonnull final BiConsumer 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> seq,
    @Nonnull final TriConsumer 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 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 consumer
  ) {
    Objects.requireNonNull(optional);
    Objects.requireNonNull(consumer);
    optional.ifPresent(consumer);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy