[core] Some util classes from Inceptum
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2022-11-02 22:30:56 +01:00
parent ecf1037b2e
commit c1d5aba9d7
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package io.gitlab.jfronny.commons;
import java.util.Objects;
import java.util.function.Supplier;
public final class OnceSupplier<T> implements Supplier<T> {
private final T value;
private boolean supplied = false;
public OnceSupplier(T value) {
this.value = value;
}
@Override
public T get() {
if (supplied) throw new IllegalStateException("Attempted to use already used OnceSupplier");
supplied = true;
return value;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof OnceSupplier<?> that)) return false;
return Objects.equals(this.value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@ -0,0 +1,25 @@
package io.gitlab.jfronny.commons;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
public record StreamIterable<T>(Supplier<Stream<T>> source) implements Iterable<T> {
public StreamIterable(Stream<T> source) {
this(new OnceSupplier<>(source));
}
@NotNull
@Override
public Iterator<T> iterator() {
return source.get().iterator();
}
@Override
public void forEach(Consumer<? super T> action) {
this.source.get().forEach(action);
}
}