Repository: incubator-tamaya Updated Branches: refs/heads/configjsr 2bebc0e4e -> 9cc5fba7a
Refactored ConfigContext/Supplier handling. Signed-off-by: Anatole Tresch <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/9cc5fba7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/9cc5fba7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/9cc5fba7 Branch: refs/heads/configjsr Commit: 9cc5fba7a147df6da68065c1d96ac5989009577f Parents: 2bebc0e Author: Anatole Tresch <[email protected]> Authored: Tue Jul 10 15:52:49 2018 +0200 Committer: Anatole Tresch <[email protected]> Committed: Tue Jul 10 15:52:49 2018 +0200 ---------------------------------------------------------------------- .../org/apache/tamaya/base/ConfigContext.java | 29 +----------- .../tamaya/base/ConfigContextSupplier.java | 50 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9cc5fba7/code/base/src/main/java/org/apache/tamaya/base/ConfigContext.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/ConfigContext.java b/code/base/src/main/java/org/apache/tamaya/base/ConfigContext.java index cfd8007..41e6f0e 100644 --- a/code/base/src/main/java/org/apache/tamaya/base/ConfigContext.java +++ b/code/base/src/main/java/org/apache/tamaya/base/ConfigContext.java @@ -46,34 +46,7 @@ public interface ConfigContext { * @return a context instance, never null. */ static ConfigContext from(Config config){ - if(config instanceof ConfigContextSupplier){ - return ((ConfigContextSupplier)config).getConfigContext(); - } - return new ConfigContext() { - @Override - public List<ConfigSource> getConfigSources() { - List<ConfigSource> configSources = new ArrayList<>(); - for(ConfigSource cs:config.getConfigSources()){ - configSources.add(cs); - } - return configSources; - } - - @Override - public List<Filter> getFilters() { - return Collections.emptyList(); - } - - @Override - public Map<Type, List<Converter>> getConverters() { - return ConverterManager.defaultInstance().getConverters(); - } - - @Override - public String toString() { - return "ConfigContext#default{\n delegate:"+config+"\n}"; - } - }; + return ConfigContextSupplier.of(config).getConfigContext(); } /** http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9cc5fba7/code/base/src/main/java/org/apache/tamaya/base/ConfigContextSupplier.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/ConfigContextSupplier.java b/code/base/src/main/java/org/apache/tamaya/base/ConfigContextSupplier.java index 9e3be69..1466f3e 100644 --- a/code/base/src/main/java/org/apache/tamaya/base/ConfigContextSupplier.java +++ b/code/base/src/main/java/org/apache/tamaya/base/ConfigContextSupplier.java @@ -18,8 +18,17 @@ */ package org.apache.tamaya.base; +import org.apache.tamaya.base.convert.ConverterManager; +import org.apache.tamaya.base.filter.Filter; + +import javax.config.Config; import javax.config.spi.ConfigSource; import javax.config.spi.Converter; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; /** * Central SPI for programmatically dealing with the setup of the configuration system. @@ -30,6 +39,47 @@ import javax.config.spi.Converter; public interface ConfigContextSupplier { /** + * Get a context supplier from the given {@link Config}. If the {@link Config} implements + * {@link ConfigContextSupplier} it is cast and the result will be returned. If the + * config does not implement {@link ConfigContextSupplier}, a default context is created, + * which includes all convereters as defined by {@link ConverterManager#defaultInstance()#getConverters()}, + * an empty filter list and the {@link ConfigSource}s as declared by the given {@link Config} + * instance. + * @param config the config instance, not null. + * @return a context supplier instance, never null. + */ + static ConfigContextSupplier of(Config config){ + if(config instanceof ConfigContextSupplier){ + return (ConfigContextSupplier)config; + } + return () -> new ConfigContext() { + @Override + public List<ConfigSource> getConfigSources() { + List<ConfigSource> configSources = new ArrayList<>(); + for(ConfigSource cs:config.getConfigSources()){ + configSources.add(cs); + } + return configSources; + } + + @Override + public List<Filter> getFilters() { + return Collections.emptyList(); + } + + @Override + public Map<Type, List<Converter>> getConverters() { + return ConverterManager.defaultInstance().getConverters(); + } + + @Override + public String toString() { + return "ConfigContext#default{\n delegate:"+config+"\n}"; + } + }; + } + + /** * Make an instance of a configuration accessible for use with Apache Tamaya specific extensions. * In most cases it should be sufficient to implement this interfance on your implementation of * {@link javax.config.Config}.
