feat(struct): introduce ObjectAggregator
This commit is contained in:
		| @@ -1,7 +1,6 @@ | |||||||
| package net.tomatentum.marinara.registry; | package net.tomatentum.marinara.registry; | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.ArrayList; |  | ||||||
| import java.util.HashSet; | import java.util.HashSet; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
| @@ -13,9 +12,10 @@ import net.tomatentum.marinara.Marinara; | |||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
| import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; | import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
| import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier; | import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier; | ||||||
| import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier; |  | ||||||
| import net.tomatentum.marinara.util.LoggerUtil; | import net.tomatentum.marinara.util.LoggerUtil; | ||||||
|  | import net.tomatentum.marinara.util.ObjectAggregator; | ||||||
| import net.tomatentum.marinara.wrapper.IdentifierProvider; | import net.tomatentum.marinara.wrapper.IdentifierProvider; | ||||||
| import net.tomatentum.marinara.interaction.methods.InteractionMethod; | import net.tomatentum.marinara.interaction.methods.InteractionMethod; | ||||||
|  |  | ||||||
| @@ -51,26 +51,19 @@ public class InteractionRegistry { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void registerCommands() { |     public void registerCommands() { | ||||||
|         List<SlashCommandDefinition> defs = new ArrayList<>(); |         List<InteractionIdentifier> slashIdentifiers = interactions.stream() | ||||||
|         List<SlashCommandIdentifier> slashIdentifiers = interactions.stream() |  | ||||||
|             .filter((x) -> x.type().equals(InteractionType.COMMAND)) |             .filter((x) -> x.type().equals(InteractionType.COMMAND)) | ||||||
|             .map((x) -> (SlashCommandIdentifier)x.identifier()) |             .map((x) -> x.identifier()) | ||||||
|             .toList(); |             .toList(); | ||||||
|  |  | ||||||
|         slashIdentifiers.forEach((ident) -> { |         SlashCommandDefinition[] defs = new ObjectAggregator<InteractionIdentifier, RootCommandIdentifier, SlashCommandDefinition>( | ||||||
|             Optional<SlashCommandDefinition> appDef = defs.stream() |             i -> (RootCommandIdentifier)i.rootNode(), | ||||||
|                 .filter((x) -> x.rootIdentifier().equals(ident.rootNode())) |             SlashCommandDefinition::addIdentifier, | ||||||
|                 .findFirst(); |             SlashCommandDefinition::new) | ||||||
|  |             .aggregate(slashIdentifiers) | ||||||
|  |             .toArray(SlashCommandDefinition[]::new); | ||||||
|  |  | ||||||
|             if (appDef.isPresent()) |         marinara.getWrapper().registerSlashCommands(defs); | ||||||
|                 appDef.get().addIdentifier(ident); |  | ||||||
|             else |  | ||||||
|                 defs.add( |  | ||||||
|                     new SlashCommandDefinition((RootCommandIdentifier) ident.rootNode()) |  | ||||||
|                         .addIdentifier(ident)); |  | ||||||
|         }); |  | ||||||
|  |  | ||||||
|         marinara.getWrapper().registerSlashCommands(defs.toArray(SlashCommandDefinition[]::new)); |  | ||||||
|         logger.info("Registered all SlashCommands"); |         logger.info("Registered all SlashCommands"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -0,0 +1,31 @@ | |||||||
|  | package net.tomatentum.marinara.util; | ||||||
|  |  | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.function.BiConsumer; | ||||||
|  | import java.util.function.Function; | ||||||
|  |  | ||||||
|  | public class ObjectAggregator<O, K, V> { | ||||||
|  |     private Function<O, K> keySupplier; | ||||||
|  |     private BiConsumer<V, O> valueConsumer; | ||||||
|  |     private Function<K, V> defaultGenerator; | ||||||
|  |  | ||||||
|  |     public ObjectAggregator(Function<O, K> keySupplier, BiConsumer<V, O> valueConsumer, Function<K, V> defaultGenerator) { | ||||||
|  |         this.keySupplier = keySupplier; | ||||||
|  |         this.valueConsumer = valueConsumer; | ||||||
|  |         this.defaultGenerator = defaultGenerator; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Collection<V> aggregate(Iterable<O> iterator) { | ||||||
|  |         Map<K, V> map = new HashMap<>(); | ||||||
|  |         for (O element : iterator) { | ||||||
|  |             K key = this.keySupplier.apply(element); | ||||||
|  |             V value = map.getOrDefault(key, this.defaultGenerator.apply(key)); | ||||||
|  |             this.valueConsumer.accept(value, element); | ||||||
|  |             map.putIfAbsent(key, value); | ||||||
|  |         } | ||||||
|  |         return map.values(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user