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

org.bbottema.javareflection.util.graph.NodePathComparator Maven / Gradle / Ivy

Go to download

Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions

There is a newer version: 4.1.0
Show newest version
package org.bbottema.javareflection.util.graph;

import java.util.Comparator;
import java.util.List;

class NodePathComparator implements Comparator>> {
	
	private static final NodePathComparator INSTANCE = new NodePathComparator<>();
	
	@SuppressWarnings("unchecked")
	static  NodePathComparator INSTANCE() {
		return (NodePathComparator) INSTANCE;
	}
	
	private NodePathComparator() {
	}
	
	@Override
	public int compare(List> nodes1, List> nodes2) {
		return sumNodes(nodes1).compareTo(sumNodes(nodes2));
	}
	
	/**
	 * Since given list of nodes represents a connected path, each subsequent nodes will exist in each previous node' toNodes collection.
	 * 

* We need to calculate the distance this way, since Dijkstra's algorithm mutates the distance property for a single shortes path to a * starting node. */ private Integer sumNodes(List> nodes) { Node currentFromNode = null; int nodesCost = 0; for (Node nodeInPath : nodes) { if (currentFromNode != null) { nodesCost += currentFromNode.getToNodes().get(nodeInPath); } currentFromNode = nodeInPath; } return nodesCost; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy