com.iwuyc.tools.commons.basic.collections.NavigateTreeNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iwuyc-common Show documentation
Show all versions of iwuyc-common Show documentation
Common tools.Include utility classes,and much much more.
The newest version!
package com.iwuyc.tools.commons.basic.collections;
import com.iwuyc.tools.commons.util.collection.MapUtil;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Setter;
import java.io.Serializable;
import java.util.*;
/**
* NavigateTreeNode
*
* @author Neil
* @version 1.0.0
* @since 2022.1
*/
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class NavigateTreeNode implements Serializable {
private static final long serialVersionUID = 3631084533193623618L;
/**
* child nodes
*/
private final Map> children = new HashMap<>();
private final K parentKey;
/**
* unique key
*/
@EqualsAndHashCode.Include
private final K primaryKey;
/**
* value,actually save data field
*/
@Setter(AccessLevel.PACKAGE)
private V val;
/**
* left score
*/
private int left;
/**
* right score
*/
private int right;
private NavigateTreeNode(K parentKey, K primaryKey) {
this.parentKey = parentKey;
this.primaryKey = primaryKey;
}
/**
* create node data
*
* @param parentKey parent key the type same as primary key
* @param primaryKey primary key of current node
* @param Key type
* @param value type
* @return Navigate Tree Node instance
*/
static NavigateTreeNode createNode(K parentKey, K primaryKey) {
return new NavigateTreeNode<>(parentKey, primaryKey);
}
/**
* add child node
*
* @param child child node
*/
public void addChild(NavigateTreeNode child) {
this.children.put(child.getPrimaryKey(), child);
}
public boolean hasChildren() {
return !MapUtil.isEmpty(this.children);
}
/**
* 获取所有子节点,包含孙子节点的所有主键key,但不包含当前节点的主键
*/
public Set getAllChildrenPrimaryKey() {
if (!hasChildren()) {
return Collections.emptySet();
}
final Set result = new HashSet<>();
final Deque> childNodes = new ArrayDeque<>(this.children.values());
do {
final NavigateTreeNode child = childNodes.pop();
result.add(child.getPrimaryKey());
if (child.hasChildren()) {
childNodes.addAll(child.children.values());
}
} while (!childNodes.isEmpty());
return result;
}
}