summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/graph
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-11-12 20:05:01 -0500
committerBen Culkin <scorpress@gmail.com>2020-11-12 20:05:01 -0500
commit6dcd129a10af0034b38bfe843d223c4593deee09 (patch)
treee27cf2574a10da36d9a737e1fef48da8e5681da8 /base/src/main/java/bjc/utils/graph
parentc41cfde634d70c3a50adf3979cc5239e5ae52e73 (diff)
Cleanup part 2
Diffstat (limited to 'base/src/main/java/bjc/utils/graph')
-rw-r--r--base/src/main/java/bjc/utils/graph/AdjacencyMap.java23
-rw-r--r--base/src/main/java/bjc/utils/graph/Edge.java33
-rw-r--r--base/src/main/java/bjc/utils/graph/Graph.java38
3 files changed, 36 insertions, 58 deletions
diff --git a/base/src/main/java/bjc/utils/graph/AdjacencyMap.java b/base/src/main/java/bjc/utils/graph/AdjacencyMap.java
index e046fb5..804e232 100644
--- a/base/src/main/java/bjc/utils/graph/AdjacencyMap.java
+++ b/base/src/main/java/bjc/utils/graph/AdjacencyMap.java
@@ -32,8 +32,7 @@ public class AdjacencyMap<T> {
* @return An adjacency map defined by the text
*/
public static AdjacencyMap<Integer> fromStream(final InputStream stream) {
- if (stream == null)
- throw new NullPointerException("Input source must not be null");
+ if (stream == null) throw new NullPointerException("Input source must not be null");
/* Create the adjacency map. */
AdjacencyMap<Integer> adjacency;
@@ -60,8 +59,7 @@ public class AdjacencyMap<T> {
throw imex;
}
- if (vertexCount <= 0)
- throw new InputMismatchException(
+ if (vertexCount <= 0) throw new InputMismatchException(
"The number of vertices must be greater than 0");
final IList<Integer> vertices = new FunctionalList<>();
@@ -126,8 +124,7 @@ public class AdjacencyMap<T> {
* The set of vertices to create a map from
*/
public AdjacencyMap(final IList<T> vertices) {
- if (vertices == null)
- throw new NullPointerException("Vertices must not be null");
+ if (vertices == null) throw new NullPointerException("Vertices must not be null");
vertices.forEach(vertex -> {
final IMap<T, Integer> row = new FunctionalMap<>();
@@ -152,9 +149,7 @@ public class AdjacencyMap<T> {
sourceValue.forEach((targetKey, targetValue) -> {
final int inverseValue = adjacency.get(targetKey).get(sourceKey);
- if (targetValue != inverseValue) {
- result.replace(false);
- }
+ if (targetValue != inverseValue) result.replace(false);
});
});
@@ -172,11 +167,8 @@ public class AdjacencyMap<T> {
* The weight of the edge.
*/
public void setWeight(final T source, final T target, final int weight) {
- if (source == null) {
- throw new NullPointerException("Source vertex must not be null");
- } else if (target == null) {
- throw new NullPointerException("Target vertex must not be null");
- }
+ if (source == null) throw new NullPointerException("Source vertex must not be null");
+ else if (target == null) throw new NullPointerException("Target vertex must not be null");
if (!adjacency.containsKey(source)) {
String msg = String.format("Source vertex %s isn't present in map", source);
@@ -215,8 +207,7 @@ public class AdjacencyMap<T> {
* The stream to convert to.
*/
public void toStream(final OutputStream sink) {
- if (sink == null)
- throw new NullPointerException("Output source must not be null");
+ if (sink == null) throw new NullPointerException("Output source must not be null");
final PrintStream outputPrinter = new PrintStream(sink);
diff --git a/base/src/main/java/bjc/utils/graph/Edge.java b/base/src/main/java/bjc/utils/graph/Edge.java
index fe3d891..b48fcd0 100644
--- a/base/src/main/java/bjc/utils/graph/Edge.java
+++ b/base/src/main/java/bjc/utils/graph/Edge.java
@@ -28,11 +28,8 @@ public class Edge<T> {
* The distance between initial and terminal edge.
*/
public Edge(final T initial, final T terminal, final int distance) {
- if (initial == null) {
- throw new NullPointerException("Initial node must not be null");
- } else if (terminal == null) {
- throw new NullPointerException("Terminal node must not be null");
- }
+ if (initial == null) throw new NullPointerException("Initial node must not be null");
+ else if (terminal == null) throw new NullPointerException("Terminal node must not be null");
this.source = initial;
this.target = terminal;
@@ -41,27 +38,23 @@ public class Edge<T> {
@Override
public boolean equals(final Object obj) {
- if (this == obj)
- return true;
- else if (obj == null)
- return false;
- else if (getClass() != obj.getClass())
- return false;
+ if (this == obj) return true;
+ else if (obj == null) return false;
+ else if (getClass() != obj.getClass()) return false;
else {
final Edge<?> other = (Edge<?>) obj;
- if (distance != other.distance)
+ if (distance != other.distance) {
return false;
- else if (source == null) {
- if (other.source != null)
- return false;
- } else if (!source.equals(other.source))
+ } else if (source == null) {
+ if (other.source != null) return false;
+ } else if (!source.equals(other.source)) {
return false;
- else if (target == null) {
- if (other.target != null)
- return false;
- } else if (!target.equals(other.target))
+ } else if (target == null) {
+ if (other.target != null) return false;
+ } else if (!target.equals(other.target)) {
return false;
+ }
return true;
}
diff --git a/base/src/main/java/bjc/utils/graph/Graph.java b/base/src/main/java/bjc/utils/graph/Graph.java
index 8ff5647..1e81fcf 100644
--- a/base/src/main/java/bjc/utils/graph/Graph.java
+++ b/base/src/main/java/bjc/utils/graph/Graph.java
@@ -72,16 +72,11 @@ public class Graph<T> {
public void addEdge(final T source, final T target, final int distance,
final boolean directed) {
/* Can't add edges with a null source or target. */
- if (source == null) {
- throw new NullPointerException("The source vertex cannot be null");
- } else if (target == null) {
- throw new NullPointerException("The target vertex cannot be null");
- }
+ if (source == null) throw new NullPointerException("The source vertex cannot be null");
+ else if (target == null) throw new NullPointerException("The target vertex cannot be null");
/* Initialize adjacency list for vertices if necessary. */
- if (!backing.containsKey(source)) {
- backing.put(source, new FunctionalMap<T, Integer>());
- }
+ if (!backing.containsKey(source)) backing.put(source, new FunctionalMap<T, Integer>());
/* Add the edge to the graph. */
backing.get(source).put(target, distance);
@@ -110,16 +105,11 @@ public class Graph<T> {
*/
public void forAllEdgesMatchingAt(final T source,
final BiPredicate<T, Integer> matcher, final BiConsumer<T, Integer> action) {
- if (matcher == null) {
- throw new NullPointerException("Matcher must not be null");
- } else if (action == null) {
- throw new NullPointerException("Action must not be null");
- }
+ if (matcher == null) throw new NullPointerException("Matcher must not be null");
+ else if (action == null) throw new NullPointerException("Action must not be null");
getEdges(source).forEach((target, weight) -> {
- if (matcher.test(target, weight)) {
- action.accept(target, weight);
- }
+ if (matcher.test(target, weight)) action.accept(target, weight);
});
}
@@ -132,10 +122,11 @@ public class Graph<T> {
*/
public IMap<T, Integer> getEdges(final T source) {
/* Can't find edges for a null source. */
- if (source == null)
+ if (source == null) {
throw new NullPointerException("The source cannot be null.");
- else if (!backing.containsKey(source))
+ } else if (!backing.containsKey(source)) {
throw new IllegalArgumentException("Vertex " + source + " is not in graph");
+ }
return backing.get(source);
}
@@ -176,11 +167,14 @@ public class Graph<T> {
while (visited.size() != getVertexCount()) {
/* Grab all edges adjacent to the provided edge. */
- forAllEdgesMatchingAt(source.getValue(), (target, weight) -> !visited.contains(target), (target, weight) -> {
- final T vert = source.unwrap(vertex -> vertex);
+ forAllEdgesMatchingAt(source.getValue(),
+ (target, weight) -> !visited.contains(target),
+ (target, weight) -> {
+ final T vert = source.unwrap(vertex -> vertex);
- available.add(new Edge<>(vert, target, weight));
- });
+ available.add(new Edge<>(vert, target, weight));
+ }
+ );
/* Get the edge with the minimum distance. */
final IHolder<Edge<T>> minimum = new Identity<>(available.poll());