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 f8d20c23e..586d88f7b 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; @@ -59,12 +57,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() {