Make Pair a record

This commit is contained in:
dfsek committed 2026-01-01 20:10:15 -07:00
1 parent eb6b3704d0
commit 47bdd66fe7
11 files changed
+24 -49

No files matched your search

@@ -121,11 +121,11 @@ public class Scope {
} }
public int getIndex(String id) { public int getIndex(String id) {
return indices.get(id).getLeft(); return indices.get(id).left();
} }
public ReturnType getType(String id) { public ReturnType getType(String id) {
return indices.get(id).getRight(); return indices.get(id).right();
} }
@@ -41,8 +41,8 @@ public class IfKeyword implements Keyword<Block.ReturnInfo<?>> {
if(statement.apply(implementationArguments, scope)) return conditional.apply(implementationArguments, scope); if(statement.apply(implementationArguments, scope)) return conditional.apply(implementationArguments, scope);
else { else {
for(Pair<Returnable<Boolean>, Block> pair : elseIf) { for(Pair<Returnable<Boolean>, Block> pair : elseIf) {
if(pair.getLeft().apply(implementationArguments, scope)) { if(pair.left().apply(implementationArguments, scope)) {
return pair.getRight().apply(implementationArguments, scope); return pair.right().apply(implementationArguments, scope);
} }
} }
if(elseBlock != null) return elseBlock.apply(implementationArguments, scope); if(elseBlock != null) return elseBlock.apply(implementationArguments, scope);
@@ -17,15 +17,8 @@ import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public final class Pair<L, R> { public record Pair<L, R>(L left, R right) implements Bifunctor{
private static final Pair<?, ?> NULL = new Pair<>(null, null); private static final Pair<?, ?> NULL = new Pair<>(null, null);
private final L left;
private final R right;
private Pair(L left, R right) {
this.left = left;
this.right = right;
}
public static <L, R, T> Function<Pair<L, R>, Pair<T, R>> mapLeft(Function<L, T> function) { public static <L, R, T> Function<Pair<L, R>, Pair<T, R>> mapLeft(Function<L, T> function) {
return pair -> of(function.apply(pair.left), pair.right); return pair -> of(function.apply(pair.left), pair.right);
@@ -76,19 +69,6 @@ public final class Pair<L, R> {
return Mutable.of(left, right); return Mutable.of(left, right);
} }
public R getRight() {
return right;
}
public L getLeft() {
return left;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof Pair<?, ?> that)) return false; if(!(obj instanceof Pair<?, ?> that)) return false;
@@ -29,8 +29,6 @@ import com.dfsek.tectonic.api.loader.type.TypeLoader;
import com.dfsek.tectonic.impl.loading.object.ObjectTemplateLoader; import com.dfsek.tectonic.impl.loading.object.ObjectTemplateLoader;
import com.dfsek.tectonic.yaml.YamlConfiguration; import com.dfsek.tectonic.yaml.YamlConfiguration;
import com.dfsek.terra.api.tectonic.ConfigLoadingDelegate;
import com.google.common.collect.ListMultimap; import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -51,11 +49,9 @@ import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader.Provider;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
@@ -186,7 +182,7 @@ public class ConfigPackImpl implements ConfigPack {
logger.info("Loading config pack \"{}:{}\"", id, namespace); logger.info("Loading config pack \"{}:{}\"", id, namespace);
configTypes.values().forEach(list -> list.forEach(pair -> configTypeRegistry.register(pair.getLeft(), pair.getRight()))); configTypes.values().forEach(list -> list.forEach(pair -> configTypeRegistry.register(pair.left(), pair.right())));
ListMultimap<ConfigType<?, ?>, Configuration> multimap = configurations.values().parallelStream().collect( ListMultimap<ConfigType<?, ?>, Configuration> multimap = configurations.values().parallelStream().collect(
() -> Multimaps.newListMultimap(new ConcurrentHashMap<>(), ArrayList::new), (configs, configuration) -> { () -> Multimaps.newListMultimap(new ConcurrentHashMap<>(), ArrayList::new), (configs, configuration) -> {
@@ -216,7 +212,7 @@ public class ConfigPackImpl implements ConfigPack {
return Pair.of(configuration.getID(), loaded); return Pair.of(configuration.getID(), loaded);
}) })
.toList() .toList()
.forEach(pair -> registry.register(key(pair.getLeft()), pair.getRight())); .forEach(pair -> registry.register(key(pair.left()), pair.right()));
platform.getEventManager().callEvent(new ConfigTypePostLoadEvent(configType, registry, this)); platform.getEventManager().callEvent(new ConfigTypePostLoadEvent(configType, registry, this));
}); });
@@ -289,7 +285,7 @@ public class ConfigPackImpl implements ConfigPack {
public ConfigPack registerConfigType(ConfigType<?, ?> type, RegistryKey key, int priority) { public ConfigPack registerConfigType(ConfigType<?, ?> type, RegistryKey key, int priority) {
Set<RegistryKey> contained = new HashSet<>(); Set<RegistryKey> contained = new HashSet<>();
configTypes.forEach((p, configs) -> configs.forEach(pair -> { configTypes.forEach((p, configs) -> configs.forEach(pair -> {
if(contained.contains(pair.getLeft())) throw new IllegalArgumentException("Duplicate config key: " + key); if(contained.contains(pair.left())) throw new IllegalArgumentException("Duplicate config key: " + key);
contained.add(key); contained.add(key);
})); }));
configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(Pair.of(key, type)); configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(Pair.of(key, type));
@@ -64,7 +64,7 @@ public class MetaListLikePreprocessor extends MetaPreprocessor<Meta> {
Pair<Configuration, Object> pair = getMetaValue(meta, depthTracker); Pair<Configuration, Object> pair = getMetaValue(meta, depthTracker);
Object metaValue = pair.getRight(); Object metaValue = pair.right();
if(!(metaValue instanceof List)) { if(!(metaValue instanceof List)) {
throw new LoadException( throw new LoadException(
@@ -86,10 +86,10 @@ public class MetaListLikePreprocessor extends MetaPreprocessor<Meta> {
indexLevel.getIndex() >= begin && indexLevel.getIndex() >= begin &&
indexLevel.getIndex() <= end) { indexLevel.getIndex() <= end) {
String configName; String configName;
if(pair.getLeft().getName() == null) { if(pair.left().getName() == null) {
configName = "Anonymous Configuration"; configName = "Anonymous Configuration";
} else { } else {
configName = pair.getLeft().getName(); configName = pair.left().getName();
} }
return Optional.of("From configuration \"" + configName + "\""); return Optional.of("From configuration \"" + configName + "\"");
} }
@@ -60,7 +60,7 @@ public class MetaMapPreprocessor extends MetaPreprocessor<Meta> {
List<String> keys = (List<String>) loader.loadType(STRING_LIST.getAnnotatedType(), map.get("<<"), depthTracker); List<String> keys = (List<String>) loader.loadType(STRING_LIST.getAnnotatedType(), map.get("<<"), depthTracker);
keys.forEach(key -> { keys.forEach(key -> {
Pair<Configuration, Object> pair = getMetaValue(key, depthTracker); Pair<Configuration, Object> pair = getMetaValue(key, depthTracker);
Object meta = pair.getRight(); Object meta = pair.right();
if(!(meta instanceof Map)) { if(!(meta instanceof Map)) {
throw new LoadException( throw new LoadException(
"MetaMap injection candidate must be list, is type " + meta.getClass().getCanonicalName(), "MetaMap injection candidate must be list, is type " + meta.getClass().getCanonicalName(),
@@ -69,10 +69,10 @@ public class MetaMapPreprocessor extends MetaPreprocessor<Meta> {
newMap.putAll((Map<?, ?>) meta); newMap.putAll((Map<?, ?>) meta);
String configName; String configName;
if(pair.getLeft().getName() == null) { if(pair.left().getName() == null) {
configName = "Anonymous Configuration"; configName = "Anonymous Configuration";
} else { } else {
configName = pair.getLeft().getName(); configName = pair.left().getName();
} }
depthTracker.addIntrinsicLevel(level -> { depthTracker.addIntrinsicLevel(level -> {
@@ -41,7 +41,7 @@ public class MetaStringPreprocessor extends MetaPreprocessor<Meta> {
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation, DepthTracker depthTracker) { public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation, DepthTracker depthTracker) {
if(String.class.equals(t.getType()) && c instanceof String candidate) { // String is final so we use #equals if(String.class.equals(t.getType()) && c instanceof String candidate) { // String is final so we use #equals
StringSubstitutor substitutor = new StringSubstitutor(key -> { StringSubstitutor substitutor = new StringSubstitutor(key -> {
Object meta = getMetaValue(key, depthTracker).getRight(); Object meta = getMetaValue(key, depthTracker).right();
if(!(meta instanceof String) && !(meta instanceof Number) && !(meta instanceof Character) && !(meta instanceof Boolean)) { if(!(meta instanceof String) && !(meta instanceof Number) && !(meta instanceof Character) && !(meta instanceof Boolean)) {
throw new LoadException("MetaString template injection candidate must be string or primitive, is type " + throw new LoadException("MetaString template injection candidate must be string or primitive, is type " +
meta.getClass().getCanonicalName(), depthTracker); meta.getClass().getCanonicalName(), depthTracker);
@@ -46,13 +46,13 @@ public class MetaValuePreprocessor extends MetaPreprocessor<Meta> {
Pair<Configuration, Object> pair = getMetaValue(value.substring(1), depthTracker); Pair<Configuration, Object> pair = getMetaValue(value.substring(1), depthTracker);
String configName; String configName;
if(pair.getLeft().getName() == null) { if(pair.left().getName() == null) {
configName = "Anonymous Configuration"; configName = "Anonymous Configuration";
} else { } else {
configName = pair.getLeft().getName(); configName = pair.left().getName();
} }
return (Result<T>) Result.overwrite(pair.getRight(), depthTracker.intrinsic("From configuration \"" + configName + "\"")); return (Result<T>) Result.overwrite(pair.right(), depthTracker.intrinsic("From configuration \"" + configName + "\""));
} }
} }
return Result.noOp(); return Result.noOp();
@@ -33,7 +33,6 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@@ -158,7 +157,7 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
return objectIDs return objectIDs
.get(id) .get(id)
.stream() .stream()
.collect(HashMap::new, (map, pair) -> map.put(pair.getLeft(), pair.getRight().getValue()), Map::putAll); .collect(HashMap::new, (map, pair) -> map.put(pair.left(), pair.right().getValue()), Map::putAll);
} }
public Map<RegistryKey, T> getDeadEntries() { public Map<RegistryKey, T> getDeadEntries() {
@@ -62,12 +62,12 @@ public final class TerraCLI implements Callable<Integer> {
if(!noSave) { if(!noSave) {
world.serialize().parallel().forEach(mcaFile -> { world.serialize().parallel().forEach(mcaFile -> {
Vector2Int pos = mcaFile.getLeft(); Vector2Int pos = mcaFile.left();
String name = MCAUtil.createNameFromRegionLocation(pos.getX(), pos.getZ()); String name = MCAUtil.createNameFromRegionLocation(pos.getX(), pos.getZ());
LOGGER.info("Writing region ({}, {}) to {}", pos.getX(), pos.getZ(), name); LOGGER.info("Writing region ({}, {}) to {}", pos.getX(), pos.getZ(), name);
try { try {
MCAUtil.write(mcaFile.getRight(), name); MCAUtil.write(mcaFile.right(), name);
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -37,15 +37,15 @@ public final class TagUtil {
PresetUtil PresetUtil
.getPresets() .getPresets()
.forEach(pair -> MinecraftUtil .forEach(pair -> MinecraftUtil
.getEntry(registry, pair.getLeft()) .getEntry(registry, pair.left())
.ifPresentOrElse( .ifPresentOrElse(
preset -> { preset -> {
boolean useExtendedTag = pair.getRight(); // Get the boolean value from the pair boolean useExtendedTag = pair.right(); // Get the boolean value from the pair
collect collect
.computeIfAbsent(useExtendedTag ? WorldPresetTags.EXTENDED : WorldPresetTags.NORMAL, tag -> new ArrayList<>()) .computeIfAbsent(useExtendedTag ? WorldPresetTags.EXTENDED : WorldPresetTags.NORMAL, tag -> new ArrayList<>())
.add(preset); .add(preset);
}, },
() -> logger.error("Preset {} does not exist!", pair.getLeft()))); () -> logger.error("Preset {} does not exist!", pair.left())));
registry.startTagReload(new RegistryTags<>(registry.getKey(), collect)).apply(); registry.startTagReload(new RegistryTags<>(registry.getKey(), collect)).apply();