Skip to content

Commit

Permalink
moditect#27 Avoiding dependency to Guava
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 24, 2019
1 parent e8ec7f6 commit 7bfae73
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 73 deletions.
4 changes: 0 additions & 4 deletions javac-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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&uuml;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) {
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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&uuml;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() {
Expand All @@ -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());
}

Expand All @@ -61,7 +57,7 @@ public boolean hasOutgoingDependencies() {
}

public void addOutgoingDependency(Dependency dependency) {
checkNotNull(dependency);
Objects.requireNonNull(dependency);
outgoingDependencies().put(dependency.getTo(), dependency);
}

Expand Down Expand Up @@ -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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package org.moditect.deptective.internal.graph.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.moditect.deptective.internal.graph.Dependency;
Expand Down Expand Up @@ -91,8 +90,7 @@ public List<List<Node>> getCycles() {
}

private void initialize(Collection<Node> unorderedArtifacts) {

checkNotNull(unorderedArtifacts);
Objects.requireNonNull(unorderedArtifacts);

upwardDependencies = new ArrayList<>();

Expand Down Expand Up @@ -123,7 +121,7 @@ private void initialize(Collection<Node> unorderedArtifacts) {
}
Collections.reverse(orderedArtifacts);
nodes = orderedArtifacts;

//
cycles = c.stream().filter(nodeList -> nodeList.size() > 1).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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&uuml;therich ([email protected])
*/
public class FastFAS {
Expand All @@ -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() {
Expand Down Expand Up @@ -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];
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -194,7 +191,7 @@ private boolean findSource() {
/**
* <p>
* </p>
*
*
* @return
*/
private boolean findVertexToRemove() {
Expand Down Expand Up @@ -229,7 +226,7 @@ private boolean findVertexToRemove() {
/**
* <p>
* </p>
*
*
* @param vertex
* @return
*/
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,34 @@
*/
package org.moditect.deptective.internal.graph.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.moditect.deptective.internal.graph.GraphUtils;
import org.moditect.deptective.internal.graph.Node;

public class Tarjan<T extends Node> {

private int _index = 0;
private ArrayList<Integer> _stack = new ArrayList<Integer>();
private List<List<T>> _stronglyConnectedComponents = new ArrayList<List<T>>();
private final ArrayList<Integer> _stack = new ArrayList<Integer>();
private final List<List<T>> _stronglyConnectedComponents = new ArrayList<List<T>>();
int[] _vlowlink;
int[] _vindex;

private Node[] _artifacts;

public List<List<T>> detectStronglyConnectedComponents(Collection<? extends T> artifacts) {
checkNotNull(artifacts);
Objects.requireNonNull(artifacts);

_artifacts = artifacts.toArray(new Node[0]);
int[][] adjacencyList = GraphUtils.computeAdjacencyList(_artifacts);
return executeTarjan(adjacencyList);
}

private List<List<T>> executeTarjan(int[][] graph) {
checkNotNull(graph);
Objects.requireNonNull(graph);

_stronglyConnectedComponents.clear();
_index = 0;
Expand All @@ -66,8 +65,8 @@ private List<List<T>> executeTarjan(int[][] graph) {

@SuppressWarnings("unchecked")
private void tarjan(int v, int[][] graph) {
checkNotNull(v);
checkNotNull(graph);
Objects.requireNonNull(v);
Objects.requireNonNull(graph);

_vindex[v] = _index;
_vlowlink[v] = _index;
Expand Down
Loading

0 comments on commit 7bfae73

Please sign in to comment.