![JAR search and dependency download from the Maven repository](/logo.png)
org.bbottema.javareflection.util.graph.NodePathComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-reflection Show documentation
Show all versions of java-reflection Show documentation
Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions
/*
* Copyright (C) ${project.inceptionYear} Benny Bottema ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bbottema.javareflection.util.graph;
import java.util.Comparator;
import java.util.List;
import static org.bbottema.javareflection.util.MiscUtil.trustedCast;
class NodePathComparator implements Comparator>> {
private static final NodePathComparator> INSTANCE = new NodePathComparator<>();
static NodePathComparator INSTANCE() {
return trustedCast(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 - 2025 Weber Informatics LLC | Privacy Policy