Repository: incubator-tamaya
Updated Branches:
  refs/heads/configjsr f0b875d6a -> c3dd1c96f


Added default access methods.

Signed-off-by: Anatole Tresch <anat...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/c3dd1c96
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/c3dd1c96
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/c3dd1c96

Branch: refs/heads/configjsr
Commit: c3dd1c96ff51dc6841fef7fd9a5f43998e93cb1c
Parents: f0b875d
Author: Anatole Tresch <anat...@apache.org>
Authored: Tue Feb 20 00:58:46 2018 +0100
Committer: Anatole Tresch <anat...@apache.org>
Committed: Tue Feb 20 00:58:46 2018 +0100

----------------------------------------------------------------------
 .../org/apache/tamaya/base/ConfigContext.java   | 49 +++++++++++++++++---
 .../org/apache/tamaya/base/DefaultConfig.java   |  4 +-
 .../tamaya/base/DefaultConfigBuilder.java       |  4 +-
 .../apache/tamaya/base/TamayaConfigBuilder.java |  9 ++--
 .../tamaya/base/DefaultConfigBuilderTest.java   | 19 ++++----
 5 files changed, 61 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c3dd1c96/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 eb6a16d..99322f6 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
@@ -18,15 +18,15 @@
  */
 package org.apache.tamaya.base;
 
+import org.apache.tamaya.base.convert.ConverterManager;
 import org.apache.tamaya.base.filter.Filter;
+import org.apache.tamaya.base.filter.FilterManager;
 
+import javax.config.Config;
 import javax.config.spi.ConfigSource;
 import javax.config.spi.Converter;
 import java.lang.reflect.Type;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
 
 /**
  * Central SPI for programmatically dealing with the setup of the 
configuration system.
@@ -36,6 +36,43 @@ import java.util.Optional;
 public interface ConfigContext {
 
     /**
+     * Get a context 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 instance, never null.
+     */
+    static ConfigContext from(Config config){
+        if(config instanceof ConfigContextSupplier){
+            return ((ConfigContextSupplier)config).getConfigContext();
+        }
+        return new ConfigContext() {
+            @Override
+            public Iterable<ConfigSource> getConfigSources() {
+                return Objects.requireNonNull(config.getConfigSources());
+            }
+
+            @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}";
+            }
+        };
+    }
+
+    /**
      * This method returns the current list of registered PropertySources 
ordered via their ordinal.
      * PropertySources with a lower ordinal come last. The PropertySource with 
the
      * highest ordinal comes first.
@@ -47,7 +84,7 @@ public interface ConfigContext {
      *
      * @return a sorted list of registered PropertySources.  The returned list 
need not be modifiable
      */
-    List<ConfigSource> getSources();
+    Iterable<ConfigSource> getConfigSources();
 
 //    /**
 //     * Access a {@link ConfigSource} using its (unique) name.
@@ -55,7 +92,7 @@ public interface ConfigContext {
 //     * @return the propoerty source found, or {@code null}.
 //     */
 //    default ConfigSource getSource(String name) {
-//        for(ConfigSource ps: getSources()){
+//        for(ConfigSource ps: getConfigSources()){
 //            if(name.equals(ps.getName())){
 //                return ps;
 //            }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c3dd1c96/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java
----------------------------------------------------------------------
diff --git a/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java 
b/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java
index 4a3e6c6..b1e5849 100644
--- a/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java
+++ b/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java
@@ -61,7 +61,7 @@ public class DefaultConfig implements Config, 
ConfigContextSupplier {
      * @param configContext The configuration Context to be used.
      */
     public DefaultConfig(ConfigContext configContext){
-        this.configSourceManager.addSources(configContext.getSources());
+        this.configSourceManager.addSources(configContext.getConfigSources());
         
this.configSourceManager.setConfigValueCombinationPolicy(configContext.getConfigValueCombinationPolicy());
         configContext.getConverters().forEach((t,c) -> 
this.converterManager.addConverter(t, Collection.class.cast(c)));
         this.filterManager.addFilter(configContext.getFilters());
@@ -133,7 +133,7 @@ public class DefaultConfig implements Config, 
ConfigContextSupplier {
     public ConfigContext getConfigContext() {
         return new ConfigContext() {
             @Override
-            public List<ConfigSource> getSources() {
+            public List<ConfigSource> getConfigSources() {
                 return configSourceManager.getSources();
             }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c3dd1c96/code/base/src/main/java/org/apache/tamaya/base/DefaultConfigBuilder.java
----------------------------------------------------------------------
diff --git 
a/code/base/src/main/java/org/apache/tamaya/base/DefaultConfigBuilder.java 
b/code/base/src/main/java/org/apache/tamaya/base/DefaultConfigBuilder.java
index 6422c88..2c0fadd 100644
--- a/code/base/src/main/java/org/apache/tamaya/base/DefaultConfigBuilder.java
+++ b/code/base/src/main/java/org/apache/tamaya/base/DefaultConfigBuilder.java
@@ -49,7 +49,7 @@ public class DefaultConfigBuilder implements 
TamayaConfigBuilder {
      * Creates a new builder instance.
      */
     public DefaultConfigBuilder(ConfigContext context) {
-        this.configSourceManager.addSources(context.getSources());
+        this.configSourceManager.addSources(context.getConfigSources());
         
this.configSourceManager.setConfigValueCombinationPolicy(context.getConfigValueCombinationPolicy());
         this.filterManager.addFilter(context.getFilters());
         context.getConverters().forEach((t,c) -> 
converterManager.addConverter(t, Collection.class.cast(c)));
@@ -287,7 +287,7 @@ public class DefaultConfigBuilder implements 
TamayaConfigBuilder {
     public ConfigContext getConfigContext() {
         return new ConfigContext() {
             @Override
-            public List<ConfigSource> getSources() {
+            public List<ConfigSource> getConfigSources() {
                 return configSourceManager.getSources();
             }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c3dd1c96/code/base/src/main/java/org/apache/tamaya/base/TamayaConfigBuilder.java
----------------------------------------------------------------------
diff --git 
a/code/base/src/main/java/org/apache/tamaya/base/TamayaConfigBuilder.java 
b/code/base/src/main/java/org/apache/tamaya/base/TamayaConfigBuilder.java
index 40ebe94..85a748a 100644
--- a/code/base/src/main/java/org/apache/tamaya/base/TamayaConfigBuilder.java
+++ b/code/base/src/main/java/org/apache/tamaya/base/TamayaConfigBuilder.java
@@ -21,6 +21,7 @@ package org.apache.tamaya.base;
 import org.apache.tamaya.base.filter.Filter;
 import org.apache.tamaya.base.ConfigValueCombinationPolicy;
 
+import javax.config.Config;
 import javax.config.spi.ConfigBuilder;
 import javax.config.spi.ConfigSource;
 import javax.config.spi.Converter;
@@ -48,17 +49,17 @@ public interface TamayaConfigBuilder extends ConfigBuilder, 
ConfigContextSupplie
         return new DefaultConfigBuilder();
     }
 
-    static TamayaConfigBuilder create(ConfigContextSupplier contextSupplier){
-        return new DefaultConfigBuilder(contextSupplier.getConfigContext());
+    static TamayaConfigBuilder create(Config config){
+        return new DefaultConfigBuilder(ConfigContext.from(config));
     }
 
     static TamayaConfigBuilder from(ConfigBuilder configBuilder){
         if(configBuilder instanceof TamayaConfigBuilder) {
             return (TamayaConfigBuilder) configBuilder;
         }else if(configBuilder instanceof ConfigContextSupplier){
-            return create((ConfigContextSupplier)configBuilder);
+            return new 
DefaultConfigBuilder((ConfigContextSupplier)configBuilder);
         }
-        throw new IllegalArgumentException("Builder must implement 
ConfigContextSupplier.");
+        return new 
DefaultConfigBuilder(ConfigContext.from(configBuilder.build()));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c3dd1c96/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java
----------------------------------------------------------------------
diff --git 
a/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java 
b/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java
index f575023..c279c59 100644
--- 
a/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java
+++ 
b/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java
@@ -18,7 +18,6 @@
  */
 package org.apache.tamaya.base;
 
-import org.apache.tamaya.base.ConfigValueCombinationPolicy;
 import org.apache.tamaya.base.filter.Filter;
 import org.junit.Test;
 
@@ -45,9 +44,9 @@ public class DefaultConfigBuilderTest {
         TamayaConfigBuilder b = new DefaultConfigBuilder()
                 .withSources(testPropertySource, testPS2);
         ConfigContext ctx = b.getConfigContext();
-        assertEquals(2, ctx.getSources().size());
-        assertTrue(ctx.getSources().contains(testPropertySource));
-        assertTrue(ctx.getSources().contains(testPS2));
+        assertEquals(2, ctx.getConfigSources().size());
+        assertTrue(ctx.getConfigSources().contains(testPropertySource));
+        assertTrue(ctx.getConfigSources().contains(testPS2));
     }
 
     @Test
@@ -56,16 +55,16 @@ public class DefaultConfigBuilderTest {
         TamayaConfigBuilder b = new DefaultConfigBuilder()
                 .withSources(testPropertySource, testPS2);
         ConfigContext ctx = b.getConfigContext();
-        assertEquals(2, ctx.getSources().size());
-        assertTrue(ctx.getSources().contains(testPropertySource));
-        assertTrue(ctx.getSources().contains(testPS2));
+        assertEquals(2, ctx.getConfigSources().size());
+        assertTrue(ctx.getConfigSources().contains(testPropertySource));
+        assertTrue(ctx.getConfigSources().contains(testPS2));
         b = new DefaultConfigBuilder()
                 .withSources(testPropertySource, testPS2);
         b.removeSources(testPropertySource);
         ctx = b.getConfigContext();
-        assertEquals(1, ctx.getSources().size());
-        assertFalse(ctx.getSources().contains(testPropertySource));
-        assertTrue(ctx.getSources().contains(testPS2));
+        assertEquals(1, ctx.getConfigSources().size());
+        assertFalse(ctx.getConfigSources().contains(testPropertySource));
+        assertTrue(ctx.getConfigSources().contains(testPS2));
     }
 
     @Test

Reply via email to