mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-06 07:46:13 +00:00
make registry return optional for get operations
This commit is contained in:
@@ -43,12 +43,11 @@ public class GenericTemplateSupplierLoader<T> implements TypeLoader<T> {
|
||||
public T load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
|
||||
Map<String, Object> map = (Map<String, Object>) c;
|
||||
try {
|
||||
if(!registry.contains((String) map.get("type"))) {
|
||||
throw new LoadException("No such entry: " + map.get("type"));
|
||||
}
|
||||
ObjectTemplate<T> template = registry.get(((String) map.get("type"))).get();
|
||||
loader.load(template, new MapConfiguration(map));
|
||||
return template.get();
|
||||
return loader
|
||||
.load(registry
|
||||
.get(((String) map.get("type")))
|
||||
.orElseThrow(() -> new LoadException("No such entry: " + map.get("type")))
|
||||
.get(), new MapConfiguration(map)).get();
|
||||
} catch(ConfigException e) {
|
||||
throw new LoadException("Unable to load object: ", e);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
@@ -50,7 +51,7 @@ public class CheckedRegistryImpl<T> implements CheckedRegistry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T get(@NotNull String identifier) {
|
||||
public Optional<T> get(@NotNull String identifier) {
|
||||
return registry.get(identifier);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
@@ -44,7 +45,7 @@ public class LockedRegistryImpl<T> implements Registry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T get(@NotNull String identifier) {
|
||||
public Optional<T> get(@NotNull String identifier) {
|
||||
return registry.get(identifier);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,13 @@ package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.BiConsumer;
|
||||
@@ -34,10 +36,6 @@ import java.util.stream.Collectors;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
|
||||
import com.dfsek.terra.api.registry.exception.NoSuchEntryException;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* Registry implementation with read/write access. For internal use only.
|
||||
@@ -59,17 +57,12 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
|
||||
@Override
|
||||
public T load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException {
|
||||
T obj;
|
||||
try {
|
||||
obj = get((String) o);
|
||||
} catch(NoSuchEntryException e) {
|
||||
return get((String) o).orElseThrow(() -> {
|
||||
String list = objects.keySet().stream().sorted().reduce("", (a, b) -> a + "\n - " + b);
|
||||
if(objects.isEmpty()) list = "[ ]";
|
||||
throw new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " + list);
|
||||
}
|
||||
|
||||
return obj;
|
||||
return new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " + list);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,12 +94,8 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public @NotNull T get(@NotNull String identifier) {
|
||||
T value = objects.getOrDefault(identifier, (Entry<T>) NULL).getValue();
|
||||
if(value == null) {
|
||||
throw new NoSuchEntryException("Entry " + identifier + " is not present in registry.");
|
||||
}
|
||||
return value;
|
||||
public Optional<T> get(@NotNull String identifier) {
|
||||
return Optional.ofNullable(objects.getOrDefault(identifier, (Entry<T>) NULL).getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user