optimize by name lookup of properties

This commit is contained in:
Pyrbu 2023-05-24 17:02:28 +01:00
parent b71207d07a
commit b7a8776162
2 changed files with 11 additions and 17 deletions

@ -9,16 +9,7 @@ public class EntityPropertyImpl<T> implements EntityProperty<T> {
private final Class<T> clazz; private final Class<T> clazz;
private final PropertySerializer<T> serializer; private final PropertySerializer<T> serializer;
protected EntityPropertyImpl(String name, Class<T> type, PropertySerializer<T> serializer) { protected EntityPropertyImpl(String name, T defaultValue, Class<T> clazz, PropertySerializer<T> serializer) {
this(name, null, type, serializer);
}
@SuppressWarnings("unchecked")
protected EntityPropertyImpl(String name, T defaultValue, PropertySerializer<T> serializer) {
this(name, defaultValue, (Class<T>) defaultValue.getClass(), serializer);
}
private EntityPropertyImpl(String name, T defaultValue, Class<T> clazz, PropertySerializer<T> serializer) {
this.name = name.toLowerCase(); this.name = name.toLowerCase();
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.clazz = clazz; this.clazz = clazz;

@ -10,15 +10,13 @@ import lol.pyr.znpcsplus.skin.cache.SkinCache;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
private final Map<Class<?>, PropertySerializer<?>> serializerMap = new HashMap<>(); private final Map<Class<?>, PropertySerializer<?>> serializerMap = new HashMap<>();
private final List<EntityPropertyImpl<?>> properties = new ArrayList<>(); private final Map<String, EntityPropertyImpl<?>> byName = new HashMap<>();
public EntityPropertyRegistryImpl(SkinCache skinCache) { public EntityPropertyRegistryImpl(SkinCache skinCache) {
registerSerializer(new BooleanPropertySerializer()); registerSerializer(new BooleanPropertySerializer());
@ -33,6 +31,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerType("silent", false); registerType("silent", false);
registerType("skin", SkinDescriptor.class); registerType("skin", SkinDescriptor.class);
registerType("name", Component.class); registerType("name", Component.class);
registerType("look", false);
} }
private void registerSerializer(PropertySerializer<?> serializer) { private void registerSerializer(PropertySerializer<?> serializer) {
@ -40,11 +39,16 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
} }
private <T> void registerType(String name, Class<T> type) { private <T> void registerType(String name, Class<T> type) {
properties.add(new EntityPropertyImpl<>(name, type, (PropertySerializer<T>) serializerMap.get(type))); registerType(name, null, type);
} }
private <T> void registerType(String name, T defaultValue) { private <T> void registerType(String name, T defaultValue) {
properties.add(new EntityPropertyImpl<>(name, defaultValue, (PropertySerializer<T>) serializerMap.get(defaultValue.getClass()))); registerType(name, defaultValue, (Class<T>) defaultValue.getClass());
}
private <T> void registerType(String name, T defaultValue, Class<T> clazz) {
EntityPropertyImpl<T> property = new EntityPropertyImpl<>(name, defaultValue, clazz, (PropertySerializer<T>) serializerMap.get(clazz));
byName.put(name.toLowerCase(), property);
} }
public <T> EntityPropertyImpl<T> getByName(String name, Class<T> type) { public <T> EntityPropertyImpl<T> getByName(String name, Class<T> type) {
@ -52,7 +56,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
} }
public EntityPropertyImpl<?> getByName(String name) { public EntityPropertyImpl<?> getByName(String name) {
for (EntityPropertyImpl<?> property : properties) if (property.getName().equalsIgnoreCase(name)) return property; return byName.get(name.toLowerCase());
return null;
} }
} }