com.zusmart.basic.graph.DirectedGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zusmart-basic Show documentation
Show all versions of zusmart-basic Show documentation
基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类
package com.zusmart.basic.graph;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class DirectedGraph implements Iterable {
private final Map> nodes = new HashMap>();
public boolean addNode(T node) {
if (nodes.containsKey(node)) {
return false;
}
nodes.put(node, new HashSet());
return true;
}
public boolean addEdge(T head, T foot) {
if (!nodes.containsKey(head)) {
this.addNode(head);
}
if (!nodes.containsKey(foot)) {
this.addNode(foot);
}
nodes.get(head).add(foot);
return true;
}
public Set getEdges(T head) {
if (nodes.containsKey(head)) {
return Collections.unmodifiableSet(nodes.get(head));
} else {
return Collections.unmodifiableSet(new HashSet());
}
}
@Override
public Iterator iterator() {
return this.nodes.keySet().iterator();
}
}