mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
refactor some static methods to generic versions in BiFunctor
This commit is contained in:
@@ -63,16 +63,16 @@ public class CacheSampler implements Sampler {
|
||||
@Override
|
||||
public double getSample(long seed, double x, double y) {
|
||||
Mutable<DoubleSeededVector2Key, LoadingCache<DoubleSeededVector2Key, Double>> cachePair = cache2D.get();
|
||||
DoubleSeededVector2Key mutableKey = cachePair.getLeft();
|
||||
DoubleSeededVector2Key mutableKey = cachePair.left();
|
||||
mutableKey.set(x, y, seed);
|
||||
return cachePair.getRight().get(mutableKey);
|
||||
return cachePair.right().get(mutableKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSample(long seed, double x, double y, double z) {
|
||||
Mutable<DoubleSeededVector3Key, LoadingCache<DoubleSeededVector3Key, Double>> cachePair = cache3D.get();
|
||||
DoubleSeededVector3Key mutableKey = cachePair.getLeft();
|
||||
DoubleSeededVector3Key mutableKey = cachePair.left();
|
||||
mutableKey.set(x, y, z, seed);
|
||||
return cachePair.getRight().get(mutableKey);
|
||||
return cachePair.right().get(mutableKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
package com.dfsek.terra.api.util.generic.data;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.types.Pair;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface BiFunctor<T, U, B extends BiFunctor<?, ?, B>> {
|
||||
static <L, R, B extends BiFunctor<?, ?, B>> Consumer<BiFunctor<L, R, B>> consumeLeft(Consumer<L> consumer) {
|
||||
return pair -> pair.mapLeft(p -> {
|
||||
consumer.accept(p);
|
||||
return p;
|
||||
});
|
||||
}
|
||||
|
||||
static <L, R> Consumer<Pair<L, R>> consumeRight(Consumer<R> consumer) {
|
||||
return pair -> pair.mapRight(p -> {
|
||||
consumer.accept(p);
|
||||
return p;
|
||||
});
|
||||
}
|
||||
|
||||
<V> BiFunctor<V, U, B> mapLeft(Function<T, V> map);
|
||||
<V> BiFunctor<T, V, B> mapRight(Function<U, V> map);
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -30,7 +29,7 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
||||
return of(left, function.apply(right));
|
||||
}
|
||||
|
||||
public static <L> Predicate<Pair<L, ?>> testLeft(Predicate<L> predicate) {
|
||||
public static <L, R> Predicate<Pair<L, R>> testLeft(Predicate<L> predicate) {
|
||||
return pair -> predicate.test(pair.left);
|
||||
}
|
||||
|
||||
@@ -38,19 +37,11 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
||||
return pair -> predicate.test(pair.right);
|
||||
}
|
||||
|
||||
public static <L> Consumer<Pair<L, ?>> consumeLeft(Consumer<L> consumer) {
|
||||
return pair -> consumer.accept(pair.left);
|
||||
}
|
||||
|
||||
public static <R> Consumer<Pair<?, R>> consumeRight(Consumer<R> consumer) {
|
||||
return pair -> consumer.accept(pair.right);
|
||||
}
|
||||
|
||||
public static <R> Function<Pair<?, R>, R> unwrapRight() {
|
||||
public static <L, R> Function<Pair<L, R>, R> unwrapRight() {
|
||||
return pair -> pair.right;
|
||||
}
|
||||
|
||||
public static <L> Function<Pair<L, ?>, L> unwrapLeft() {
|
||||
public static <L, R> Function<Pair<L, R>, L> unwrapLeft() {
|
||||
return pair -> pair.left;
|
||||
}
|
||||
|
||||
@@ -73,9 +64,9 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof Pair<?, ?> that)) return false;
|
||||
if(!(obj instanceof Pair<?, ?>(Object left1, Object right1))) return false;
|
||||
|
||||
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
|
||||
return Objects.equals(this.left, left1) && Objects.equals(this.right, right1);
|
||||
}
|
||||
|
||||
public Pair<L, R> apply(BiConsumer<L, R> consumer) {
|
||||
@@ -109,7 +100,7 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
||||
return Pair.of(left, right);
|
||||
}
|
||||
|
||||
public L getLeft() {
|
||||
public L left() {
|
||||
return left;
|
||||
}
|
||||
|
||||
@@ -117,7 +108,7 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public R getRight() {
|
||||
public R right() {
|
||||
return right;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,17 +74,17 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
|
||||
@Override
|
||||
public Biome getBiome(int x, int y, int z, long seed) {
|
||||
Mutable<SeededVector3Key, LoadingCache<SeededVector3Key, Biome>> cachePair = cache.get();
|
||||
SeededVector3Key mutableKey = cachePair.getLeft();
|
||||
SeededVector3Key mutableKey = cachePair.left();
|
||||
mutableKey.set(x, y, z, seed);
|
||||
return cachePair.getRight().get(mutableKey);
|
||||
return cachePair.right().get(mutableKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Maybe<Biome> getBaseBiome(int x, int z, long seed) {
|
||||
Mutable<SeededVector2Key, LoadingCache<SeededVector2Key, Maybe<Biome>>> cachePair = baseCache.get();
|
||||
SeededVector2Key mutableKey = cachePair.getLeft();
|
||||
SeededVector2Key mutableKey = cachePair.left();
|
||||
mutableKey.set(x, z, seed);
|
||||
return cachePair.getRight().get(mutableKey);
|
||||
return cachePair.right().get(mutableKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
package com.dfsek.terra;
|
||||
|
||||
import com.dfsek.tectonic.api.TypeRegistry;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.BiFunctor;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -352,7 +355,7 @@ public abstract class AbstractPlatform implements Platform {
|
||||
paths
|
||||
.stream()
|
||||
.filter(Pair.testRight(resourcePath::startsWith))
|
||||
.forEach(Pair.consumeLeft(path -> {
|
||||
.forEach(BiFunctor.consumeLeft(path -> {
|
||||
logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath);
|
||||
try {
|
||||
Files.delete(path);
|
||||
|
||||
Reference in New Issue
Block a user