Container performance

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-01-14 10:50:54 +01:00
parent 7a9b08b085
commit 374e534c0a

View File

@ -68,20 +68,22 @@ public final class Registry {
@ApiStatus.Internal @ApiStatus.Internal
public static <T extends ProtocolObject> Container<T> createContainer(Resource resource, Container.Loader<T> loader) { public static <T extends ProtocolObject> Container<T> createContainer(Resource resource, Container.Loader<T> loader) {
Map<String, T> namespaces = new HashMap<>(); var entries = Registry.load(resource);
ObjectArray<T> ids = new ObjectArray<>(); Map<String, T> namespaces = new HashMap<>(entries.size());
for (var entry : Registry.load(resource).entrySet()) { ObjectArray<T> ids = new ObjectArray<>(entries.size());
for (var entry : entries.entrySet()) {
final String namespace = entry.getKey(); final String namespace = entry.getKey();
final Map<String, Object> object = entry.getValue(); final Map<String, Object> object = entry.getValue();
final T value = loader.get(namespace, object); final T value = loader.get(namespace, object);
ids.set(value.id(), value); ids.set(value.id(), value);
namespaces.put(value.name(), value); namespaces.put(value.name(), value);
} }
return new Container<>(namespaces, ids); return new Container<>(resource, namespaces, ids);
} }
@ApiStatus.Internal @ApiStatus.Internal
public record Container<T extends ProtocolObject>(Map<String, T> namespaces, public record Container<T extends ProtocolObject>(Resource resource,
Map<String, T> namespaces,
ObjectArray<T> ids) { ObjectArray<T> ids) {
public Container { public Container {
namespaces = Map.copyOf(namespaces); namespaces = Map.copyOf(namespaces);
@ -104,6 +106,18 @@ public final class Registry {
return namespaces.values(); return namespaces.values();
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Container<?> container)) return false;
return resource == container.resource;
}
@Override
public int hashCode() {
return Objects.hash(resource);
}
public interface Loader<T extends ProtocolObject> { public interface Loader<T extends ProtocolObject> {
T get(String namespace, Map<String, Object> object); T get(String namespace, Map<String, Object> object);
} }