From 63f889baec16c499ff7a02cc4d3db5d82145e250 Mon Sep 17 00:00:00 2001 From: Jeff Trent Date: Mon, 27 Mar 2023 22:23:26 -0400 Subject: [PATCH] remove optional args from ConfigBeanRegistry --- .../services/ConfigBeanRegistry.java | 35 +++++++---- .../services/DefaultConfigResolver.java | 4 +- .../DefaultPicoConfigBeanRegistry.java | 62 +++++++++++-------- .../configuredby/test/ConfiguredByTest.java | 3 +- 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/ConfigBeanRegistry.java b/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/ConfigBeanRegistry.java index 4ae5814faf8..e916941c53f 100644 --- a/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/ConfigBeanRegistry.java +++ b/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/ConfigBeanRegistry.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import io.helidon.builder.config.spi.ConfigBeanInfo; @@ -56,26 +55,37 @@ public interface ConfigBeanRegistry extends HelidonConfigBeanRegistry { List> configuredServiceProviders(); /** - * These are the managed/slave service providers that are associated with config bean instances with the config key provided. + * These are the managed/slave service providers that are associated with config bean instances with the config {@code key} + * provided. * - * @param key the config options key - note that this is a partial key - and not relative to the parent - the same - * key used by {@link io.helidon.builder.config.ConfigBean#value()}. + * @param key the config options key - note that this is a partial key - and not relative to the parent - the same + * key used by {@link io.helidon.builder.config.ConfigBean#value()}. * @return the list of configured services */ List> configuredServiceProvidersConfiguredBy(String key); /** - * Returns all the known config beans in order of rank given the short config key / alias. Callers should understand - * that this list might be incomplete until ready state is reached (see {@link #ready()}). + * Returns all the known config beans in order of rank given the {@code key}. Callers should understand + * that this list might be incomplete until ready state is reached (see {@link #ready()}). Note also that callers should + * attempt to use {@link #configBeansByConfigKey(String)} whenever possible since it will generate more precise matches. * * @param key the config options key - note that this is a partial key - and not relative to the parent - the same * key used by {@link io.helidon.builder.config.ConfigBean#value()}. - * @param fullConfigKey optionally, the full config key - if not passed will return the list of all matches - * using just the key * @return the set of known config keys */ + Set configBeansByConfigKey(String key); + + /** + * Returns all the known config beans in order of rank matching the {@code key} and {@code fullConfigKey}. Callers should + * understand that this list might be incomplete until ready state is reached (see {@link #ready()}). + * + * @param key the config options key - note that this is a partial key - and not relative to the parent - the same + * key used by {@link io.helidon.builder.config.ConfigBean#value()}. + * @param fullConfigKey the full config key + * @return the set of known config keys matching the provided criteria + */ Set configBeansByConfigKey(String key, - Optional fullConfigKey); + String fullConfigKey); /** * Similar to {@link #configBeansByConfigKey}, but instead returns all the known config beans in a @@ -83,11 +93,10 @@ Set configBeansByConfigKey(String key, * * @param key the config options key - note that this is a partial key - and not relative to the parent - the same * key used by {@link io.helidon.builder.config.ConfigBean#value()}. - * @param fullConfigKey optionally, the full config key - if not passed will return the list of all matches - * using just the key - * @return the map of known config keys + * @param fullConfigKey the full config key + * @return the map of known config keys to config beans */ Map configBeanMapByConfigKey(String key, - Optional fullConfigKey); + String fullConfigKey); } diff --git a/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultConfigResolver.java b/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultConfigResolver.java index ced3b7604ce..96c4e852cab 100644 --- a/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultConfigResolver.java +++ b/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultConfigResolver.java @@ -120,7 +120,7 @@ static List findInConfigBeanRegistryAsList(ResolutionContext ctx, DefaultPicoConfigBeanRegistry cbr = (DefaultPicoConfigBeanRegistry) ConfigBeanRegistryHolder.configBeanRegistry() .orElseThrow(); String fullConfigKey = fullConfigKeyOf(safeDowncastOf(ctx.config()), request.configKey(), meta); - Set result = cbr.configBeansByConfigKey(request.configKey(), Optional.of(fullConfigKey)); + Set result = cbr.configBeansByConfigKey(request.configKey(), fullConfigKey); return new ArrayList<>(result); } @@ -135,7 +135,7 @@ static Map findInConfigBeanRegistryAsMap(ResolutionContext ctx, DefaultPicoConfigBeanRegistry cbr = (DefaultPicoConfigBeanRegistry) ConfigBeanRegistryHolder.configBeanRegistry() .orElseThrow(); String fullConfigKey = fullConfigKeyOf(safeDowncastOf(ctx.config()), request.configKey(), meta); - Map result = cbr.configBeanMapByConfigKey(request.configKey(), Optional.of(fullConfigKey)); + Map result = cbr.configBeanMapByConfigKey(request.configKey(), fullConfigKey); return Objects.requireNonNull((Map) result); } diff --git a/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultPicoConfigBeanRegistry.java b/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultPicoConfigBeanRegistry.java index af679237c45..2ee40aa703e 100644 --- a/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultPicoConfigBeanRegistry.java +++ b/pico/configdriven/services/src/main/java/io/helidon/pico/configdriven/services/DefaultPicoConfigBeanRegistry.java @@ -253,36 +253,19 @@ public boolean ready() { } @Override - public Set configBeansByConfigKey(String key, - Optional optFullConfigKey) { - List> cspsUsingSameKey = - configuredServiceProvidersByConfigKey.get(Objects.requireNonNull(key)); - if (cspsUsingSameKey == null) { - return Set.of(); - } + public Set configBeansByConfigKey(String key) { + return configBeansByConfigKey(key, Optional.empty()); + } - Set result = new LinkedHashSet<>(); - cspsUsingSameKey.stream() - .filter(csp -> csp instanceof AbstractConfiguredServiceProvider) - .map(AbstractConfiguredServiceProvider.class::cast) - .forEach(csp -> { - Map configBeans = csp.configBeanMap(); - if (optFullConfigKey.isEmpty()) { - result.addAll(configBeans.values()); - } else { - configBeans.forEach((k, v) -> { - if (optFullConfigKey.get().equals(k)) { - result.add(v); - } - }); - } - }); - return result; + @Override + public Set configBeansByConfigKey(String key, + String fullConfigKey) { + return configBeansByConfigKey(key, Optional.of(fullConfigKey)); } @Override public Map configBeanMapByConfigKey(String key, - Optional optFullConfigKey) { + String fullConfigKey) { List> cspsUsingSameKey = configuredServiceProvidersByConfigKey.get(Objects.requireNonNull(key)); if (cspsUsingSameKey == null) { @@ -296,7 +279,7 @@ public Set configBeansByConfigKey(String key, .forEach(csp -> { Map configBeans = csp.configBeanMap(); configBeans.forEach((k, v) -> { - if (optFullConfigKey.isEmpty() || optFullConfigKey.get().equals(k)) { + if (fullConfigKey.isEmpty() || fullConfigKey.equals(k)) { Object prev = result.put(k, v); if (prev != null && prev != v) { throw new IllegalStateException("had two entries with the same key: " + prev + " and " + v); @@ -526,6 +509,33 @@ void registerConfigBean(Object configBean, csp.registerConfigBean(instanceId, configBean); } + private Set configBeansByConfigKey(String key, + Optional optFullConfigKey) { + List> cspsUsingSameKey = + configuredServiceProvidersByConfigKey.get(Objects.requireNonNull(key)); + if (cspsUsingSameKey == null) { + return Set.of(); + } + + Set result = new LinkedHashSet<>(); + cspsUsingSameKey.stream() + .filter(csp -> csp instanceof AbstractConfiguredServiceProvider) + .map(AbstractConfiguredServiceProvider.class::cast) + .forEach(csp -> { + Map configBeans = csp.configBeanMap(); + if (optFullConfigKey.isEmpty()) { + result.addAll(configBeans.values()); + } else { + configBeans.forEach((k, v) -> { + if (optFullConfigKey.get().equals(k)) { + result.add(v); + } + }); + } + }); + return result; + } + private void initialize(Config commonCfg) { if (configuredServiceProvidersByConfigKey.isEmpty()) { LOGGER.log(System.Logger.Level.DEBUG, "No config driven services found"); diff --git a/pico/configdriven/tests/configuredby/src/test/java/io/helidon/pico/configdriven/configuredby/test/ConfiguredByTest.java b/pico/configdriven/tests/configuredby/src/test/java/io/helidon/pico/configdriven/configuredby/test/ConfiguredByTest.java index 094a37b346a..991ee56e319 100644 --- a/pico/configdriven/tests/configuredby/src/test/java/io/helidon/pico/configdriven/configuredby/test/ConfiguredByTest.java +++ b/pico/configdriven/tests/configuredby/src/test/java/io/helidon/pico/configdriven/configuredby/test/ConfiguredByTest.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -100,7 +99,7 @@ void serverConfigWithOneSocketConfigNested() { "fake-server" )); - Set configBeans = cbr.configBeansByConfigKey("fake-server", Optional.empty()); + Set configBeans = cbr.configBeansByConfigKey("fake-server"); assertThat(configBeans.toString(), configBeans.size(), is(1));