Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2019 original authors
* SPDX-License-Identifier: Apache-2.0
*/
package yakworks.commons.map
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import yakworks.commons.lang.PropertyTools
import yakworks.commons.util.StringUtils
/**
* Helpful methods for dealing with maps
* some merge ideas take from https://gist.github.com/robhruska/4612278 and https://e.printstacktrace.blog/how-to-merge-two-maps-in-groovy/
*
* @author Joshua Burnett (@basejump)
*/
@Slf4j
@CompileStatic
class Maps {
/**
* Return the value of a nested path. Alias to PropertyTools.getProperty.
*
* Example Maps.getProperty(source, "x.y.z")
*
* @param source - The source object
* @param property - the property
* @return value of the specified property or null if any of the intermediate objects are null
*/
static Object value(Map source, String property) {
PropertyTools.getProperty(source, property)
}
/**
* Deeply merges the contents of each Map in sources, merging from
* "right to left" and returning the merged Map.
*
* Mimics 'extend()' functions often seen in JavaScript libraries.
* Any specific Map implementations (e.g. TreeMap, LinkedHashMap)
* are not guaranteed to be retained. The ordering of the keys in
* the result Map is not guaranteed. Only nested maps will be
* merged; primitives, objects, and other collection types will be
* overwritten.
*
* The source maps will not be modified.
*
* If only 1 map is passed in then it just returns that without making a copy or modifying
*
* @return the new merged map
*/
static Map merge(Map[] sources) {
if (sources.length == 0) return [:]
if (sources.length == 1) return sources[0]
sources.inject([:]) { merged, source ->
source.each { k, val ->
def mergedVal = merged[k]
if (( mergedVal == null || mergedVal instanceof Map ) && val instanceof Map) {
if(mergedVal == null) merged[k] = [:]
merged[k] = merge(merged[k] as Map, val as Map)
} else if ((mergedVal == null || mergedVal instanceof Collection) && val instanceof Collection) {
if(mergedVal == null) merged[k] = []
merged[k] = (Collection)merged[k] + (Collection)val
//The list could be list of maps, so make sure they get copied
merged[k] = merged[k].collect{ item ->
return (item instanceof Map) ? merge([:], item) : item
}
} else {
merged[k] = val
}
}
return merged
} as Map
}
static Map merge(List