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

com.zusmart.basic.graph.DirectedGraph Maven / Gradle / Ivy

Go to download

基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类

There is a newer version: 0.0.3
Show newest version
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();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy