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

com.ebay.jetstream.application.dataflows.DirectedGraph Maven / Gradle / Ivy

/*******************************************************************************
 *  Copyright © 2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/
package com.ebay.jetstream.application.dataflows;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/**
 * The simple implementation of directed graph.
 * 
 * @author weijin
 * 
 * @param 
 */
public class DirectedGraph {

	private final Map> map = new HashMap>();

	public DirectedGraph(T[] ts) {
		for (T t : ts) {
			addNode(t);
		}
	}

	public DirectedGraph() {
	}

	public boolean addNode(T node) {
		if (map.containsKey(node)) {
			return false;
		}

		map.put(node, new HashSet());

		return true;
	}

	public void addEdge(T start, T dest) {
		if ((!map.containsKey(dest) || !map.containsKey(start))) {
			throw new NoSuchElementException("Both nodes must be in the graph.");
		}

		map.get(start).add(dest);
	}

	public void removeEdge(T start, T dest) {
		if ((!map.containsKey(dest) || !map.containsKey(start))) {
			throw new NoSuchElementException("Both nodes must be in the graph.");
		}

		map.get(start).remove(dest);
	}

	public Set edgesFrom(T node) {
		return map.get(node);
	}

	public Map> getMap() {
		return map;
	}

	public boolean isEmpty() {
		return map.isEmpty();
	}

	public Set getNodes() {
		return map.keySet();
	}

	public String toString() {
		return map.toString();
	}

	public static void main(String[] args) {
		String[] strs = new String[] { "input1", "input2", "esperprocessor",
				"output1", "output2" };
		DirectedGraph graph = new DirectedGraph(strs);
		graph.addEdge("input1", "esperprocessor");
		graph.addEdge("input2", "esperprocessor");

		graph.addEdge("esperprocessor", "output1");
		graph.addEdge("esperprocessor", "output2");

		graph.addEdge("output2", "input1");

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy