refactor(registry): outsource entry discovery to seperate method
Some checks failed
github-mirror / push-github (push) Successful in 1m39s
Build / Gradle-Build (push) Failing after 37s
Test / Gradle-Test (push) Failing after 34s

This commit is contained in:
tueem 2025-03-26 19:26:57 +01:00
parent a3c5eb62ac
commit 2647a1f0b4
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB
2 changed files with 23 additions and 14 deletions

View File

@ -1,6 +1,8 @@
package net.tomatentum.marinara.registry;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
@ -11,6 +13,17 @@ import net.tomatentum.marinara.interaction.methods.InteractionMethod;
import net.tomatentum.marinara.util.LoggerUtil;
public class InteractionEntry {
public static InteractionEntry findEntry(Set<InteractionEntry> entries, InteractionIdentifier identifier) {
Optional<InteractionEntry> oentry = entries.stream()
.filter(i -> i.identifier().equals(identifier))
.findFirst();
InteractionEntry entry = oentry.orElse(new InteractionEntry(identifier));
if (oentry.isEmpty()) entries.add(entry);
return entry;
}
private InteractionIdentifier identifier;
private Set<InteractionMethod> methods;
@ -22,11 +35,16 @@ public class InteractionEntry {
}
public InteractionEntry addMethod(InteractionMethod method) {
if (!method.identifier().equals(this.identifier))
throw new IllegalArgumentException("Method's identifier did not match the entry's identifier");
Optional<InteractionIdentifier> methodId = Arrays.stream(method.identifier())
.filter(x -> x.name().equals(identifier().name()))
.findFirst();
if (methodId.isEmpty())
throw new IllegalArgumentException("Method's identifiers did not contain the entry's identifier");
this.methods.add(method);
InteractionIdentifier.tryAddDescriptions(identifier, method.identifier());
InteractionIdentifier.tryAddDescriptions(identifier, methodId.get());
logger.debug("Added method {} to entry {}", method.method().getName(), this.identifier);
return this;
}

View File

@ -4,7 +4,6 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
@ -32,20 +31,12 @@ public class InteractionRegistry {
this.identifierProvider = marinara.getWrapper().createIdentifierProvider();
marinara.getWrapper().subscribeInteractions(this::handle);
}
/*
* TODO: Maybe relocate InteractionEntry checking to another class with description merging.
*/
public void addInteractions(InteractionHandler interactionHandler) {
for (Method method : interactionHandler.getClass().getMethods()) {
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara);
if (iMethod != null) {
Optional<InteractionEntry> oentry = this.interactions.stream()
.filter(i -> i.identifier().equals(iMethod.identifier()))
.findFirst();
InteractionEntry entry = oentry.orElse(new InteractionEntry(iMethod.identifier())).addMethod(iMethod);
if (oentry.isEmpty()) this.interactions.add(entry);
InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod);
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
}
}