cache value of ModDependentConfigSection

This commit is contained in:
dfsek 2021-06-01 01:33:54 -07:00
parent 788a3c2d48
commit ed2fa85d81

View File

@ -9,9 +9,12 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ModDependentConfigSection<T> { public class ModDependentConfigSection<T> {
private static final Object NULL = new Object(); // Null object
private final TerraPlugin main; private final TerraPlugin main;
private final Map<String, T> results = new HashMap<>(); private final Map<String, T> results = new HashMap<>();
private final T defaultValue; private final T defaultValue;
@SuppressWarnings("unchecked")
private T value = (T) NULL;
protected ModDependentConfigSection(TerraPlugin main, T defaultValue) { protected ModDependentConfigSection(TerraPlugin main, T defaultValue) {
this.main = main; this.main = main;
@ -27,6 +30,11 @@ public class ModDependentConfigSection<T> {
} }
public T get() { public T get() {
if(value == NULL) value = compute(); // Cache the value.
return value;
}
private T compute() {
if(main != null) { if(main != null) {
Set<String> mods = main.getMods().stream().map(Mod::getID).collect(Collectors.toSet()); Set<String> mods = main.getMods().stream().map(Mod::getID).collect(Collectors.toSet());
for(Map.Entry<String, T> entry : results.entrySet()) { for(Map.Entry<String, T> entry : results.entrySet()) {