All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.akurilov.commons.collection.TreeUtil Maven / Gradle / Ivy

package com.github.akurilov.commons.collection;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface TreeUtil {

	/**
	 Deep copy the tree implemented as map of maps/leafs
	 @param srcTree the source tree
	 @return the tree copy
	 */
	@SuppressWarnings("unchecked")
	static Map copyTree(final Map srcTree) {
		final Map dstTree = new HashMap<>(srcTree.size());
		srcTree.forEach(
			(k, v) -> dstTree.put(k, v instanceof Map ? copyTree((Map) v) : v)
		);
		return dstTree;
	}

	@SuppressWarnings("unchecked")
	static Map addBranches(
		final Map dst, final Map src
	) {
		src.forEach(
			(k, v) -> {
				if(v instanceof Map) {
					final Object dstNode = dst.get(k);
					if(dstNode instanceof Map) {
						addBranches((Map) dstNode, (Map) v);
					} else {
						dst.put(k, v);
					}
				} else {
					dst.put(k, v);
				}
			}
		);
		return dst;
	}

	/**
	 Merge the tree by reducing the given forest. Any leaf in any next tree is added to the
	 resulting tree, overwriting already existing leaf if any
	 @param forest the given forest (list of trees)
	 @return the resulting tree (may be empty)
	 */
	static Map reduceForest(final List> forest) {
		return forest
			.stream()
			.reduce(TreeUtil::addBranches)
			.orElseGet(Collections::emptyMap);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy