make registry return optional for get operations

This commit is contained in:
dfsek
2021-12-01 17:48:41 -07:00
parent 4cc07a7b02
commit a69be58b58
17 changed files with 73 additions and 86 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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