This is an automated email from the ASF dual-hosted git repository. anatole pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-tamaya-sandbox.git
commit 5c0cd943626f772ad8a1ba22c96fa38d62b59f6a Author: Anatole Tresch <atsti...@gmail.com> AuthorDate: Sun Mar 3 23:07:32 2019 +0100 Adapted to latest API changes. --- configjsr/pom.xml | 2 +- .../apache/tamaya/jsr382/JavaConfigAdapter.java | 5 +- .../apache/tamaya/jsr382/TamayaConfigAccessor.java | 193 ++++++++++++++------- .../tamaya/jsr382/JavaConfigAdapterTest.java | 2 +- .../org/apache/tamaya/jsr382/SmokeExamples.java | 19 +- 5 files changed, 143 insertions(+), 78 deletions(-) diff --git a/configjsr/pom.xml b/configjsr/pom.xml index 83b55b3..541e8f7 100644 --- a/configjsr/pom.xml +++ b/configjsr/pom.xml @@ -87,7 +87,7 @@ under the License. <dependency> <groupId>com.github.eclipse</groupId> <artifactId>ConfigJSR</artifactId> - <version>bdab2f22923ba83e93a841f61e177c95796cfb68</version> + <version>f739e6d322dbf4eab62d5ffb121c5b0a9e63c2d4</version> </dependency> <dependency> <groupId>org.mockito</groupId> diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java index cf337a9..411fca0 100644 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java @@ -20,6 +20,7 @@ package org.apache.tamaya.jsr382; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; import javax.config.Config; import javax.config.ConfigAccessor; @@ -79,8 +80,8 @@ public class JavaConfigAdapter implements Config, Serializable { } @Override - public ConfigAccessor<String> access(String name) { - return new TamayaConfigAccessor(this, name); + public <T> ConfigAccessor.Builder<T> access(String name, Class<T> type) { + return new TamayaConfigAccessor.TamayaConfigAccessorBuilder<>(this, name, TypeLiteral.of(type)); } @Override diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java index d80e7ae..616c04e 100644 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java @@ -23,6 +23,7 @@ import org.apache.tamaya.TypeLiteral; import javax.config.ConfigAccessor; import javax.config.ConfigSnapshot; import javax.config.spi.Converter; +import java.time.Duration; import java.util.*; import java.util.concurrent.TimeUnit; @@ -46,28 +47,20 @@ class TamayaConfigAccessor<T> implements ConfigAccessor<T> { private long timeout = 0; private T cachedValue; - /** * Constructor. * - * @param javaConfigAdapter - * @param name + * @param builder the builder, not null. */ - TamayaConfigAccessor(JavaConfigAdapter javaConfigAdapter, String name) { - this.javaConfigAdapter = Objects.requireNonNull(javaConfigAdapter); - this.name = Objects.requireNonNull(name); - this.targetType = TypeLiteral.of(String.class); - } - - private TamayaConfigAccessor(TamayaConfigAccessor<?> accessor, TypeLiteral<T> target) { - this.name = accessor.name; - this.targetType = target; - this.stringDefaultValue = accessor.stringDefaultValue; - this.javaConfigAdapter = javaConfigAdapter; - this.evaluateVariables = accessor.evaluateVariables; - this.lookupSuffixes.addAll(accessor.lookupSuffixes); + TamayaConfigAccessor(TamayaConfigAccessorBuilder<T> builder) { + this.name = builder.name; + this.targetType = builder.targetType; + this.stringDefaultValue = builder.stringDefaultValue; + this.javaConfigAdapter = builder.configAdapter; + this.evaluateVariables = builder.evaluateVariables; + this.lookupSuffixes.addAll(builder.lookupSuffixes); // What to do if the fallback accessors do not match, e.g. with collection types... - this.fallbackAccessors.addAll(accessor.fallbackAccessors); + this.fallbackAccessors.addAll(builder.fallbackAccessors); } /** @@ -103,66 +96,134 @@ class TamayaConfigAccessor<T> implements ConfigAccessor<T> { return result; } - @Override - public <N> ConfigAccessor<N> as(Class<N> aClass) { - return new TamayaConfigAccessor<N>(this, TypeLiteral.of(aClass)); - } + static final class TamayaConfigAccessorBuilder<T> implements ConfigAccessor.Builder<T>{ - @Override - public ConfigAccessor<List<T>> asList() { - TypeLiteral<List<T>> target = new TypeLiteral<List<T>>(); - return new TamayaConfigAccessor<List<T>>(this, target); - } + private String name; + private JavaConfigAdapter configAdapter; + private Converter<T> customConverter; + private TypeLiteral<T> targetType; + private T typedDefaultValue; + private String stringDefaultValue; + private List<ConfigAccessor<String>> fallbackAccessors = new ArrayList<>(); + private List<String> lookupSuffixes = new ArrayList<>(); + private boolean evaluateVariables = true; + private long timeout = 0; + private T cachedValue; + private Duration cacheDuration; - @Override - public ConfigAccessor<Set<T>> asSet() { - TypeLiteral<Set<T>> target = new TypeLiteral<Set<T>>(); - return new TamayaConfigAccessor<Set<T>>(this, target); - } + /** + * Constructor. + * + * @param configAdapter the config, not null. + */ + TamayaConfigAccessorBuilder(JavaConfigAdapter configAdapter, String name, TypeLiteral<T> type) { + this.configAdapter = configAdapter; + this.name = name; + this.targetType = targetType; + } - @Override - public ConfigAccessor<T> useConverter(Converter<T> converter) { - this.customConverter = converter; - return this; - } + @Override + public Builder<T> useConverter(Converter<T> converter) { + this.customConverter = Objects.requireNonNull(converter); + return this; + } - @Override - public ConfigAccessor<T> withDefault(T defaultValue) { - this.typedDefaultValue = defaultValue; - return this; - } + @Override + public Builder<T> withDefault(T value) { + this.typedDefaultValue = Objects.requireNonNull(value); + return this; + } - @Override - public ConfigAccessor<T> withStringDefault(String defaultValue) { - this.stringDefaultValue = defaultValue; - return this; - } + @Override + public Builder<T> withStringDefault(String value) { + this.stringDefaultValue = value; + return this; + } - @Override - public ConfigAccessor<T> cacheFor(long duration, TimeUnit timeUnit) { - this.cachedValue = getValue(); - this.timeout = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(duration, timeUnit); - return this; - } + @Override + public Builder<T> cacheFor(Duration duration) { + this.cacheDuration = Objects.requireNonNull(duration); + return this; + } - @Override - public ConfigAccessor<T> evaluateVariables(boolean evaluateVariables) { - this.evaluateVariables = evaluateVariables; - return this; - } + @Override + public Builder<T> evaluateVariables(boolean evaluateVariables) { + this.evaluateVariables = evaluateVariables; + return this; + } - @Override - public ConfigAccessor<T> addLookupSuffix(String lookupSuffix) { - this.lookupSuffixes.add(lookupSuffix); - return this; - } + @Override + public Builder<T> addLookupSuffix(String suffixValue) { + this.lookupSuffixes.add(Objects.requireNonNull(suffixValue)); + return this; + } - @Override - public ConfigAccessor<T> addLookupSuffix(ConfigAccessor<String> configAccessor) { - this.fallbackAccessors.add(configAccessor); - return this; + @Override + public ConfigAccessor<T> build() { + return new TamayaConfigAccessor<T>(this); + } } +// @Override +// public <N> ConfigAccessor<N> as(Class<N> aClass) { +// return new TamayaConfigAccessor<N>(this, TypeLiteral.of(aClass)); +// } +// +// @Override +// public ConfigAccessor<List<T>> asList() { +// TypeLiteral<List<T>> target = new TypeLiteral<List<T>>(); +// return new TamayaConfigAccessor<List<T>>(this, target); +// } +// +// @Override +// public ConfigAccessor<Set<T>> asSet() { +// TypeLiteral<Set<T>> target = new TypeLiteral<Set<T>>(); +// return new TamayaConfigAccessor<Set<T>>(this, target); +// } +// +// @Override +// public ConfigAccessor<T> useConverter(Converter<T> converter) { +// this.customConverter = converter; +// return this; +// } +// +// @Override +// public ConfigAccessor<T> withDefault(T defaultValue) { +// this.typedDefaultValue = defaultValue; +// return this; +// } +// +// @Override +// public ConfigAccessor<T> withStringDefault(String defaultValue) { +// this.stringDefaultValue = defaultValue; +// return this; +// } +// +// @Override +// public ConfigAccessor<T> cacheFor(long duration, TimeUnit timeUnit) { +// this.cachedValue = getValue(); +// this.timeout = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(duration, timeUnit); +// return this; +// } +// +// @Override +// public ConfigAccessor<T> evaluateVariables(boolean evaluateVariables) { +// this.evaluateVariables = evaluateVariables; +// return this; +// } +// +// @Override +// public ConfigAccessor<T> addLookupSuffix(String lookupSuffix) { +// this.lookupSuffixes.add(lookupSuffix); +// return this; +// } +// +// @Override +// public ConfigAccessor<T> addLookupSuffix(ConfigAccessor<String> configAccessor) { +// this.fallbackAccessors.add(configAccessor); +// return this; +// } + @Override public T getValue() { if (this.timeout > System.currentTimeMillis()) { diff --git a/configjsr/src/test/java/org/apache/tamaya/jsr382/JavaConfigAdapterTest.java b/configjsr/src/test/java/org/apache/tamaya/jsr382/JavaConfigAdapterTest.java index 062bafa..15551cb 100644 --- a/configjsr/src/test/java/org/apache/tamaya/jsr382/JavaConfigAdapterTest.java +++ b/configjsr/src/test/java/org/apache/tamaya/jsr382/JavaConfigAdapterTest.java @@ -192,7 +192,7 @@ public class JavaConfigAdapterTest { } @Override - public ConfigAccessor<String> access(String propertyName) { + public <T> ConfigAccessor.Builder<T> access(String propertyName, Class<T> type) { throw new RuntimeException("Not implemented yet!"); } diff --git a/configjsr/src/test/java/org/apache/tamaya/jsr382/SmokeExamples.java b/configjsr/src/test/java/org/apache/tamaya/jsr382/SmokeExamples.java index 86a0539..2c81f5a 100644 --- a/configjsr/src/test/java/org/apache/tamaya/jsr382/SmokeExamples.java +++ b/configjsr/src/test/java/org/apache/tamaya/jsr382/SmokeExamples.java @@ -116,10 +116,10 @@ public class SmokeExamples { Config config = ConfigProvider.getConfig(); - ConfigAccessor<String> accessor = config.access("foo.bar.property"); + ConfigAccessor.Builder<String> accessor = config.access("foo.bar.property", String.class); accessor = accessor.addLookupSuffix("DEV").addLookupSuffix("server01"); accessor = accessor.withDefault("anyDefault"); - String textValue = accessor.getValue(); + String textValue = accessor.build().getValue(); } public void apacheTamaya_multiKeyLookup(){ @@ -139,11 +139,13 @@ public class SmokeExamples { Config config = ConfigProvider.getConfig(); - ConfigAccessor<String> accessor = config.access("foo.bar.property"); - accessor = accessor.addLookupSuffix("DEV"); - accessor = accessor.withDefault("anyDefault"); - ConfigAccessor<Integer> accessor2 = config.access("foo.bar.property2").as(Integer.class); - accessor = accessor.withDefault("1234"); + ConfigAccessor.Builder<String> builder = config.access("foo.bar.property", String.class); + builder = builder.addLookupSuffix("DEV"); + builder = builder.withDefault("anyDefault"); + ConfigAccessor.Builder<Integer> builder2 = config.access("foo.bar.property2", Integer.class); + builder2 = builder2.withStringDefault("1234"); + ConfigAccessor<String> accessor = builder.build(); + ConfigAccessor<Integer> accessor2 = builder2.build(); ConfigSnapshot snapshot = config.snapshotFor(accessor, accessor2); String property1 = accessor.getValue(snapshot); @@ -212,7 +214,8 @@ public class SmokeExamples { String myKey = "Test ${java.version},${java.foo},${PATH}"; Config config = ConfigProvider.getConfig(); - String resolvedValue = config.access("myKey").evaluateVariables(true).getValue(); + String resolvedValue = config.access("myKey", String.class).evaluateVariables(true). + build().getValue(); } public void resolution_tamaya(){