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
@@ -10,8 +10,6 @@ package com.dfsek.terra.api.handle;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.state.BlockState;
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;
@@ -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);
}
}
@@ -13,40 +13,39 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class Pair<L, R> {
private L left;
private R right;
public final class Pair<L, R> {
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;
}
@NotNull
@Contract("_, _ -> new")
public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) {
return new Pair<>(left, right);
}
@Contract("-> new")
public ImmutablePair<L, R> immutable() {
return ImmutablePair.of(left, right);
@SuppressWarnings("unchecked")
public static <L1, R1> Pair<L1, R1> ofNull() {
return (Pair<L1, R1>) NULL;
}
public L getLeft() {
return left;
}
public void setLeft(L left) {
this.left = left;
@NotNull
@Contract("-> new")
public Pair.Mutable<L, R> mutable() {
return Mutable.of(left, right);
}
public R getRight() {
return right;
}
public void setRight(R right) {
this.right = right;
public L getLeft() {
return left;
}
@Override
@@ -56,9 +55,57 @@ public class Pair<L, R> {
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Pair)) return false;
Pair<?, ?> that = (Pair<?, ?>) 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() {
return left;
}
public void setLeft(L left) {
this.left = left;
}
public R getRight() {
return right;
}
public void setRight(R right) {
this.right = right;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Mutable<?, ?> that)) return false;
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
}
}
}
@@ -71,7 +71,7 @@ import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
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.world.World;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
@@ -116,12 +116,12 @@ public class ConfigPackImpl implements ConfigPack {
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 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 {
try {
@@ -255,7 +255,7 @@ public class ConfigPackImpl implements ConfigPack {
if(contained.contains(pair.getLeft())) throw new IllegalArgumentException("Duplicate config ID: " + 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
@@ -327,7 +327,7 @@ public class ConfigPackImpl implements ConfigPack {
}
}
return ImmutablePair.of(registry, new CheckedRegistryImpl<>(registry));
return Pair.of(registry, new CheckedRegistryImpl<>(registry));
}).getRight();
}
@@ -378,7 +378,7 @@ public class ConfigPackImpl implements ConfigPack {
}
selfLoader.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);
}
protected Map<Type, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() {
protected Map<Type, Pair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() {
return registryMap;
}
@@ -464,18 +464,18 @@ public class ConfigPackImpl implements ConfigPack {
@Override
@SuppressWarnings("unchecked")
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")
@Override
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")
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