This commit is contained in:
1 parent
bc8fe44033
commit
16087bdfec
22 files changed
+1019
-28
No files matched your search
@@ -0,0 +1,43 @@
|
||||
package games.dmg.fantasystick.passive;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Internal stable-ID passive catalogue consumed by persistence, books and focused runtime services. */
|
||||
public final class PassiveRegistry {
|
||||
private final Map<String, PassiveDefinition> definitions = new LinkedHashMap<>();
|
||||
|
||||
public synchronized void register(PassiveDefinition definition) {
|
||||
Objects.requireNonNull(definition);
|
||||
if (definitions.containsKey(definition.id())) {
|
||||
throw new IllegalArgumentException("Duplicate passive ID: " + definition.id());
|
||||
}
|
||||
validatePrerequisites(definition);
|
||||
definitions.put(definition.id(), definition);
|
||||
}
|
||||
|
||||
public synchronized void replace(PassiveDefinition definition) {
|
||||
Objects.requireNonNull(definition);
|
||||
if (!definitions.containsKey(definition.id())) throw new IllegalArgumentException("Passive is not registered");
|
||||
validatePrerequisites(definition);
|
||||
definitions.put(definition.id(), definition);
|
||||
}
|
||||
|
||||
private void validatePrerequisites(PassiveDefinition definition) {
|
||||
if (!definitions.keySet().containsAll(definition.prerequisites())) {
|
||||
throw new IllegalArgumentException("Passive prerequisite is not registered");
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Optional<PassiveDefinition> find(String id) {
|
||||
return Optional.ofNullable(definitions.get(id));
|
||||
}
|
||||
public synchronized Collection<PassiveDefinition> definitions() { return java.util.List.copyOf(definitions.values()); }
|
||||
public synchronized java.util.Set<String> dependents(String id) {
|
||||
return definitions.values().stream().filter(definition -> definition.prerequisites().contains(id))
|
||||
.map(PassiveDefinition::id).collect(java.util.stream.Collectors.toUnmodifiableSet());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user