refactor TypeToken to TypeKey

This commit is contained in:
dfsek
2021-07-18 14:10:34 -07:00
parent 15f749bfe9
commit 4447005b43
11 changed files with 34 additions and 32 deletions

View File

@@ -6,7 +6,7 @@ import com.dfsek.terra.api.registry.meta.RegistryFactory;
import com.dfsek.terra.api.registry.meta.RegistryHolder;
import com.dfsek.terra.api.tectonic.LoaderHolder;
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.util.reflection.TypeKey;
import com.dfsek.terra.api.util.seeded.SeededBuilder;
import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
@@ -26,7 +26,7 @@ public interface ConfigPack extends LoaderRegistrar, LoaderHolder, RegistryHolde
return getOrCreateRegistry((Type) clazz);
}
default <T> CheckedRegistry<T> getOrCreateRegistry(TypeToken<T> type) {
default <T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> type) {
return getOrCreateRegistry(type.getType());
}

View File

@@ -2,7 +2,7 @@ package com.dfsek.terra.api.config;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.util.reflection.TypeKey;
import java.util.function.Supplier;
@@ -11,7 +11,7 @@ public interface ConfigType<T extends AbstractableTemplate, R> {
ConfigFactory<T, R> getFactory();
TypeToken<R> getTypeClass();
TypeKey<R> getTypeClass();
Supplier<OpenRegistry<R>> registrySupplier();
}

View File

@@ -2,7 +2,7 @@ package com.dfsek.terra.api.registry.meta;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.util.reflection.TypeKey;
import java.lang.reflect.Type;
@@ -11,7 +11,7 @@ public interface RegistryHolder {
return getRegistry((Type) clazz);
}
default <T> Registry<T> getRegistry(TypeToken<T> type) {
default <T> Registry<T> getRegistry(TypeKey<T> type) {
return getRegistry(type.getType());
}
@@ -21,7 +21,7 @@ public interface RegistryHolder {
return getCheckedRegistry((Type) clazz);
}
default <T> CheckedRegistry<T> getCheckedRegistry(TypeToken<T> type) throws IllegalStateException {
default <T> CheckedRegistry<T> getCheckedRegistry(TypeKey<T> type) throws IllegalStateException {
return getCheckedRegistry(type.getType());
}

View File

@@ -1,16 +1,18 @@
package com.dfsek.terra.api.util;
package com.dfsek.terra.api.util.reflection;
import com.dfsek.terra.api.util.ReflectionUtil;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Objects;
public class TypeToken<T> {
public class TypeKey<T> {
final Class<? super T> rawType;
final Type type;
final int hashCode;
@SuppressWarnings("unchecked")
protected TypeToken() {
protected TypeKey() {
this.type = getSuperclassTypeParameter(getClass());
this.rawType = (Class<? super T>) ReflectionUtil.getRawType(type);
this.hashCode = type.hashCode();
@@ -47,8 +49,8 @@ public class TypeToken<T> {
@Override
public final boolean equals(Object o) {
return o instanceof TypeToken<?>
&& equals(type, ((TypeToken<?>) o).type);
return o instanceof TypeKey<?>
&& equals(type, ((TypeKey<?>) o).type);
}
@Override