
com.diffplug.common.base.TreeNode Maven / Gradle / Ivy
Show all versions of durian Show documentation
/*
* 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 super T, String> toString) {
return getPath(toString, "/");
}
/** Returns the path of this node, using the given {@code toString} method and {@code delimiter}. */
public String getPath(Function super T, String> 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 super T> 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 super T, ? extends R> 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 super T, ? extends R> 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