tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [1/2] incubator-tamaya git commit: TAMAYA-3: -Fixed tests, by implementing a mutable config instance. - Added some Javadocs.
Date Sun, 07 Dec 2014 23:54:37 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 67f27e1a2 -> 1c11c0f09


TAMAYA-3:
 -Fixed tests, by implementing a mutable config instance.
- Added some Javadocs.


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

Branch: refs/heads/master
Commit: 889bf37b81bbcd180a9ba480f1f59c40a61bcc04
Parents: 5c36d32
Author: anatole <anatole@apache.org>
Authored: Mon Dec 8 00:52:33 2014 +0100
Committer: anatole <anatole@apache.org>
Committed: Mon Dec 8 00:52:33 2014 +0100

----------------------------------------------------------------------
 .../java/org/apache/tamaya/ConfigFunctions.java | 113 -------------------
 .../tamaya/annotation/WithLoadPolicy.java       |   4 +
 .../tamaya/core/config/MappedConfiguration.java |  58 ++++++++++
 .../internal/MutableTestConfigProvider.java     |  64 +++++++++--
 .../apache/tamaya/ucs/UC1ReadProperties.java    |   1 +
 5 files changed, 118 insertions(+), 122 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/889bf37b/api/src/main/java/org/apache/tamaya/ConfigFunctions.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/tamaya/ConfigFunctions.java b/api/src/main/java/org/apache/tamaya/ConfigFunctions.java
deleted file mode 100644
index c732120..0000000
--- a/api/src/main/java/org/apache/tamaya/ConfigFunctions.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * 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;
-
-import java.util.*;
-import java.util.stream.Collectors;
-
-/**
- * Accessor that provides useful functions along with configuration.
- */
-public final class ConfigFunctions {
-    /**
-     * Private singleton constructor.
-     */
-    private ConfigFunctions() {
-    }
-
-    /**
-     * Creates a ConfigOperator that creates a Configuration containing only keys
-     * that are contained in the given area (non recursive). Hereby
-     * the area key is stripped away fromMap the resulting key.
-     *
-     * @param areaKey the area key, not null
-     * @return the area configuration, with the areaKey stripped away.
-     */
-    public static ConfigOperator selectArea(String areaKey) {
-        return selectArea(areaKey, true);
-    }
-
-    /**
-     * Creates a ConfigOperator that creates a Configuration containing only keys
-     * that are contained in the given area (non recursive).
-     *
-     * @param areaKey   the area key, not null
-     * @param stripKeys if set to true, the area key is stripped away fromMap the resulting
key.
-     * @return the area configuration, with the areaKey stripped away.
-     */
-    public static ConfigOperator selectArea(String areaKey, boolean stripKeys) {
-        return config -> {
-            Map<String, String> area = new HashMap<>();
-            area.putAll(
-                    config.toMap().entrySet().stream()
-                            .filter(e -> isKeyInArea(e.getKey(), areaKey))
-                            .collect(Collectors.toMap(
-                                    e -> stripKeys ? e.getKey().substring(areaKey.length()
+ 1) : e.getKey(),
-                                    e -> e.getValue())));
-            return PropertyProviderBuilder.create("area: " + areaKey).addMap(area).build().toConfiguration();
-        };
-    }
-
-    /**
-     * Calculates the current area key and compares it with the given key.
-     *
-     * @param key     the fully qualified entry key, not null
-     * @param areaKey the area key, not null
-     * @return true, if the entry is exact in this area
-     */
-    public static boolean isKeyInArea(String key, String areaKey) {
-        int lastIndex = key.lastIndexOf('.');
-        String curAreaKey = lastIndex > 0 ? key.substring(0, lastIndex) : "";
-        return curAreaKey.equals(areaKey);
-    }
-
-    /**
-     * Creates a ConfigOperator that creates a Configuration containing only keys
-     * that are contained in the given area (recursive). Hereby
-     * the area key is stripped away fromMap the resulting key.
-     *
-     * @param areaKey the area key, not null
-     * @return the area configuration, with the areaKey stripped away.
-     */
-    public static ConfigOperator selectAreaRecursive(String areaKey) {
-        return selectAreaRecursive(areaKey, true);
-    }
-
-    /**
-     * Creates a ConfigOperator that creates a Configuration containing only keys
-     * that are contained in the given area (recursive).
-     *
-     * @param areaKey   the area key, not null
-     * @param stripKeys if set to true, the area key is stripped away fromMap the resulting
key.
-     * @return the area configuration, with the areaKey stripped away.
-     */
-    public static ConfigOperator selectAreaRecursive(String areaKey, boolean stripKeys) {
-        return config -> {
-            Map<String, String> area = new HashMap<>();
-            String lookupKey = areaKey + '.';
-            area.putAll(
-                    config.toMap().entrySet().stream()
-                            .filter(e -> e.getKey().startsWith(lookupKey))
-                            .collect(Collectors.toMap(
-                                    e -> stripKeys ? e.getKey().substring(areaKey.length()
+ 1) : e.getKey(),
-                                    e -> e.getValue())));
-            return PropertyProviderBuilder.create("area (recursive): " + areaKey).addMap(area).build().toConfiguration();
-        };
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/889bf37b/api/src/main/java/org/apache/tamaya/annotation/WithLoadPolicy.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/tamaya/annotation/WithLoadPolicy.java b/api/src/main/java/org/apache/tamaya/annotation/WithLoadPolicy.java
index 572a36e..e469f5a 100644
--- a/api/src/main/java/org/apache/tamaya/annotation/WithLoadPolicy.java
+++ b/api/src/main/java/org/apache/tamaya/annotation/WithLoadPolicy.java
@@ -31,6 +31,10 @@ import java.lang.annotation.Target;
 @Target(value = { ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
 public @interface WithLoadPolicy {
 
+    /**
+     * The load policy to be used. If this annotation is present a load policy must be defined.
+     * @return The load policy to be used, not null.
+     */
     LoadPolicy value();
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/889bf37b/core/src/main/java/org/apache/tamaya/core/config/MappedConfiguration.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/config/MappedConfiguration.java b/core/src/main/java/org/apache/tamaya/core/config/MappedConfiguration.java
new file mode 100644
index 0000000..d61b99c
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/config/MappedConfiguration.java
@@ -0,0 +1,58 @@
+package org.apache.tamaya.core.config;
+
+import com.sun.javafx.scene.control.behavior.OptionalBoolean;
+import org.apache.tamaya.*;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+/**
+ * Created by Anatole on 07.12.2014.
+ */
+class MappedConfiguration extends AbstractConfiguration implements Configuration {
+
+    private UnaryOperator<String> keyMapper;
+    private Configuration config;
+
+    public MappedConfiguration(Configuration config, UnaryOperator<String> keyMapper)
{
+        super(MetaInfoBuilder.of(config.getMetaInfo()).setInfo("Mapped configuration, mapper="
+ keyMapper).build());
+        this.config = Objects.requireNonNull(config);
+        this.keyMapper = Objects.requireNonNull(keyMapper);
+    }
+
+    @Override
+    public Map<String, String> toMap() {
+        Map<String, String> result = new HashMap<>();
+        Map<String, String> map = this.config.toMap();
+        map.forEach((k,v) -> {
+            String targetKey = keyMapper.apply(k);
+            if(targetKey!=null){
+                result.put(targetKey, v);
+            }
+        });
+        return result;
+    }
+
+    @Override
+    public boolean isMutable() {
+        return this.config.isMutable();
+    }
+
+    @Override
+    public void apply(ConfigChangeSet change) {
+        this.config.apply(change);
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return this.config.isEmpty();
+    }
+
+    @Override
+    public Configuration toConfiguration() {
+        return this;
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/889bf37b/core/src/test/java/org/apache/tamaya/internal/MutableTestConfigProvider.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/tamaya/internal/MutableTestConfigProvider.java
b/core/src/test/java/org/apache/tamaya/internal/MutableTestConfigProvider.java
index d035e1d..34ab3a9 100644
--- a/core/src/test/java/org/apache/tamaya/internal/MutableTestConfigProvider.java
+++ b/core/src/test/java/org/apache/tamaya/internal/MutableTestConfigProvider.java
@@ -18,32 +18,37 @@
  */
 package org.apache.tamaya.internal;
 
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.MetaInfoBuilder;
-import org.apache.tamaya.PropertyProvider;
-import org.apache.tamaya.PropertyProviderBuilder;
+import org.apache.tamaya.*;
+import org.apache.tamaya.core.config.AbstractConfiguration;
 import org.apache.tamaya.core.spi.ConfigurationProviderSpi;
 
+import java.beans.PropertyChangeEvent;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
- * Created by Anatole on 29.09.2014.
+ * Simple test provider that creates a mutable instance of a configuration, just using a
simple map instance.
  */
 public class MutableTestConfigProvider implements ConfigurationProviderSpi{
-
+    /** The config name. */
     private static final String CONFIG_NAME = "mutableTestConfig";
-    private Configuration testConfig;
-    private final Map<String, String> dataMap = new ConcurrentHashMap<>();
+    /** The config provided. */
+    private MutableConfiguration testConfig;
 
+    /**
+     * COnsatructor.
+     */
     public MutableTestConfigProvider(){
+        Map<String, String> dataMap = new HashMap<>();
         dataMap.put("dad", "Anatole");
         dataMap.put("mom", "Sabine");
         dataMap.put("sons.1", "Robin");
         dataMap.put("sons.2", "Luke");
         dataMap.put("sons.3", "Benjamin");
         PropertyProvider provider = PropertyProviderBuilder.create(CONFIG_NAME).addMap(dataMap).build();
-        testConfig = provider.toConfiguration();
+        testConfig = new MutableConfiguration(dataMap, MetaInfo.of(CONFIG_NAME));
     }
 
     @Override
@@ -55,4 +60,45 @@ public class MutableTestConfigProvider implements ConfigurationProviderSpi{
     public Configuration getConfiguration(){
         return testConfig;
     }
+
+    /**
+     * Implements a simple mutable config based on a Mao instance.
+     */
+    private final class MutableConfiguration extends AbstractConfiguration{
+
+        private final Map<String,String> data = new ConcurrentHashMap<>();
+
+        MutableConfiguration(Map<String,String> data, MetaInfo metaInfo){
+            super(metaInfo);
+            this.data.putAll(data);
+        }
+
+        @Override
+        public Map<String, String> toMap() {
+            return Collections.unmodifiableMap(data);
+        }
+
+        @Override
+        public boolean isMutable() {
+            return true;
+        }
+
+        @Override
+        public void apply(ConfigChangeSet changeSet) {
+            for(PropertyChangeEvent change: changeSet.getEvents()){
+                if(change.getNewValue()==null){
+                    this.data.remove(change.getPropertyName());
+                }
+                else{
+                    this.data.put(change.getPropertyName(), (String) change.getNewValue());
+                }
+            }
+            publishPropertyChangeEvents(changeSet.getEvents());
+        }
+
+        @Override
+        public Configuration toConfiguration(){
+            return this;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/889bf37b/core/src/test/java/org/apache/tamaya/ucs/UC1ReadProperties.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/tamaya/ucs/UC1ReadProperties.java b/core/src/test/java/org/apache/tamaya/ucs/UC1ReadProperties.java
index 6c2ae19..61ef70d 100644
--- a/core/src/test/java/org/apache/tamaya/ucs/UC1ReadProperties.java
+++ b/core/src/test/java/org/apache/tamaya/ucs/UC1ReadProperties.java
@@ -19,6 +19,7 @@
 package org.apache.tamaya.ucs;
 
 import org.apache.tamaya.*;
+import org.apache.tamaya.core.config.ConfigFunctions;
 import org.junit.Test;
 
 import java.math.BigDecimal;


Mime
View raw message