Skip to content

Commit

Permalink
feat(core): use stable merging strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Nov 13, 2023
1 parent e4545cd commit 3034657
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +44,11 @@ public abstract class AbstractClassLevelListenerScanner<

private final SchemasService schemasService;

private static final Comparator<Map.Entry<String, ChannelItem>> byPublishOperationName =
Comparator.comparing(it -> it.getValue().getPublish().getOperationId());
private static final Supplier<Set<Map.Entry<String, ChannelItem>>> channelItemSupplier =
() -> new TreeSet<>(byPublishOperationName);

/**
* This annotation is used on class level
*
Expand Down Expand Up @@ -93,17 +100,17 @@ protected AsyncHeaders buildHeaders(Method method) {
@Override
public Map<String, ChannelItem> scan() {
Set<Class<?>> components = componentClassScanner.scan();
List<Map.Entry<String, ChannelItem>> channels = mapToChannels(components);
Set<Map.Entry<String, ChannelItem>> channels = mapToChannels(components);
return ChannelMerger.merge(new ArrayList<>(channels));
}

private List<Map.Entry<String, ChannelItem>> mapToChannels(Set<Class<?>> components) {
private Set<Map.Entry<String, ChannelItem>> mapToChannels(Set<Class<?>> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,19 +41,24 @@ public abstract class AbstractMethodLevelListenerScanner<T extends Annotation> i

private final SchemasService schemasService;

private static final Comparator<Map.Entry<String, ChannelItem>> byPublishOperationName =
Comparator.comparing(it -> it.getValue().getPublish().getOperationId());
private static final Supplier<Set<Map.Entry<String, ChannelItem>>> channelItemSupplier =
() -> new TreeSet<>(byPublishOperationName);

@Override
public Map<String, ChannelItem> scan() {
Set<Class<?>> components = componentClassScanner.scan();
List<Map.Entry<String, ChannelItem>> channels = mapToChannels(components);
return ChannelMerger.merge(channels);
Set<Map.Entry<String, ChannelItem>> channels = mapToChannels(components);
return ChannelMerger.merge(new ArrayList<>(channels));
}

private List<Map.Entry<String, ChannelItem>> mapToChannels(Set<Class<?>> components) {
private Set<Map.Entry<String, ChannelItem>> mapToChannels(Set<Class<?>> components) {
return components.stream()
.map(this::getAnnotatedMethods)
.flatMap(Collection::stream)
.map(this::mapMethodToChannel)
.collect(Collectors.toList());
.collect(Collectors.toCollection(channelItemSupplier));
}

/**
Expand Down

0 comments on commit 3034657

Please sign in to comment.