rename Lazy to Memo

This commit is contained in:
dfsek
2026-01-02 21:13:24 -07:00
parent 53dafa4a2c
commit e9d30b8794
5 changed files with 22 additions and 22 deletions

View File

@@ -17,7 +17,7 @@ import com.dfsek.terra.addons.image.image.Image;
import com.dfsek.terra.addons.image.image.SuppliedImage; import com.dfsek.terra.addons.image.image.SuppliedImage;
import com.dfsek.terra.api.config.ConfigPack; import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.properties.Properties; import com.dfsek.terra.api.properties.Properties;
import com.dfsek.terra.api.util.generic.Lazy; import com.dfsek.terra.api.util.generic.Memo;
import static com.dfsek.terra.api.util.cache.CacheUtils.CACHE_EXECUTOR; import static com.dfsek.terra.api.util.cache.CacheUtils.CACHE_EXECUTOR;
@@ -41,7 +41,7 @@ record ImageCache(LoadingCache<String, Image> cache) implements Properties {
return new SuppliedImage(() -> images.cache.get(path)); return new SuppliedImage(() -> images.cache.get(path));
} else { } else {
// If images do not time out, image can be lazily loaded once instead of performing cache lookups for each image operation // If images do not time out, image can be lazily loaded once instead of performing cache lookups for each image operation
Lazy<Image> lazyImage = Lazy.lazy(() -> images.cache.get(path)); Memo<Image> lazyImage = Memo.lazy(() -> images.cache.get(path));
return new SuppliedImage(lazyImage::value); return new SuppliedImage(lazyImage::value);
} }
} }

View File

@@ -11,13 +11,13 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import com.dfsek.terra.api.block.state.properties.Property; import com.dfsek.terra.api.block.state.properties.Property;
import com.dfsek.terra.api.util.generic.Lazy; import com.dfsek.terra.api.util.generic.Memo;
public interface EnumProperty<T extends Enum<T>> extends Property<T> { public interface EnumProperty<T extends Enum<T>> extends Property<T> {
static <T extends Enum<T>> EnumProperty<T> of(String name, Class<T> clazz) { static <T extends Enum<T>> EnumProperty<T> of(String name, Class<T> clazz) {
return new EnumProperty<>() { return new EnumProperty<>() {
private final Lazy<Collection<T>> constants = Lazy.lazy(() -> Arrays.asList(clazz.getEnumConstants())); private final Memo<Collection<T>> constants = Memo.lazy(() -> Arrays.asList(clazz.getEnumConstants()));
@Override @Override
public Collection<T> values() { public Collection<T> values() {

View File

@@ -11,7 +11,7 @@ import java.util.function.BooleanSupplier;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
import com.dfsek.terra.api.util.function.IntPredicate; import com.dfsek.terra.api.util.function.IntPredicate;
import com.dfsek.terra.api.util.generic.Lazy; import com.dfsek.terra.api.util.generic.Memo;
import com.dfsek.terra.api.util.range.Range; import com.dfsek.terra.api.util.range.Range;
@@ -23,7 +23,7 @@ public class BinaryColumn {
private final IntPredicate data; private final IntPredicate data;
private final int minY; private final int minY;
private final int maxY; private final int maxY;
private final Lazy<boolean[]> results; private final Memo<boolean[]> results;
/** /**
* Constructs a new {@link BinaryColumn} with all values initiated to {@code false} * Constructs a new {@link BinaryColumn} with all values initiated to {@code false}
@@ -34,7 +34,7 @@ public class BinaryColumn {
public BinaryColumn(int minY, int maxY, IntPredicate data) { public BinaryColumn(int minY, int maxY, IntPredicate data) {
this.minY = minY; this.minY = minY;
this.maxY = maxY; this.maxY = maxY;
this.results = Lazy.lazy(() -> { this.results = Memo.lazy(() -> {
boolean[] res = new boolean[maxY - minY]; boolean[] res = new boolean[maxY - minY];
for(int y = minY; y < maxY; y++) { for(int y = minY; y < maxY; y++) {
res[y - minY] = get(y); res[y - minY] = get(y);
@@ -48,7 +48,7 @@ public class BinaryColumn {
public BinaryColumn(int minY, int maxY, boolean[] data) { public BinaryColumn(int minY, int maxY, boolean[] data) {
this.minY = minY; this.minY = minY;
this.maxY = maxY; this.maxY = maxY;
this.results = Lazy.lazy(() -> data); this.results = Memo.lazy(() -> data);
if(maxY <= minY) throw new IllegalArgumentException("Max y must be greater than min y"); if(maxY <= minY) throw new IllegalArgumentException("Max y must be greater than min y");
this.data = y -> data[y - minY]; this.data = y -> data[y - minY];
} }

View File

@@ -16,21 +16,21 @@ import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public final class Lazy<T> implements Monad<T, Lazy<?>> { public final class Memo<T> implements Monad<T, Memo<?>> {
private final Supplier<T> valueSupplier; private final Supplier<T> valueSupplier;
private volatile T value = null; private volatile T value = null;
private final AtomicBoolean got = new AtomicBoolean(false); private final AtomicBoolean got = new AtomicBoolean(false);
private Lazy(Supplier<T> valueSupplier) { private Memo(Supplier<T> valueSupplier) {
this.valueSupplier = valueSupplier; this.valueSupplier = valueSupplier;
} }
public static <T> Lazy<T> lazy(Supplier<T> valueSupplier) { public static <T> Memo<T> lazy(Supplier<T> valueSupplier) {
return new Lazy<>(valueSupplier); return new Memo<>(valueSupplier);
} }
public static <T> Lazy<T> of(T value) { public static <T> Memo<T> of(T value) {
return new Lazy<>(() -> value); return new Memo<>(() -> value);
} }
public T value() { public T value() {
@@ -41,17 +41,17 @@ public final class Lazy<T> implements Monad<T, Lazy<?>> {
} }
@Override @Override
public @NotNull <T2> Lazy<T2> bind(@NotNull Function<T, Monad<T2, Lazy<?>>> map) { public @NotNull <T2> Memo<T2> bind(@NotNull Function<T, Monad<T2, Memo<?>>> map) {
return lazy(() -> ((Lazy<T2>) map.apply(value())).value()); return lazy(() -> ((Memo<T2>) map.apply(value())).value());
} }
@Override @Override
public @NotNull <U> Lazy<U> map(@NotNull Function<T, U> map) { public @NotNull <U> Memo<U> map(@NotNull Function<T, U> map) {
return lazy(() -> map.apply(value())); return lazy(() -> map.apply(value()));
} }
@Override @Override
public @NotNull <T1> Lazy<T1> pure(@NotNull T1 t) { public @NotNull <T1> Memo<T1> pure(@NotNull T1 t) {
return new Lazy<>(() -> t); return new Memo<>(() -> t);
} }
} }

View File

@@ -2,21 +2,21 @@ package com.dfsek.terra.cli.block;
import com.dfsek.terra.api.block.BlockType; import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.util.generic.Lazy; import com.dfsek.terra.api.util.generic.Memo;
public class CLIBlockType implements BlockType { public class CLIBlockType implements BlockType {
private final String value; private final String value;
private final boolean solid; private final boolean solid;
private final boolean water; private final boolean water;
private final Lazy<CLIBlockState> defaultState; private final Memo<CLIBlockState> defaultState;
public CLIBlockType(String value) { public CLIBlockType(String value) {
if(value.contains("[")) throw new IllegalArgumentException("Block Type must not contain properties"); if(value.contains("[")) throw new IllegalArgumentException("Block Type must not contain properties");
this.value = value; this.value = value;
this.solid = !value.equals("minecraft:air"); this.solid = !value.equals("minecraft:air");
this.water = value.equals("minecraft:water"); this.water = value.equals("minecraft:water");
this.defaultState = Lazy.lazy(() -> new CLIBlockState(value)); this.defaultState = Memo.lazy(() -> new CLIBlockState(value));
} }
@Override @Override