diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractClassLevelListenerScanner.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractClassLevelListenerScanner.java index 15ba53f28..67408b10d 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractClassLevelListenerScanner.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractClassLevelListenerScanner.java @@ -21,11 +21,13 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; +import java.util.function.Supplier; import java.util.stream.Collectors; import static io.github.stavshamir.springwolf.asyncapi.MessageHelper.toMessageObjectOrComposition; @@ -42,6 +44,11 @@ public abstract class AbstractClassLevelListenerScanner< private final SchemasService schemasService; + private static final Comparator> byPublishOperationName = + Comparator.comparing(it -> it.getValue().getPublish().getOperationId()); + private static final Supplier>> channelItemSupplier = + () -> new TreeSet<>(byPublishOperationName); + /** * This annotation is used on class level * @@ -93,17 +100,17 @@ protected AsyncHeaders buildHeaders(Method method) { @Override public Map scan() { Set> components = componentClassScanner.scan(); - List> channels = mapToChannels(components); + Set> channels = mapToChannels(components); return ChannelMerger.merge(new ArrayList<>(channels)); } - private List> mapToChannels(Set> components) { + private Set> mapToChannels(Set> components) { return components.stream() .filter(this::isClassAnnotated) .map(this::mapClassToChannel) .filter(Optional::isPresent) .map(Optional::get) - .collect(Collectors.toList()); + .collect(Collectors.toCollection(channelItemSupplier)); } private boolean isClassAnnotated(Class component) { diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractMethodLevelListenerScanner.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractMethodLevelListenerScanner.java index 2fe477e4e..c1f054c9d 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractMethodLevelListenerScanner.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/scanners/channels/annotation/AbstractMethodLevelListenerScanner.java @@ -19,13 +19,16 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; +import java.util.function.Supplier; import java.util.stream.Collectors; import static java.util.stream.Collectors.toSet; @@ -38,19 +41,24 @@ public abstract class AbstractMethodLevelListenerScanner i private final SchemasService schemasService; + private static final Comparator> byPublishOperationName = + Comparator.comparing(it -> it.getValue().getPublish().getOperationId()); + private static final Supplier>> channelItemSupplier = + () -> new TreeSet<>(byPublishOperationName); + @Override public Map scan() { Set> components = componentClassScanner.scan(); - List> channels = mapToChannels(components); - return ChannelMerger.merge(channels); + Set> channels = mapToChannels(components); + return ChannelMerger.merge(new ArrayList<>(channels)); } - private List> mapToChannels(Set> components) { + private Set> mapToChannels(Set> components) { return components.stream() .map(this::getAnnotatedMethods) .flatMap(Collection::stream) .map(this::mapMethodToChannel) - .collect(Collectors.toList()); + .collect(Collectors.toCollection(channelItemSupplier)); } /**