WIP: add Message-Components handling/registry #21
| @@ -1,6 +1,8 @@ | |||||||
| package net.tomatentum.marinara.registry; | package net.tomatentum.marinara.registry; | ||||||
|  |  | ||||||
|  | import java.util.Arrays; | ||||||
| import java.util.HashSet; | import java.util.HashSet; | ||||||
|  | import java.util.Optional; | ||||||
| import java.util.Set; | import java.util.Set; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| @@ -11,6 +13,17 @@ import net.tomatentum.marinara.interaction.methods.InteractionMethod; | |||||||
| import net.tomatentum.marinara.util.LoggerUtil; | import net.tomatentum.marinara.util.LoggerUtil; | ||||||
|  |  | ||||||
| public class InteractionEntry { | 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 InteractionIdentifier identifier; | ||||||
|     private Set<InteractionMethod> methods; |     private Set<InteractionMethod> methods; | ||||||
|  |  | ||||||
| @@ -22,11 +35,16 @@ public class InteractionEntry { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public InteractionEntry addMethod(InteractionMethod method) { |     public InteractionEntry addMethod(InteractionMethod method) { | ||||||
|         if (!method.identifier().equals(this.identifier)) |         Optional<InteractionIdentifier> methodId = Arrays.stream(method.identifier()) | ||||||
|             throw new IllegalArgumentException("Method's identifier did not match the entry's 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); |         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; |         return this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,7 +4,6 @@ import java.lang.reflect.Method; | |||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.HashSet; | import java.util.HashSet; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; |  | ||||||
| import java.util.Set; | import java.util.Set; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| @@ -33,19 +32,11 @@ public class InteractionRegistry { | |||||||
|         marinara.getWrapper().subscribeInteractions(this::handle); |         marinara.getWrapper().subscribeInteractions(this::handle); | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     /* |  | ||||||
|      * TODO: Maybe relocate InteractionEntry checking to another class with description merging. |  | ||||||
|      */ |  | ||||||
|     public void addInteractions(InteractionHandler interactionHandler) { |     public void addInteractions(InteractionHandler interactionHandler) { | ||||||
|         for (Method method : interactionHandler.getClass().getMethods()) { |         for (Method method : interactionHandler.getClass().getMethods()) { | ||||||
|             InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara); |             InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara); | ||||||
|             if (iMethod != null) { |             if (iMethod != null) { | ||||||
|                 Optional<InteractionEntry> oentry = this.interactions.stream() |                 InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod); | ||||||
|                     .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); |  | ||||||
|                 logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName()); |                 logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName()); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user