forked from moditect/deptective
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moditect#27 Avoiding dependency to Guava
- Loading branch information
1 parent
a7a6138
commit 91c89d4
Showing
7 changed files
with
53 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,42 +15,41 @@ | |
*/ | ||
package org.moditect.deptective.internal.graph; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.moditect.deptective.internal.graph.impl.DependencyStructureMatrix; | ||
import org.moditect.deptective.internal.graph.impl.FastFasSorter; | ||
import org.moditect.deptective.internal.graph.impl.Tarjan; | ||
|
||
/** | ||
* | ||
* | ||
* @author Gerd Wütherich ([email protected]) | ||
*/ | ||
public class GraphUtils { | ||
|
||
/** | ||
* A directed graph is called strongly connected if there is a path in each direction between each pair of vertices | ||
* of the graph. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. | ||
* | ||
* | ||
* @param nodes the collection of nodes (the directed graph) | ||
* @return a list of strongly connected components (SCCs). Note that the result also contains components that | ||
* contain just a single node. If you want to detect 'real' cycle (size > 1) please use {@link GraphUtils#detectCycles(Collection)}. | ||
*/ | ||
public static List<List<Node>> detectStronglyConnectedComponents(Collection<Node> nodes) { | ||
return new Tarjan<Node>().detectStronglyConnectedComponents(checkNotNull(nodes)); | ||
return new Tarjan<Node>().detectStronglyConnectedComponents(Objects.requireNonNull(nodes)); | ||
} | ||
|
||
/** | ||
* Returns all strongly connected subgraphs (size > 1) of the specified graph. | ||
* | ||
* @param nodes | ||
* Returns all strongly connected subgraphs (size > 1) of the specified graph. | ||
* | ||
* @param nodes | ||
* @return a list of strongly connected components (SCCs) with a size > 1. | ||
*/ | ||
public static List<List<Node>> detectCycles(Collection<Node> nodes) { | ||
|
@@ -59,8 +58,8 @@ public static List<List<Node>> detectCycles(Collection<Node> nodes) { | |
} | ||
|
||
/** | ||
* Creates a dependency structure matrix (DSM) for the given graph nodes. | ||
* | ||
* Creates a dependency structure matrix (DSM) for the given graph nodes. | ||
* | ||
* @param nodes the collection of nodes | ||
* @return | ||
*/ | ||
|
@@ -70,21 +69,21 @@ public static IDependencyStructureMatrix createDependencyStructureMatrix( | |
} | ||
|
||
/** | ||
* An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix | ||
* An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix | ||
* indicate whether pairs of vertices are connected (adjacent) or not in the graph. | ||
* | ||
* | ||
* @param nodes the collection of nodes | ||
* @return the adjacency matrix for the given list of nodes | ||
*/ | ||
public static int[][] computeAdjacencyMatrix(List<Node> nodes) { | ||
checkNotNull(nodes); | ||
return computeAdjacencyMatrix((Node[]) nodes.toArray(new Node[nodes.size()])); | ||
Objects.requireNonNull(nodes); | ||
return computeAdjacencyMatrix(nodes.toArray(new Node[nodes.size()])); | ||
} | ||
|
||
/** | ||
* An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix | ||
* An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix | ||
* indicate whether pairs of vertices are connected (adjacent) or not in the graph. | ||
* | ||
* | ||
* @param nodes the array of nodes | ||
* @return the adjacency matrix for the given list of nodes | ||
*/ | ||
|
@@ -100,26 +99,26 @@ public static int[][] computeAdjacencyMatrix(Node... nodes) { | |
} | ||
|
||
/** | ||
* An adjacency list is a collection of (unordered) lists used to represent a finite graph. Each list | ||
* describes the set of neighbors of a node. | ||
* | ||
* An adjacency list is a collection of (unordered) lists used to represent a finite graph. Each list | ||
* describes the set of neighbors of a node. | ||
* | ||
* @param nodes the array of nodes | ||
* @return the adjacency list for the given list of nodes | ||
*/ | ||
public static int[][] computeAdjacencyList(Collection<Node> nodes) { | ||
checkNotNull(nodes); | ||
return computeAdjacencyList((Node[]) nodes.toArray(new Node[nodes.size()])); | ||
Objects.requireNonNull(nodes); | ||
return computeAdjacencyList(nodes.toArray(new Node[nodes.size()])); | ||
} | ||
|
||
/** | ||
* An adjacency list is a collection of (unordered) lists used to represent a finite graph. Each list | ||
* describes the set of neighbors of a node. | ||
* | ||
* An adjacency list is a collection of (unordered) lists used to represent a finite graph. Each list | ||
* describes the set of neighbors of a node. | ||
* | ||
* @param nodes the array of nodes | ||
* @return the adjacency list for the given list of nodes | ||
*/ | ||
public static int[][] computeAdjacencyList(Node... nodes) { | ||
|
||
int[][] matrix; | ||
|
||
// prepare | ||
|
@@ -149,7 +148,7 @@ public static int[][] computeAdjacencyList(Node... nodes) { | |
|
||
/** | ||
* Creates a FastFAS based {@link INodeSorter}. | ||
* | ||
* | ||
* @return a FastFAS based {@link INodeSorter}. | ||
*/ | ||
public static INodeSorter createFasNodeSorter() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,27 +15,23 @@ | |
*/ | ||
package org.moditect.deptective.internal.graph; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.moditect.deptective.internal.graph.Dependency; | ||
import org.moditect.deptective.internal.graph.Node; | ||
|
||
/** | ||
* @author Gerd Wütherich ([email protected]) | ||
*/ | ||
public class Node { | ||
|
||
private Map<Node, Dependency> outgoingDependencies; | ||
private String id; | ||
private final String id; | ||
|
||
public Node(String id) { | ||
this.id = checkNotNull(id); | ||
this.id = Objects.requireNonNull(id); | ||
} | ||
|
||
public String getId() { | ||
|
@@ -44,15 +40,15 @@ public String getId() { | |
|
||
public Dependency getOutgoingDependencyTo(Node node) { | ||
|
||
if (!hasOutgoingDependencies() || !outgoingDependencies.containsKey(checkNotNull(node))) { | ||
if (!hasOutgoingDependencies() || !outgoingDependencies.containsKey(Objects.requireNonNull(node))) { | ||
return null; | ||
} | ||
|
||
return outgoingDependencies.get(node); | ||
} | ||
|
||
public Set<Dependency> getOutgoingDependenciesTo(Collection<Node> nodes) { | ||
return checkNotNull(nodes).stream().map(node -> getOutgoingDependencyTo(node)).filter(dep -> dep != null) | ||
return Objects.requireNonNull(nodes).stream().map(node -> getOutgoingDependencyTo(node)).filter(dep -> dep != null) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
|
@@ -61,7 +57,7 @@ public boolean hasOutgoingDependencies() { | |
} | ||
|
||
public void addOutgoingDependency(Dependency dependency) { | ||
checkNotNull(dependency); | ||
Objects.requireNonNull(dependency); | ||
outgoingDependencies().put(dependency.getTo(), dependency); | ||
} | ||
|
||
|
@@ -95,7 +91,7 @@ else if (!id.equals(other.id)) | |
return false; | ||
return true; | ||
} | ||
|
||
private Map<Node, Dependency> outgoingDependencies() { | ||
if (outgoingDependencies == null) { | ||
outgoingDependencies = new HashMap<>(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,18 +15,17 @@ | |
*/ | ||
package org.moditect.deptective.internal.graph.impl; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* <p> | ||
* http://dl.acm.org/citation.cfm?id=595057 | ||
* </p> | ||
* | ||
* | ||
* @author Gerd Wütherich ([email protected]) | ||
*/ | ||
public class FastFAS { | ||
|
@@ -50,18 +49,18 @@ public class FastFAS { | |
* <p> | ||
* Creates a new instance of type {@link FastFAS}. | ||
* </p> | ||
* | ||
* | ||
* @param adjacencyMatrix the adjacency matrix | ||
*/ | ||
public FastFAS(int[][] adjacencyMatrix) { | ||
this.adjacencyMatrix = checkNotNull(adjacencyMatrix); | ||
this.adjacencyMatrix = Objects.requireNonNull(adjacencyMatrix); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Returns the ordered sequence. | ||
* </p> | ||
* | ||
* | ||
* @return the ordered sequence. | ||
*/ | ||
public int[] getOrderedSequence() { | ||
|
@@ -101,9 +100,7 @@ public List<Integer[]> getSkippedEdge() { | |
} | ||
|
||
public static int[] reverse(int[] sequence) { | ||
|
||
// | ||
checkNotNull(sequence); | ||
Objects.requireNonNull(sequence); | ||
|
||
// | ||
int[] result = new int[sequence.length]; | ||
|
@@ -119,7 +116,7 @@ public static int[] reverse(int[] sequence) { | |
* <p> | ||
* Tries to find and remove a sink. | ||
* </p> | ||
* | ||
* | ||
* @return <code>true</code> if a sink was found and removed. | ||
*/ | ||
private boolean findSink() { | ||
|
@@ -157,7 +154,7 @@ private boolean findSink() { | |
* <p> | ||
* Tries to find and remove a source. | ||
* </p> | ||
* | ||
* | ||
* @return <code>true</code> if a source was found and removed. | ||
*/ | ||
private boolean findSource() { | ||
|
@@ -194,7 +191,7 @@ private boolean findSource() { | |
/** | ||
* <p> | ||
* </p> | ||
* | ||
* | ||
* @return | ||
*/ | ||
private boolean findVertexToRemove() { | ||
|
@@ -229,7 +226,7 @@ private boolean findVertexToRemove() { | |
/** | ||
* <p> | ||
* </p> | ||
* | ||
* | ||
* @param vertex | ||
* @return | ||
*/ | ||
|
@@ -253,7 +250,7 @@ private int getDelta(int vertex) { | |
* <p> | ||
* Helper method. Concatenates the given lists and returns them as one array. | ||
* </p> | ||
* | ||
* | ||
* @param s1 the list s1 | ||
* @param s2 the list s2 | ||
* @return the result array. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.