refactor Pairs

This commit is contained in:
dfsek 2021-11-23 21:00:35 -07:00
parent d17dcc17b0
commit 07520b9014
6 changed files with 100 additions and 117 deletions

View File

@ -10,8 +10,6 @@ package com.dfsek.terra.api.handle;
import com.dfsek.terra.api.block.entity.BlockEntity; import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.entity.EntityType; import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.api.util.generic.pair.Pair;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.Vector3;

View File

@ -1,63 +0,0 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.util.generic.pair;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public final class ImmutablePair<L, R> {
private static final ImmutablePair<?, ?> NULL = new ImmutablePair<>(null, null);
private final L left;
private final R right;
private ImmutablePair(L left, R right) {
this.left = left;
this.right = right;
}
@Contract("_, _ -> new")
public static <L1, R1> ImmutablePair<L1, R1> of(L1 left, R1 right) {
return new ImmutablePair<>(left, right);
}
@Contract("-> new")
@SuppressWarnings("unchecked")
public static <L1, R1> ImmutablePair<L1, R1> ofNull() {
return (ImmutablePair<L1, R1>) NULL;
}
@NotNull
@Contract("-> new")
public Pair<L, R> mutable() {
return Pair.of(left, right);
}
public R getRight() {
return right;
}
public L getLeft() {
return left;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ImmutablePair)) return false;
ImmutablePair<?, ?> that = (ImmutablePair<?, ?>) obj;
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
}
}

View File

@ -13,24 +13,71 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects; import java.util.Objects;
public class Pair<L, R> { public final class Pair<L, R> {
private L left; private static final Pair<?, ?> NULL = new Pair<>(null, null);
private R right; private final L left;
private final R right;
private Pair(L left, R right) { private Pair(L left, R right) {
this.left = left; this.left = left;
this.right = right; this.right = right;
} }
@NotNull
@Contract("_, _ -> new") @Contract("_, _ -> new")
public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) { public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) {
return new Pair<>(left, right); return new Pair<>(left, right);
} }
@Contract("-> new") @Contract("-> new")
public ImmutablePair<L, R> immutable() { @SuppressWarnings("unchecked")
return ImmutablePair.of(left, right); public static <L1, R1> Pair<L1, R1> ofNull() {
return (Pair<L1, R1>) NULL;
}
@NotNull
@Contract("-> new")
public Pair.Mutable<L, R> mutable() {
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
public boolean equals(Object obj) {
if(!(obj instanceof Pair<?, ?> that)) return false;
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
}
public static class Mutable<L, R> {
private L left;
private R right;
private Mutable(L left, R right) {
this.left = left;
this.right = right;
}
@NotNull
@Contract("_, _ -> new")
public static <L1, R1> Pair.Mutable<L1, R1> of(L1 left, R1 right) {
return new Mutable<>(left, right);
}
@Contract("-> new")
public Pair<L, R> immutable() {
return Pair.of(left, right);
} }
public L getLeft() { public L getLeft() {
@ -56,9 +103,9 @@ public class Pair<L, R> {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof Pair)) return false; if(!(obj instanceof Mutable<?, ?> that)) return false;
Pair<?, ?> that = (Pair<?, ?>) obj;
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right); return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
} }
}
} }

View File

@ -71,7 +71,7 @@ import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.registry.Registry; import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException; import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
import com.dfsek.terra.api.registry.meta.RegistryFactory; import com.dfsek.terra.api.registry.meta.RegistryFactory;
import com.dfsek.terra.api.util.generic.pair.ImmutablePair; import com.dfsek.terra.api.util.generic.pair.Pair;
import com.dfsek.terra.api.util.reflection.ReflectionUtil; import com.dfsek.terra.api.util.reflection.ReflectionUtil;
import com.dfsek.terra.api.world.World; import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider; import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
@ -116,12 +116,12 @@ public class ConfigPackImpl implements ConfigPack {
private final BiomeProvider seededBiomeProvider; private final BiomeProvider seededBiomeProvider;
private final Map<Type, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> registryMap = new HashMap<>(); private final Map<Type, Pair<OpenRegistry<?>, CheckedRegistry<?>>> registryMap = new HashMap<>();
private final ConfigTypeRegistry configTypeRegistry; private final ConfigTypeRegistry configTypeRegistry;
private final TreeMap<Integer, List<ImmutablePair<String, ConfigType<?, ?>>>> configTypes = new TreeMap<>(); private final TreeMap<Integer, List<Pair<String, ConfigType<?, ?>>>> configTypes = new TreeMap<>();
public ConfigPackImpl(File folder, Platform platform) throws ConfigException { public ConfigPackImpl(File folder, Platform platform) throws ConfigException {
try { try {
@ -255,7 +255,7 @@ public class ConfigPackImpl implements ConfigPack {
if(contained.contains(pair.getLeft())) throw new IllegalArgumentException("Duplicate config ID: " + id); if(contained.contains(pair.getLeft())) throw new IllegalArgumentException("Duplicate config ID: " + id);
contained.add(id); contained.add(id);
})); }));
configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(ImmutablePair.of(id, type)); configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(Pair.of(id, type));
} }
@Override @Override
@ -327,7 +327,7 @@ public class ConfigPackImpl implements ConfigPack {
} }
} }
return ImmutablePair.of(registry, new CheckedRegistryImpl<>(registry)); return Pair.of(registry, new CheckedRegistryImpl<>(registry));
}).getRight(); }).getRight();
} }
@ -378,7 +378,7 @@ public class ConfigPackImpl implements ConfigPack {
} }
selfLoader.registerLoader(configType.getTypeKey().getType(), openRegistry); selfLoader.registerLoader(configType.getTypeKey().getType(), openRegistry);
abstractConfigLoader.registerLoader(configType.getTypeKey().getType(), openRegistry); abstractConfigLoader.registerLoader(configType.getTypeKey().getType(), openRegistry);
registryMap.put(configType.getTypeKey().getType(), ImmutablePair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry))); registryMap.put(configType.getTypeKey().getType(), Pair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry)));
}); });
} }
@ -453,7 +453,7 @@ public class ConfigPackImpl implements ConfigPack {
template.getID(), template.getVersion(), template.getAuthor(), (System.nanoTime() - start) / 1000000.0D); template.getID(), template.getVersion(), template.getAuthor(), (System.nanoTime() - start) / 1000000.0D);
} }
protected Map<Type, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() { protected Map<Type, Pair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() {
return registryMap; return registryMap;
} }
@ -464,18 +464,18 @@ public class ConfigPackImpl implements ConfigPack {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> CheckedRegistry<T> getRegistry(Type type) { public <T> CheckedRegistry<T> getRegistry(Type type) {
return (CheckedRegistry<T>) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight(); return (CheckedRegistry<T>) registryMap.getOrDefault(type, Pair.ofNull()).getRight();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException { public <T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException {
return (CheckedRegistry<T>) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight(); return (CheckedRegistry<T>) registryMap.getOrDefault(type, Pair.ofNull()).getRight();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> OpenRegistry<T> getOpenRegistry(Class<T> clazz) { protected <T> OpenRegistry<T> getOpenRegistry(Class<T> clazz) {
return (OpenRegistry<T>) registryMap.getOrDefault(clazz, ImmutablePair.ofNull()).getLeft(); return (OpenRegistry<T>) registryMap.getOrDefault(clazz, Pair.ofNull()).getLeft();
} }
@Override @Override

View File

@ -23,6 +23,8 @@ import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.api.addon.BaseAddon; import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.util.generic.pair.Pair.Mutable;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BuiltinRegistries; import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@ -40,7 +42,6 @@ import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler; import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException; import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
import com.dfsek.terra.api.util.generic.pair.Pair;
import com.dfsek.terra.api.world.Tree; import com.dfsek.terra.api.world.Tree;
import com.dfsek.terra.api.world.biome.TerraBiome; import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.fabric.config.PostLoadCompatibilityOptions; import com.dfsek.terra.fabric.config.PostLoadCompatibilityOptions;
@ -54,7 +55,7 @@ public final class FabricAddon implements BaseAddon {
private final PlatformImpl terraFabricPlugin; private final PlatformImpl terraFabricPlugin;
private static final Logger logger = LoggerFactory.getLogger(FabricAddon.class); private static final Logger logger = LoggerFactory.getLogger(FabricAddon.class);
private final Map<ConfigPack, Pair<PreLoadCompatibilityOptions, PostLoadCompatibilityOptions>> templates = new HashMap<>(); private final Map<ConfigPack, Mutable<PreLoadCompatibilityOptions, PostLoadCompatibilityOptions>> templates = new HashMap<>();
public FabricAddon(PlatformImpl terraFabricPlugin) { public FabricAddon(PlatformImpl terraFabricPlugin) {
this.terraFabricPlugin = terraFabricPlugin; this.terraFabricPlugin = terraFabricPlugin;
@ -88,7 +89,7 @@ public final class FabricAddon implements BaseAddon {
} }
}); });
} }
templates.put(event.getPack(), Pair.of(template, null)); templates.put(event.getPack(), Mutable.of(template, null));
}) })
.global(); .global();
@ -143,7 +144,7 @@ public final class FabricAddon implements BaseAddon {
} }
} }
public Map<ConfigPack, Pair<PreLoadCompatibilityOptions, PostLoadCompatibilityOptions>> getTemplates() { public Map<ConfigPack, Mutable<PreLoadCompatibilityOptions, PostLoadCompatibilityOptions>> getTemplates() {
return templates; return templates;
} }

View File

@ -35,52 +35,52 @@ import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.block.state.properties.Property; import com.dfsek.terra.api.block.state.properties.Property;
import com.dfsek.terra.api.block.state.properties.base.Properties; import com.dfsek.terra.api.block.state.properties.base.Properties;
import com.dfsek.terra.api.util.generic.Construct; import com.dfsek.terra.api.util.generic.Construct;
import com.dfsek.terra.api.util.generic.pair.ImmutablePair; import com.dfsek.terra.api.util.generic.pair.Pair;
import com.dfsek.terra.fabric.mixin.access.StateAccessor; import com.dfsek.terra.fabric.mixin.access.StateAccessor;
import com.dfsek.terra.fabric.util.FabricAdapter; import com.dfsek.terra.fabric.util.FabricAdapter;
public class FabricBlockState implements BlockState { public class FabricBlockState implements BlockState {
private static final Map<Property<?>, ImmutablePair<net.minecraft.state.property.Property<?>, Function<Object, Object>>> private static final Map<Property<?>, Pair<net.minecraft.state.property.Property<?>, Function<Object, Object>>>
PROPERTY_DELEGATES_T2M = Construct.construct(() -> { PROPERTY_DELEGATES_T2M = Construct.construct(() -> {
Map<Property<?>, ImmutablePair<net.minecraft.state.property.Property<?>, Function<Object, Object>>> map = new HashMap<>(); Map<Property<?>, Pair<net.minecraft.state.property.Property<?>, Function<Object, Object>>> map = new HashMap<>();
map.put(Properties.AXIS, ImmutablePair.of(net.minecraft.state.property.Properties.AXIS, map.put(Properties.AXIS, Pair.of(net.minecraft.state.property.Properties.AXIS,
a -> FabricAdapter.adapt((net.minecraft.util.math.Direction.Axis) a))); a -> FabricAdapter.adapt((net.minecraft.util.math.Direction.Axis) a)));
map.put(Properties.NORTH, ImmutablePair.of(net.minecraft.state.property.Properties.NORTH, Function.identity())); map.put(Properties.NORTH, Pair.of(net.minecraft.state.property.Properties.NORTH, Function.identity()));
map.put(Properties.SOUTH, ImmutablePair.of(net.minecraft.state.property.Properties.SOUTH, Function.identity())); map.put(Properties.SOUTH, Pair.of(net.minecraft.state.property.Properties.SOUTH, Function.identity()));
map.put(Properties.EAST, ImmutablePair.of(net.minecraft.state.property.Properties.EAST, Function.identity())); map.put(Properties.EAST, Pair.of(net.minecraft.state.property.Properties.EAST, Function.identity()));
map.put(Properties.WEST, ImmutablePair.of(net.minecraft.state.property.Properties.WEST, Function.identity())); map.put(Properties.WEST, Pair.of(net.minecraft.state.property.Properties.WEST, Function.identity()));
map.put(Properties.NORTH_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.NORTH_WIRE_CONNECTION, map.put(Properties.NORTH_CONNECTION, Pair.of(net.minecraft.state.property.Properties.NORTH_WIRE_CONNECTION,
c -> FabricAdapter.adapt((WireConnection) c))); c -> FabricAdapter.adapt((WireConnection) c)));
map.put(Properties.SOUTH_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.SOUTH_WIRE_CONNECTION, map.put(Properties.SOUTH_CONNECTION, Pair.of(net.minecraft.state.property.Properties.SOUTH_WIRE_CONNECTION,
c -> FabricAdapter.adapt((WireConnection) c))); c -> FabricAdapter.adapt((WireConnection) c)));
map.put(Properties.EAST_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.EAST_WIRE_CONNECTION, map.put(Properties.EAST_CONNECTION, Pair.of(net.minecraft.state.property.Properties.EAST_WIRE_CONNECTION,
c -> FabricAdapter.adapt((WireConnection) c))); c -> FabricAdapter.adapt((WireConnection) c)));
map.put(Properties.WEST_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.WEST_WIRE_CONNECTION, map.put(Properties.WEST_CONNECTION, Pair.of(net.minecraft.state.property.Properties.WEST_WIRE_CONNECTION,
c -> FabricAdapter.adapt((WireConnection) c))); c -> FabricAdapter.adapt((WireConnection) c)));
map.put(Properties.NORTH_HEIGHT, map.put(Properties.NORTH_HEIGHT,
ImmutablePair.of(net.minecraft.state.property.Properties.NORTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); Pair.of(net.minecraft.state.property.Properties.NORTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h)));
map.put(Properties.SOUTH_HEIGHT, map.put(Properties.SOUTH_HEIGHT,
ImmutablePair.of(net.minecraft.state.property.Properties.SOUTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); Pair.of(net.minecraft.state.property.Properties.SOUTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h)));
map.put(Properties.EAST_HEIGHT, map.put(Properties.EAST_HEIGHT,
ImmutablePair.of(net.minecraft.state.property.Properties.EAST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); Pair.of(net.minecraft.state.property.Properties.EAST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h)));
map.put(Properties.WEST_HEIGHT, map.put(Properties.WEST_HEIGHT,
ImmutablePair.of(net.minecraft.state.property.Properties.WEST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); Pair.of(net.minecraft.state.property.Properties.WEST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h)));
map.put(Properties.DIRECTION, map.put(Properties.DIRECTION,
ImmutablePair.of(net.minecraft.state.property.Properties.FACING, d -> FabricAdapter.adapt((Direction) d))); Pair.of(net.minecraft.state.property.Properties.FACING, d -> FabricAdapter.adapt((Direction) d)));
map.put(Properties.WATERLOGGED, ImmutablePair.of(net.minecraft.state.property.Properties.WATERLOGGED, Function.identity())); map.put(Properties.WATERLOGGED, Pair.of(net.minecraft.state.property.Properties.WATERLOGGED, Function.identity()));
map.put(Properties.RAIL_SHAPE, map.put(Properties.RAIL_SHAPE,
ImmutablePair.of(net.minecraft.state.property.Properties.RAIL_SHAPE, s -> FabricAdapter.adapt((RailShape) s))); Pair.of(net.minecraft.state.property.Properties.RAIL_SHAPE, s -> FabricAdapter.adapt((RailShape) s)));
map.put(Properties.HALF, map.put(Properties.HALF,
ImmutablePair.of(net.minecraft.state.property.Properties.BLOCK_HALF, h -> FabricAdapter.adapt((BlockHalf) h))); Pair.of(net.minecraft.state.property.Properties.BLOCK_HALF, h -> FabricAdapter.adapt((BlockHalf) h)));
return map; return map;
}); });
@ -110,7 +110,7 @@ public class FabricBlockState implements BlockState {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T get(Property<T> property) { public <T> T get(Property<T> property) {
ImmutablePair<net.minecraft.state.property.Property<?>, Function<Object, Object>> pair = PROPERTY_DELEGATES_T2M.get(property); Pair<net.minecraft.state.property.Property<?>, Function<Object, Object>> pair = PROPERTY_DELEGATES_T2M.get(property);
return (T) pair.getRight().apply(delegate.get(pair.getLeft())); return (T) pair.getRight().apply(delegate.get(pair.getLeft()));
} }