Repository: incubator-tamaya Updated Branches: refs/heads/master 43acbddb5 -> 4929d3cd2
TAMAYA-61: Added Interface for value combination. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b60af86e Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b60af86e Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b60af86e Branch: refs/heads/master Commit: b60af86e36d94abb4afd7b889f99a914d1bdcda5 Parents: 43acbdd Author: anatole <[email protected]> Authored: Sat Jan 24 11:09:52 2015 +0100 Committer: anatole <[email protected]> Committed: Sat Jan 24 21:06:13 2015 +0100 ---------------------------------------------------------------------- .../src/main/resources/checkstyle/style.xml | 2 +- .../resources/findbugs/findbugs-exclude.xml | 7 ++- .../spi/PropertyValueCombinationPolicy.java | 63 ++++++++++++++++++++ .../spi/PropertyValueCombinationPolicy.java | 59 ++++++++++++++++++ 4 files changed, 128 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b60af86e/buildconfigurations/src/main/resources/checkstyle/style.xml ---------------------------------------------------------------------- diff --git a/buildconfigurations/src/main/resources/checkstyle/style.xml b/buildconfigurations/src/main/resources/checkstyle/style.xml index b96b02c..5a43f94 100644 --- a/buildconfigurations/src/main/resources/checkstyle/style.xml +++ b/buildconfigurations/src/main/resources/checkstyle/style.xml @@ -41,7 +41,7 @@ under the License. <!-- Checks for Javadoc comments. --> <!-- See http://checkstyle.sf.net/config_javadoc.html --> <!-- module name="JavadocMethod"/ --> - <!-- module name="JavadocType"/ --> + <module name="JavadocType"/> <!-- module name="JavadocVariable"/ --> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b60af86e/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml ---------------------------------------------------------------------- diff --git a/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml b/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml index 50d545c..0ad63f2 100644 --- a/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml +++ b/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml @@ -79,13 +79,16 @@ under the License. </Match> + <!-- False positive returnin null for Boolean is required by the implemented interface. --> + <Match> + <Class name="org.apache.tamaya.core.internal.converters.BooleanConverter"/> + </Match> + <!-- Issues to review --> <Match> <Class name="org.apache.tamaya.core.internal.PropertiesFileLoader"/> </Match> - - <Match> <Class name="org.apache.tamaya.resolver.internal.URLResolver" /> </Match> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b60af86e/java7/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java ---------------------------------------------------------------------- diff --git a/java7/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java b/java7/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java new file mode 100644 index 0000000..dbda457 --- /dev/null +++ b/java7/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spi; + +/** + * Policy that determines how the final value of a configuration entry is evaluated. An instances of this + * interface can be registered to get control how multiple PropertySources are combined. This is useful in cases + * where the default overriding policy as implemented in {@link #DEFAULT_OVERRIDING_COLLECTOR} is not matching + * the need of the current application, e.g. then entries containing multiple values should be combined to new + * values instead of overridden. + */ +public interface PropertyValueCombinationPolicy { + + /** + * Default overriding collector, where each existing entry ({@code current} is overridden by a subsequent non-null + * entry evaluated by {@code propertySource.get(key)}. + */ + public static final PropertyValueCombinationPolicy DEFAULT_OVERRIDING_COLLECTOR = new PropertyValueCombinationPolicy(){ + + @Override + public String collect(String currentValue, String key, PropertySource propertySource) { + String value = propertySource.get(key); + return value!=null?value:currentValue; + } + + }; + + /** + * Method that is called for each value evaluated by a PropertySource for the given key. This method is called + * either when a single key is accessed, e.g. by calling {@code org.apache.tamaya.Configuration.getXXX}, but also + * when the full configuration property map is accessed by calling + * {@link org.apache.tamaya.Configuration#getProperties()}. + * + * @param currentValue the current value, including null. + * The collector should either combine the existing value with value from {@code currentValue} + * or replace the value in {@code currentValue} with {@code valueRead}, hereby returning the + * result to be used as new {@code currentValue}. + * @param key The current key to be evaluated. + * @param propertySource The PropertySource that may return an value for the given key. The PropertySource given + * may be evaluated for additional meta-data, how the given values are to be combined. + * Note that the value returned by a PropertySource can be null. In that case + * {@code currentValue} should be returned in almost all cases. + * @return the value to be used for future evaluation. + */ + String collect(String currentValue, String key, PropertySource propertySource); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b60af86e/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java new file mode 100644 index 0000000..660cc11 --- /dev/null +++ b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spi; + +import java.util.Optional; + +/** + * Policy that determines how the final value of a configuration entry is evaluated. An instances of this + * interface can be registered to get control how multiple PropertySources are combined. This is useful in cases + * where the default overriding policy as implemented in {@link #DEFAULT_OVERRIDING_COLLECTOR} is not matching + * the need of the current application, e.g. then entries containing multiple values should be combined to new + * values instead of overridden. + */ +@FunctionalInterface +public interface PropertyValueCombinationPolicy { + + /** + * Default overriding collector, where each existing entry ({@code current} is overridden by a subsequent non-null + * entry evaluated by {@code propertySource.get(key)}. + */ + public final PropertyValueCombinationPolicy DEFAULT_OVERRIDING_COLLECTOR = (current, key, propertySource) -> + Optional.ofNullable(propertySource.get(key)).orElse(current); + + /** + * Method that is called for each value evaluated by a PropertySource for the given key. This method is called + * either when a single key is accessed, e.g. by calling {@code org.apache.tamaya.Configuration.getXXX}, but also + * when the full configuration property map is accessed by calling + * {@link org.apache.tamaya.Configuration#getProperties()}. + * + * @param currentValue the current value, including null. + * The collector should either combine the existing value with value from {@code currentValue} + * or replace the value in {@code currentValue} with {@code valueRead}, hereby returning the + * result to be used as new {@code currentValue}. + * @param key The current key to be evaluated. + * @param propertySource The PropertySource that may return an value for the given key. The PropertySource given + * may be evaluated for additional meta-data, how the given values are to be combined. + * Note that the value returned by a PropertySource can be null. In that case + * {@code currentValue} should be returned in almost all cases. + * @return the value to be used for future evaluation. + */ + String collect(String currentValue, String key, PropertySource propertySource); + +}
