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

com.harium.etyl.linear.graph.GenericComplexGraph Maven / Gradle / Ivy

package com.harium.etyl.linear.graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.harium.etyl.linear.Point2D;

public abstract class GenericComplexGraph> {

	protected Set edges;
	protected Set> nodes;

	protected Map, List> edgesByNodes;

	public GenericComplexGraph() {
		super();

		nodes = new LinkedHashSet>();
		edges = new LinkedHashSet();
		edgesByNodes = new HashMap, List>();
	}

	public Set> getNodes() {
		return nodes;
	}

	public void setNodes(Set> nodes) {
		this.nodes = nodes;
	}

	public void addNode(Node node) {
		this.nodes.add(node);
	}

	public void addNode(Point2D point) {
		Node node = new Node();
		node.setLocation(point.getX(), point.getY());

		nodes.add(node);		
	}

	public Map, List> getAllEdges() {
		return edgesByNodes;
	}

	public List getAllEdgesAsList() {

		List list = new ArrayList();
		for(List nodeEdges : edgesByNodes.values()) {
			list.addAll(nodeEdges);
		}

		return list;
	}

	public Set getEdges() {
		return edges;
	}

	public List getEdges(Node node) {

		if(edgesByNodes.containsKey(node)) {
			return edgesByNodes.get(node);
		}

		return new ArrayList();
	}

	public void addEdge(E edge) {

		edges.add(edge);
		addNodesFromEdge(edge);

		Node origin = edge.getOrigin();

		if(!edgesByNodes.containsKey(origin)) {
			edgesByNodes.put(origin, new ArrayList());
		}

		List originEdges = edgesByNodes.get(origin);
		if (!originEdges.contains(edge)) {
			edgesByNodes.get(origin).add(0, edge);
		}
	}

	private void addNodesFromEdge(GenericEdge edge) {

		Node origin = edge.getOrigin();

		if(!nodes.contains(origin)) {
			nodes.add(origin);
		}

		Node destination = edge.getDestination();

		if(!nodes.contains(destination)) {
			nodes.add(destination);
		}
	}

	public boolean hasDiretionalEdge(Node origin, Node destination) {
		for(E edge: edges) {
			if(edge.getOrigin() == origin && edge.getDestination() == destination) {
				return true;
			}
		}
		return false;
	}

	public boolean hasEdge(Node origin, Node destination) {

		for(E edge: edges) {
			if(edge.getOrigin() == origin && edge.getDestination() == destination) {
				return true;
			}
			if(edge.getDestination() == origin && edge.getOrigin() == destination) {
				return true;
			}
		}
		return false;
	}

	public void mergeGraph(GenericComplexGraph anotherGraph) {
		for(E edge : anotherGraph.edges) {
			addEdge(edge);	
		}
	}

	public void clear() {
		nodes.clear();
		edges.clear();
		edgesByNodes.clear();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy