diff --git a/implementation/src/main/java/io/smallrye/config/AbstractMappingConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/AbstractMappingConfigSourceInterceptor.java index 17568f402..923c059e7 100644 --- a/implementation/src/main/java/io/smallrye/config/AbstractMappingConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/AbstractMappingConfigSourceInterceptor.java @@ -1,9 +1,7 @@ package io.smallrye.config; -import java.util.HashSet; import java.util.Iterator; import java.util.Map; -import java.util.Set; import java.util.function.Function; public abstract class AbstractMappingConfigSourceInterceptor implements ConfigSourceInterceptor { @@ -26,17 +24,30 @@ public String apply(final String name) { @Override public Iterator iterateNames(final ConfigSourceInterceptorContext context) { - final Set names = new HashSet<>(); - final Iterator namesIterator = context.iterateNames(); - while (namesIterator.hasNext()) { - final String name = namesIterator.next(); - names.add(name); - final String mappedName = mapping.apply(name); - if (mappedName != null) { - names.add(mappedName); + return new Iterator<>() { + final Iterator iterator = context.iterateNames(); + String mappedName = null; + + @Override + public boolean hasNext() { + return mappedName != null || iterator.hasNext(); + } + + @Override + public String next() { + if (mappedName != null) { + String mappedName = this.mappedName; + this.mappedName = null; + return mappedName; + } + String name = iterator.next(); + String mappedName = mapping.apply(name); + if (!name.equals(mappedName)) { + this.mappedName = mappedName; + } + return name; } - } - return names.iterator(); + }; } protected Function getMapping() { diff --git a/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java index e0dc37449..e91a0f6f4 100644 --- a/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java @@ -7,10 +7,8 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; import jakarta.annotation.Priority; @@ -66,12 +64,19 @@ public ConfigValue getProfileValue(final ConfigSourceInterceptorContext context, @Override public Iterator iterateNames(final ConfigSourceInterceptorContext context) { - final Set names = new HashSet<>(); - final Iterator namesIterator = context.iterateNames(); - while (namesIterator.hasNext()) { - names.add(activeName(namesIterator.next(), profiles)); - } - return names.iterator(); + return new Iterator<>() { + final Iterator iterator = context.iterateNames(); + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public String next() { + return activeName(iterator.next(), profiles); + } + }; } public List getProfiles() { diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigSources.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigSources.java index 17deef2a2..5e545239a 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigSources.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigSources.java @@ -1,8 +1,9 @@ package io.smallrye.config; +import static java.util.Collections.emptyIterator; + import java.io.Serializable; import java.util.ArrayList; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -43,16 +44,36 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final @Override public Iterator iterateNames(final ConfigSourceInterceptorContext context) { - final Set names = new HashSet<>(); - for (final ConfigValueConfigSource configSource : configSources) { - final Set propertyNames = configSource.getPropertyNames(); - if (propertyNames != null) { - names.addAll(propertyNames); + return new Iterator<>() { + final Iterator configSourceIterator = configSources.iterator(); + Iterator propertiesIterator = context.iterateNames(); + + @Override + public boolean hasNext() { + if (propertiesIterator.hasNext()) { + return true; + } else { + propertiesIterator = nextConfigSource(); + if (propertiesIterator.hasNext()) { + return true; + } else if (configSourceIterator.hasNext()) { + return hasNext(); + } else { + return false; + } + } } - } - Iterator iter = context.iterateNames(); - iter.forEachRemaining(names::add); - return names.iterator(); + + @Override + public String next() { + return propertiesIterator.next(); + } + + private Iterator nextConfigSource() { + return configSourceIterator.hasNext() ? configSourceIterator.next().getPropertyNames().iterator() + : emptyIterator(); + } + }; } boolean negative() {