[data-manipulation] Temporary workaround for nullable openRoot

This commit is contained in:
Johannes Frohnmeyer 2022-06-21 18:53:26 +02:00
parent c63d637d1f
commit cebb022838
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 61 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package io.gitlab.jfronny.libjf.data.manipulation.api;
import io.gitlab.jfronny.commons.LazySupplier;
import io.gitlab.jfronny.commons.throwable.*;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.data.manipulation.impl.JLazySupplier;
import io.gitlab.jfronny.libjf.data.manipulation.impl.ResourcePackHook;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
@ -73,7 +74,7 @@ public class UserResourceEvents {
public static final Event<OpenRoot> OPEN_ROOT = EventFactory.createArrayBacked(OpenRoot.class,
(listeners) -> ((fileName, previous, pack) -> {
LazySupplier<InputStream> lazy = new LazySupplier<>(previous);
JLazySupplier<InputStream> lazy = new JLazySupplier<>(previous);
for (OpenRoot listener : listeners) {
lazy = lazy.andThen(supplier -> {
try {

View File

@ -0,0 +1,59 @@
package io.gitlab.jfronny.libjf.data.manipulation.impl;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @deprecated Use java-commons LazySupplier instead once that is included in libjf-base
*/
@Deprecated
//TODO remove this once I update libjf-base
public class JLazySupplier<T> implements Supplier<T> {
private final Supplier<T> supplier;
private T cache = null;
private boolean initialized;
/**
* Create a lazy supplier from a pre-initialized value.
* The backing supplier will be empty and never called.
* @param value The value to initialize the cache with
*/
public JLazySupplier(@Nullable T value) {
this.supplier = () -> {
throw new RuntimeException("Supplier should have never been called");
};
initialized = true;
cache = value;
}
/**
* Create a lazy supplier backed with the specified supplier.
* It will at most be called once when this supplier is first used.
* @param supplier The backing supplier
*/
public JLazySupplier(@NotNull Supplier<T> supplier) {
this.supplier = Objects.requireNonNull(supplier);
initialized = false;
}
@Override @Contract(pure = true) @NotNull public T get() {
if (!initialized) cache = supplier.get();
return cache;
}
/**
* Generates a new lazy supplier from a function.
* This function is provided with this lazy supplier as a data source for transformation.
* Allows complex transformations to not be run if a later supplier has a shortcut
* @param after A backing function for the new lazy supplier
* @return A new lazy supplier
*/
@Contract(pure = true) @NotNull public JLazySupplier<T> andThen(@NotNull Function<JLazySupplier<T>, T> after) {
return new JLazySupplier<>(() -> after.apply(this));
}
}