diff options
Diffstat (limited to 'bundle-plugin/src/main/scala/com/yahoo/container/plugin/util/Maps.scala')
-rw-r--r-- | bundle-plugin/src/main/scala/com/yahoo/container/plugin/util/Maps.scala | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/bundle-plugin/src/main/scala/com/yahoo/container/plugin/util/Maps.scala b/bundle-plugin/src/main/scala/com/yahoo/container/plugin/util/Maps.scala new file mode 100644 index 00000000000..7f12c5ba95d --- /dev/null +++ b/bundle-plugin/src/main/scala/com/yahoo/container/plugin/util/Maps.scala @@ -0,0 +1,19 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.util + +import collection.mutable.MultiMap + +/** + * @author tonytv + */ +object Maps { + def combine[K, V](map1 : Map[K, V], map2 : Map[K, V])(f : (V, V) => V) : Map[K, V] = { + def logicError : V = throw new RuntimeException("Logic error.") + def combineValues(key : K) = key -> f(map1.getOrElse(key, logicError), map2.getOrElse(key, logicError)) + + val keysInBoth = map1.keySet intersect map2.keySet + def notInBoth = !keysInBoth.contains(_ : K) + + map1.filterKeys(notInBoth) ++ map2.filterKeys(notInBoth) ++ keysInBoth.map(combineValues) + } +} |