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

com.diffplug.common.base.TreeNode Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.common.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

/** Class for manually constructing a tree, or for copying an existing tree. */
public class TreeNode {
	private TreeNode parent;
	private T content;
	private List> children;

	/** Creates a TreeNode with the given parent and content. Automatically adds itself as a child of its parent. */
	public TreeNode(TreeNode parent, T content) {
		this(parent, content, 0);
	}

	/**
	 * Creates a TreeNode with the given parent, content, and initial child capacity. Automatically adds itself as a child of its parent.
	 * 

* {@code childCapacity} is provided strictly for performance reasons. */ @SuppressWarnings("unchecked") public TreeNode(TreeNode parent, T content, int childCapacity) { this.parent = parent; this.content = content; if (parent != null) { // if it's a Collections.emptyList(), then we need to make it a list we can add to if (parent.children == Collections.EMPTY_LIST) { parent.children = new ArrayList<>(); } parent.children.add(this); } if (childCapacity == 0) { children = Collections.EMPTY_LIST; } else { children = new ArrayList<>(childCapacity); } } /** Returns the object which is encapsulated by this TreeNode. */ public T getContent() { return content; } /** Sets the object which is encapsulated by this TreeNode. */ public void setContent(T content) { this.content = content; } /** Returns the (possibly-null) parent of this TreeNode. */ public TreeNode getParent() { return parent; } /** Returns the children of this TreeNode. */ public List> getChildren() { return Collections.unmodifiableList(children); } /** Removes this TreeNode from its parent. */ public void removeFromParent() { Objects.requireNonNull(parent); parent.children.remove(this); parent = null; } @Override public String toString() { return "TreeNode[" + content + "]"; } /** Returns the path of this node, using the content's {@code toString()} method and {@code /} as the delimiter. */ public String getPath() { return getPath(Object::toString); } /** Returns the path of this node, using the given {@code toString} method and {@code /} as the delimiter. */ public String getPath(Function toString) { return getPath(toString, "/"); } /** Returns the path of this node, using the given {@code toString} method and {@code delimiter}. */ public String getPath(Function toString, String delimiter) { return TreeQuery.path(treeDef(), this, node -> toString.apply(node.getContent()), delimiter); } /** * Returns a "deep" toString, including the entire tree below this level. * * @see TreeQuery#toString(TreeDef, Object, Function, String) */ public String toStringDeep() { return TreeQuery.toString(treeDef(), this, node -> node.getContent().toString()); } /** Creates a deep copy of this TreeNode. */ public TreeNode copy() { return copy(treeDef(), this, TreeNode::getContent); } /** Recursively sorts all children using the given comparator of their content. */ public void sortChildrenByContent(Comparator comparator) { Comparator> byContent = Comparator.comparing(TreeNode::getContent, comparator); sortChildrenByNode(byContent); } /** Recursively sorts all children using the given comparator of TreeNode. */ public void sortChildrenByNode(Comparator> comparator) { Collections.sort(children, comparator); for (TreeNode child : children) { child.sortChildrenByNode(comparator); } } /** Creates a hierarchy of TreeNodes that copies the structure and content of the given tree. */ public static TreeNode copy(TreeDef treeDef, T root) { return copy(treeDef, root, Function.identity()); } /** * Creates a hierarchy of TreeNodes that copies the structure and content of the given tree, * using {@code mapper} to calculate the content of the nodes. */ public static TreeNode copy(TreeDef treeDef, T root, Function mapper) { List children = treeDef.childrenOf(root); R mapped = mapper.apply(root); TreeNode copyRoot = new TreeNode<>(null, mapped, children.size()); copyRecurse(copyRoot, treeDef, root, children, mapper); return copyRoot; } private static void copyRecurse(TreeNode copiedRoot, TreeDef treeDef, T root, List children, Function mapper) { for (T child : children) { R mapped = mapper.apply(child); List grandChildren = treeDef.childrenOf(child); copyRecurse(new TreeNode<>(copiedRoot, mapped, grandChildren.size()), treeDef, child, grandChildren, mapper); } } /** {@link TreeDef.Parented} for TreeNodes. */ @SuppressWarnings("unchecked") public static TreeDef.Parented> treeDef() { return (TreeDef.Parented>) TREE_DEF; } @SuppressWarnings("rawtypes") private static final TreeDef.Parented TREE_DEF = new TreeDef.Parented>() { @Override public List> childrenOf(TreeNode root) { return root.getChildren(); } @Override public TreeNode parentOf(TreeNode child) { return child.parent; } }; //////////////// // Test stuff // //////////////// /** * Creates a hierarchy of {@code TreeNode} using an easy-to-read array of strings. *

* Spaces are used to represent parent / child relationships, e.g. *

	 * TreeNode<String> root = createTestData(
	 *     "root",
	 *     " bigNode1",
	 *     " bigNode2",
	 *     "  child1",
	 *     "  child2",
	 *     " bigNode3"
	 * );
	 * 
* There can only be one root node, and that is the node that is returned. */ public static TreeNode createTestData(String... testData) { List test = Arrays.asList(testData); // make the first node (which should have 0 leading spaces) assert (test.size() > 0); assert (0 == TreeNode.leadingSpaces(test.get(0))); TreeNode rootNode = new TreeNode<>(null, test.get(0)); TreeNode lastNode = rootNode; int lastSpaces = 0; for (int i = 1; i < test.size(); ++i) { int newSpaces = TreeNode.leadingSpaces(test.get(i)); String name = test.get(i).substring(newSpaces); if (newSpaces == lastSpaces + 1) { // one level deeper, so the last guy should be the parent lastNode = new TreeNode<>(lastNode, name); lastSpaces = newSpaces; } else if (newSpaces <= lastSpaces) { // any level back up, or the same level TreeNode properParent = lastNode.getParent(); int diff = lastSpaces - newSpaces; for (int j = 0; j < diff; ++j) { properParent = properParent.getParent(); } lastNode = new TreeNode<>(properParent, name); lastSpaces = newSpaces; } else { throw new IllegalArgumentException("Last element \"" + test.get(i - 1) + "\"" + " and this element \"" + test.get(i) + "\" have too many spaces between them."); } } return rootNode; } /** Helps makeDummyTree */ private static int leadingSpaces(String name) { int i = 0; while ((i < name.length()) && (name.charAt(i) == ' ')) { ++i; } return i; } /** * Finds a child TreeNode based on its path. *

* Searches the child nodes for the first element, then that * node's children for the second element, etc. * * @throws IllegalArgumentException if no such node exists */ @SuppressWarnings("unchecked") public TreeNode findByPath(T... path) { return findByPath(Arrays.asList(path)); } /** @see #findByPath(Object...) */ public TreeNode findByPath(List path) { Optional> result = TreeQuery.findByPath(treeDef(), this, TreeNode::getContent, path, Function.identity()); if (result.isPresent()) { return result.get(); } else { throw new IllegalArgumentException(this.toString() + " has no element with path " + path); } } /** * Searches breadth-first for the TreeNode with the given content. * * @throws IllegalArgumentException if no such node exists */ public TreeNode findByContent(T content) { Optional> opt = TreeStream.breadthFirst(treeDef(), this).filter(node -> node.getContent().equals(content)).findFirst(); if (opt.isPresent()) { return opt.get(); } else { throw new IllegalArgumentException(this.toString() + " has no child with content " + content); } } }