From 72d9409ff48845a87b3ddaf858b83e003f9dc7a4 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Thu, 24 Jan 2019 22:54:53 +0100 Subject: [PATCH] #27 Avoiding dependency to Guava --- javac-plugin/pom.xml | 4 -- .../deptective/internal/graph/GraphUtils.java | 51 +++++++++---------- .../deptective/internal/graph/Node.java | 18 +++---- .../graph/impl/DependencyStructureMatrix.java | 8 ++- .../internal/graph/impl/FastFAS.java | 25 ++++----- .../internal/graph/impl/Tarjan.java | 15 +++--- pom.xml | 5 -- 7 files changed, 53 insertions(+), 73 deletions(-) diff --git a/javac-plugin/pom.xml b/javac-plugin/pom.xml index d7c8c75..608f3ec 100644 --- a/javac-plugin/pom.xml +++ b/javac-plugin/pom.xml @@ -45,10 +45,6 @@ com.fasterxml.jackson.core jackson-databind - - com.google.guava - guava - junit diff --git a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/GraphUtils.java b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/GraphUtils.java index f149f17..e66c8f3 100644 --- a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/GraphUtils.java +++ b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/GraphUtils.java @@ -15,14 +15,13 @@ */ 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; @@ -30,7 +29,7 @@ import org.moditect.deptective.internal.graph.impl.Tarjan; /** - * + * * @author Gerd Wütherich (gw@code-kontor.io) */ public class GraphUtils { @@ -38,19 +37,19 @@ 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> detectStronglyConnectedComponents(Collection nodes) { - return new Tarjan().detectStronglyConnectedComponents(checkNotNull(nodes)); + return new Tarjan().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> detectCycles(Collection nodes) { @@ -59,8 +58,8 @@ public static List> detectCycles(Collection 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 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 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() { diff --git a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/Node.java b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/Node.java index d957e63..544f796 100644 --- a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/Node.java +++ b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/Node.java @@ -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 (gw@code-kontor.io) */ public class Node { private Map 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,7 +40,7 @@ public String getId() { public Dependency getOutgoingDependencyTo(Node node) { - if (!hasOutgoingDependencies() || !outgoingDependencies.containsKey(checkNotNull(node))) { + if (!hasOutgoingDependencies() || !outgoingDependencies.containsKey(Objects.requireNonNull(node))) { return null; } @@ -52,7 +48,7 @@ public Dependency getOutgoingDependencyTo(Node node) { } public Set getOutgoingDependenciesTo(Collection 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 outgoingDependencies() { if (outgoingDependencies == null) { outgoingDependencies = new HashMap<>(); diff --git a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/DependencyStructureMatrix.java b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/DependencyStructureMatrix.java index b207cd4..9b0cdc1 100644 --- a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/DependencyStructureMatrix.java +++ b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/DependencyStructureMatrix.java @@ -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; @@ -91,8 +90,7 @@ public List> getCycles() { } private void initialize(Collection unorderedArtifacts) { - - checkNotNull(unorderedArtifacts); + Objects.requireNonNull(unorderedArtifacts); upwardDependencies = new ArrayList<>(); @@ -123,7 +121,7 @@ private void initialize(Collection unorderedArtifacts) { } Collections.reverse(orderedArtifacts); nodes = orderedArtifacts; - + // cycles = c.stream().filter(nodeList -> nodeList.size() > 1).collect(Collectors.toList()); } diff --git a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/FastFAS.java b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/FastFAS.java index e5bf461..1e302b3 100644 --- a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/FastFAS.java +++ b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/FastFAS.java @@ -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; /** *

* http://dl.acm.org/citation.cfm?id=595057 *

- * + * * @author Gerd Wütherich (gerd@gerd-wuetherich.de) */ public class FastFAS { @@ -50,18 +49,18 @@ public class FastFAS { *

* Creates a new instance of type {@link FastFAS}. *

- * + * * @param adjacencyMatrix the adjacency matrix */ public FastFAS(int[][] adjacencyMatrix) { - this.adjacencyMatrix = checkNotNull(adjacencyMatrix); + this.adjacencyMatrix = Objects.requireNonNull(adjacencyMatrix); } /** *

* Returns the ordered sequence. *

- * + * * @return the ordered sequence. */ public int[] getOrderedSequence() { @@ -101,9 +100,7 @@ public List 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) { *

* Tries to find and remove a sink. *

- * + * * @return true if a sink was found and removed. */ private boolean findSink() { @@ -157,7 +154,7 @@ private boolean findSink() { *

* Tries to find and remove a source. *

- * + * * @return true if a source was found and removed. */ private boolean findSource() { @@ -194,7 +191,7 @@ private boolean findSource() { /** *

*

- * + * * @return */ private boolean findVertexToRemove() { @@ -229,7 +226,7 @@ private boolean findVertexToRemove() { /** *

*

- * + * * @param vertex * @return */ @@ -253,7 +250,7 @@ private int getDelta(int vertex) { *

* Helper method. Concatenates the given lists and returns them as one array. *

- * + * * @param s1 the list s1 * @param s2 the list s2 * @return the result array. diff --git a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/Tarjan.java b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/Tarjan.java index 2d167c5..7d0c8fb 100644 --- a/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/Tarjan.java +++ b/javac-plugin/src/main/java/org/moditect/deptective/internal/graph/impl/Tarjan.java @@ -15,11 +15,10 @@ */ 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; @@ -27,15 +26,15 @@ public class Tarjan { private int _index = 0; - private ArrayList _stack = new ArrayList(); - private List> _stronglyConnectedComponents = new ArrayList>(); + private final ArrayList _stack = new ArrayList(); + private final List> _stronglyConnectedComponents = new ArrayList>(); int[] _vlowlink; int[] _vindex; private Node[] _artifacts; public List> detectStronglyConnectedComponents(Collection artifacts) { - checkNotNull(artifacts); + Objects.requireNonNull(artifacts); _artifacts = artifacts.toArray(new Node[0]); int[][] adjacencyList = GraphUtils.computeAdjacencyList(_artifacts); @@ -43,7 +42,7 @@ public List> detectStronglyConnectedComponents(Collection a } private List> executeTarjan(int[][] graph) { - checkNotNull(graph); + Objects.requireNonNull(graph); _stronglyConnectedComponents.clear(); _index = 0; @@ -66,8 +65,8 @@ private List> 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; diff --git a/pom.xml b/pom.xml index 29fb116..9b24588 100644 --- a/pom.xml +++ b/pom.xml @@ -66,11 +66,6 @@ jackson-databind 2.9.8
- - com.google.guava - guava - 27.0.1-jre -