Skip to content

Commit

Permalink
Cache the profile converter (#1282)
Browse files Browse the repository at this point in the history
The convertProfile() method is called several times and there's no need
to create the converter again and again.
  • Loading branch information
gsmet authored Jan 7, 2025
1 parent 9ab5fbb commit dbde487
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.IntFunction;

import jakarta.annotation.Priority;

import org.eclipse.microprofile.config.spi.Converter;

@Priority(Priorities.LIBRARY + 200)
public class ProfileConfigSourceInterceptor implements ConfigSourceInterceptor {
private static final long serialVersionUID = -6305289277993917313L;

private static final Converter<ArrayList<String>> PROFILES_CONVERTER = newCollectionConverter(
newTrimmingConverter(STRING_CONVERTER), new ArrayListFactory());

private final List<String> profiles;
private final List<String> prefixProfiles;

Expand Down Expand Up @@ -138,7 +144,15 @@ public static String activeName(final String name, final List<String> profiles)
}

public static List<String> convertProfile(final String profile) {
List<String> profiles = newCollectionConverter(newTrimmingConverter(STRING_CONVERTER), ArrayList::new).convert(profile);
List<String> profiles = PROFILES_CONVERTER.convert(profile);
return profiles != null ? profiles : Collections.emptyList();
}

private static class ArrayListFactory implements IntFunction<ArrayList<String>> {

@Override
public ArrayList<String> apply(int value) {
return new ArrayList<String>(value);
}
}
}

0 comments on commit dbde487

Please sign in to comment.