mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-27 14:30:23 +00:00
refactor some static methods to generic versions in BiFunctor
This commit is contained in:
1 parent
86bb4d5a1a
commit
9357b18964
5 files changed
+36
-25
No files matched your search
+4
-4
@@ -63,16 +63,16 @@ public class CacheSampler implements Sampler {
|
|||||||
@Override
|
@Override
|
||||||
public double getSample(long seed, double x, double y) {
|
public double getSample(long seed, double x, double y) {
|
||||||
Mutable<DoubleSeededVector2Key, LoadingCache<DoubleSeededVector2Key, Double>> cachePair = cache2D.get();
|
Mutable<DoubleSeededVector2Key, LoadingCache<DoubleSeededVector2Key, Double>> cachePair = cache2D.get();
|
||||||
DoubleSeededVector2Key mutableKey = cachePair.getLeft();
|
DoubleSeededVector2Key mutableKey = cachePair.left();
|
||||||
mutableKey.set(x, y, seed);
|
mutableKey.set(x, y, seed);
|
||||||
return cachePair.getRight().get(mutableKey);
|
return cachePair.right().get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getSample(long seed, double x, double y, double z) {
|
public double getSample(long seed, double x, double y, double z) {
|
||||||
Mutable<DoubleSeededVector3Key, LoadingCache<DoubleSeededVector3Key, Double>> cachePair = cache3D.get();
|
Mutable<DoubleSeededVector3Key, LoadingCache<DoubleSeededVector3Key, Double>> cachePair = cache3D.get();
|
||||||
DoubleSeededVector3Key mutableKey = cachePair.getLeft();
|
DoubleSeededVector3Key mutableKey = cachePair.left();
|
||||||
mutableKey.set(x, y, z, seed);
|
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;
|
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;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
|
||||||
public interface BiFunctor<T, U, B extends BiFunctor<?, ?, B>> {
|
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<V, U, B> mapLeft(Function<T, V> map);
|
||||||
<V> BiFunctor<T, V, B> mapRight(Function<U, 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.Objects;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
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));
|
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);
|
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);
|
return pair -> predicate.test(pair.right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <L> Consumer<Pair<L, ?>> consumeLeft(Consumer<L> consumer) {
|
public static <L, R> Function<Pair<L, R>, R> unwrapRight() {
|
||||||
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() {
|
|
||||||
return pair -> pair.right;
|
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;
|
return pair -> pair.left;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,9 +64,9 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
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) {
|
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);
|
return Pair.of(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public L getLeft() {
|
public L left() {
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +108,7 @@ public record Pair<L, R>(L left, R right) implements BiFunctor<L, R, Pair<?, ?>>
|
|||||||
this.left = left;
|
this.left = left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public R getRight() {
|
public R right() {
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -74,17 +74,17 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
|
|||||||
@Override
|
@Override
|
||||||
public Biome getBiome(int x, int y, int z, long seed) {
|
public Biome getBiome(int x, int y, int z, long seed) {
|
||||||
Mutable<SeededVector3Key, LoadingCache<SeededVector3Key, Biome>> cachePair = cache.get();
|
Mutable<SeededVector3Key, LoadingCache<SeededVector3Key, Biome>> cachePair = cache.get();
|
||||||
SeededVector3Key mutableKey = cachePair.getLeft();
|
SeededVector3Key mutableKey = cachePair.left();
|
||||||
mutableKey.set(x, y, z, seed);
|
mutableKey.set(x, y, z, seed);
|
||||||
return cachePair.getRight().get(mutableKey);
|
return cachePair.right().get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Maybe<Biome> getBaseBiome(int x, int z, long seed) {
|
public Maybe<Biome> getBaseBiome(int x, int z, long seed) {
|
||||||
Mutable<SeededVector2Key, LoadingCache<SeededVector2Key, Maybe<Biome>>> cachePair = baseCache.get();
|
Mutable<SeededVector2Key, LoadingCache<SeededVector2Key, Maybe<Biome>>> cachePair = baseCache.get();
|
||||||
SeededVector2Key mutableKey = cachePair.getLeft();
|
SeededVector2Key mutableKey = cachePair.left();
|
||||||
mutableKey.set(x, z, seed);
|
mutableKey.set(x, z, seed);
|
||||||
return cachePair.getRight().get(mutableKey);
|
return cachePair.right().get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -18,6 +18,9 @@
|
|||||||
package com.dfsek.terra;
|
package com.dfsek.terra;
|
||||||
|
|
||||||
import com.dfsek.tectonic.api.TypeRegistry;
|
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.FileUtils;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -352,7 +355,7 @@ public abstract class AbstractPlatform implements Platform {
|
|||||||
paths
|
paths
|
||||||
.stream()
|
.stream()
|
||||||
.filter(Pair.testRight(resourcePath::startsWith))
|
.filter(Pair.testRight(resourcePath::startsWith))
|
||||||
.forEach(Pair.consumeLeft(path -> {
|
.forEach(BiFunctor.consumeLeft(path -> {
|
||||||
logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath);
|
logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath);
|
||||||
try {
|
try {
|
||||||
Files.delete(path);
|
Files.delete(path);
|
||||||
|
|||||||
Reference in new issue
Block a user