You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/09/30 15:02:08 UTC

[brooklyn-server] 01/03: convenience to MutableMap for transforming an existing map

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 3c71dcee8210d47f37da4e223037f92d046f4f7c
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Thu Sep 30 16:01:28 2021 +0100

    convenience to MutableMap for transforming an existing map
---
 .../org/apache/brooklyn/util/collections/MutableMap.java     | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableMap.java b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableMap.java
index 41a9bd1..280caa2 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableMap.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableMap.java
@@ -24,6 +24,7 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import java.util.function.Function;
 import javax.annotation.Nullable;
 
 import org.apache.brooklyn.util.exceptions.Exceptions;
@@ -114,7 +115,16 @@ public class MutableMap<K,V> extends LinkedHashMap<K,V> {
             result.putAll(orig);
         return result;
     }
-    
+
+    /** creates a copy of a map, transforming the values */
+    public static <K,V1,V2> MutableMap<K,V2> copyOf(@Nullable Map<? extends K, ? extends V1> orig, Function<V1,V2> transform) {
+        MutableMap<K,V2> result = new MutableMap<>();
+        if (orig!=null) {
+            orig.forEach( (k,v) -> result.put(k, transform.apply(v)) );
+        }
+        return result;
+    }
+
     public MutableMap() {}
     @SuppressWarnings("unchecked")
     public MutableMap(@SuppressWarnings("rawtypes") Map source) { super(source); }