[core] Some util classes from Inceptum
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
ecf1037b2e
commit
c1d5aba9d7
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue