No more guava

This commit is contained in:
Frank van der Heijden 2021-08-30 23:09:45 +02:00
parent 49fa7d3e12
commit f5636c082e
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
3 changed files with 51 additions and 25 deletions

View file

@ -0,0 +1,48 @@
package net.frankheijden.serverutils.common.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class HashGraph<T> {
private final Set<T> nodes;
private final Map<T, Set<T>> successors;
private final Map<T, Set<T>> predecessors;
public HashGraph() {
this(16);
}
/**
* Constructs a new hash-based graph.
*/
public HashGraph(int initialCapacity) {
this.nodes = new HashSet<>(initialCapacity);
this.successors = new HashMap<>(initialCapacity);
this.predecessors = new HashMap<>(initialCapacity);
}
public void addNode(T node) {
this.nodes.add(node);
}
public void putEdge(T from, T to) {
this.successors.computeIfAbsent(from, k -> new HashSet<>()).add(to);
this.predecessors.computeIfAbsent(to, k -> new HashSet<>()).add(from);
}
public Set<T> nodes() {
return this.nodes;
}
public Set<T> successors(T node) {
return this.successors.getOrDefault(node, Collections.emptySet());
}
public Set<T> predecessors(T node) {
return this.predecessors.getOrDefault(node, Collections.emptySet());
}
}

View file

@ -1,8 +1,5 @@
package net.frankheijden.serverutils.common.utils;
import com.google.common.graph.Graph;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
@ -10,6 +7,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.frankheijden.serverutils.common.util.HashGraph;
public class DependencyUtils {
@ -22,7 +20,7 @@ public class DependencyUtils {
*/
@SuppressWarnings("UnstableApiUsage")
public static <T> List<T> determineOrder(Map<T, Set<T>> dependencyMap) throws IllegalStateException {
MutableGraph<T> dependencyGraph = GraphBuilder.directed().allowsSelfLoops(true).build();
HashGraph<T> dependencyGraph = new HashGraph<>();
for (T node : dependencyMap.keySet()) {
dependencyGraph.addNode(node);
}
@ -45,7 +43,7 @@ public class DependencyUtils {
@SuppressWarnings("UnstableApiUsage")
private static <T> void visitNode(
Graph<T> dependencyGraph,
HashGraph<T> dependencyGraph,
T node,
Map<T, Mark> marks,
List<T> orderedList,