class MetadataSelectorMatcher {

    public Set<String> filterNamespaces(@Nullable String namespaceSelector, @Nullable Locale locale) {
        if (namespaceSelector == null || namespaceSelector.isEmpty()) {
            return Collections.emptySet();
        } else {
            Set<String> originalNamespaces = Arrays.stream(namespaceSelector.split(",")) //
                    .filter(n -> !metadataRegistry.isInternalNamespace(n)) //
                    .map(n -> n.trim()) //
                    .collect(Collectors.toSet());

            String namespacePattern = originalNamespaces.stream().collect(Collectors.joining("|"));

            Pattern pattern = Pattern.compile(METADATA_SCHEME_PREFIX + "(" + namespacePattern + ")$");
            Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions(locale);

            Set<String> configNamespaces = configDescriptions.stream()
                    .filter(cd -> METADATA_SCHEME.equals(cd.getUID().getScheme())).map(cd -> cd.getUID().toString())
                    .filter(pattern.asPredicate()).map(uri -> uri.substring(METADATA_SCHEME_PREFIX.length()))
                    .collect(toSet());

            // merge configDescription namespaces and namespaces from the namespace selector:
            Set<String> result = new HashSet<>(originalNamespaces);
            result.addAll(configNamespaces);

            // filter all name spaces which do not match the UID segment pattern (this will be the regex tokens):
            return result.stream().filter(namespace -> namespace.matches(AbstractUID.SEGMENT_PATTERN)).collect(toSet());
        }
    }

    @Activate public  MetadataSelectorMatcher(final @Reference MetadataRegistry metadataRegistry,
            final @Reference ConfigDescriptionRegistry configDescriptionRegistry);

}