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: Moved ConfigFunctions to core.
Date Thu, 11 Dec 2014 17:08:41 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master ab1ac6bf3 -> 5d1a1fa13


TAMAYA-3: Moved ConfigFunctions to core.


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

Branch: refs/heads/master
Commit: c0bea43c63a1122fd1164cfecc2a198e6cf2d074
Parents: 1a3e512
Author: anatole <anatole@apache.org>
Authored: Thu Dec 11 18:08:27 2014 +0100
Committer: anatole <anatole@apache.org>
Committed: Thu Dec 11 18:08:27 2014 +0100

----------------------------------------------------------------------
 .../tamaya/core/config/ConfigFunctions.java     | 143 +++++++++++++++++++
 1 file changed, 143 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c0bea43c/core/src/main/java/org/apache/tamaya/core/config/ConfigFunctions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/config/ConfigFunctions.java b/core/src/main/java/org/apache/tamaya/core/config/ConfigFunctions.java
new file mode 100644
index 0000000..473cd40
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/config/ConfigFunctions.java
@@ -0,0 +1,143 @@
+/*
+ * 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.core.config;
+
+import org.apache.tamaya.ConfigOperator;
+import org.apache.tamaya.PropertyProviderBuilder;
+
+import java.util.*;
+import java.util.function.UnaryOperator;
+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();
+        };
+    }
+
+    /**
+     * 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 mapArea(String areaKey, String mappedAreaKey) {
+        return mapKeys(key -> key.startsWith(areaKey + '.')?
+                mappedAreaKey + key.substring(areaKey.length()):key);
+    }
+
+    /**
+     * Creates a {@link ConfigOperator} that creates a {@link org.apache.tamaya.Configuration}
that maps any keys as
+     * defined by the {@code keyMapper} given. If the {@code keyMapper} returns
+     * {@code null} for a value, it is removed from the resulting map.
+     *
+     * @param keyMapper the key mapper, not null
+     * @return the area configuration, with the areaKey stripped away.
+     */
+    public static ConfigOperator mapKeys(UnaryOperator<String> keyMapper) {
+        return (c) -> new MappedConfiguration(c, keyMapper);
+    }
+
+}


Mime
View raw message