/** Deep copy the tree implemented as map of maps/leafs @param srcTree the source tree @return the tree copy */ @SuppressWarnings("unchecked") static Map<String, Object> copyTree(final Map<String, Object> srcTree) { final Map<String, Object> dstTree = new HashMap<>(srcTree.size()); srcTree.forEach( (k, v) -> dstTree.put(k, v instanceof Map ? copyTree((Map<String, Object>) v) : v) ); return dstTree; }
@SuppressWarnings("unchecked") static Map<String, Object> addBranches( final Map<String, Object> dst, final Map<String, Object> src ) { src.forEach( (k, v) -> { if(v instanceof Map) { final Object dstNode = dst.get(k); if(dstNode instanceof Map) { addBranches((Map<String, Object>) dstNode, (Map<String, Object>) v); } else { dst.put(k, v); } } else { dst.put(k, v); } } ); return dst; }
/** @param id schema id @param clsLoader clsLoader the class loader which will be used for the resolution @return the single schema reduced from the multiple schemes resolved @throws Exception */ static Map<String, Object> resolveAndReduce(final String id, final ClassLoader clsLoader) throws Exception { return reduceForest(resolve(id, clsLoader)); } }
@SuppressWarnings("unchecked") public BasicConfig( final String pathSep, final Map<String, Object> schema, final Map<String, Object> node ) throws InvalidValuePathException, InvalidValueTypeException, IllegalArgumentException { if(pathSep == null || pathSep.isEmpty()) { throw new IllegalArgumentException("Path separator should not be null/empty"); } this.pathSep = pathSep; this.schema = copyTree(schema); if(node == null || node.size() == 0) { this.node = new HashMap<>(); } else { this.node = new HashMap<>(node.size()); node.forEach(this::putBranch); } }
static Config resolveAndReduce( final String id, final ClassLoader clsLoader, final String pathSep, final Map<String, Object> schema ) throws Exception { final List<Config> resolvedConfigs = resolve(id, clsLoader, pathSep, schema); if(resolvedConfigs == null || resolvedConfigs.size() == 0) { return null; } final List<Map<String, Object>> configForest = resolvedConfigs .stream() .map(c -> c.mapVal(Config.ROOT_PATH)) .collect(Collectors.toList()); final Map<String, Object> configTree = reduceForest(configForest); return new BasicConfig(pathSep, schema, configTree); } }