Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small Code Maintenance #1222

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,7 @@ public LabeledPath<V> clone()

return newLabeledPath;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ public CapacitatedSpanningTreeSolutionRepresentation clone()

return capacitatedSpanningTreeSolutionRepresentation;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.jgrapht.event;

import java.util.EventListener;

/**
* A listener on graph iterator or on a graph traverser.
*
Expand All @@ -25,7 +27,7 @@
*
* @author Barak Naveh
*/
public interface TraversalListener<V, E>
public interface TraversalListener<V, E> extends EventListener
{
/**
* Called to inform listeners that the traversal of the current connected component has
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ public Object clone()

return newGraph;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}

Expand Down
18 changes: 6 additions & 12 deletions jgrapht-core/src/main/java/org/jgrapht/graph/AbstractGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public boolean containsEdge(V sourceVertex, V targetVertex)
@Override
public boolean removeAllEdges(Collection<? extends E> edges)
{
Objects.requireNonNull(edges);
boolean modified = false;

for (E e : edges) {
Expand Down Expand Up @@ -90,6 +91,7 @@ public Set<E> removeAllEdges(V sourceVertex, V targetVertex)
@Override
public boolean removeAllVertices(Collection<? extends V> vertices)
{
Objects.requireNonNull(vertices);
boolean modified = false;

for (V v : vertices) {
Expand Down Expand Up @@ -124,10 +126,9 @@ public String toString()
*/
protected boolean assertVertexExist(V v)
{
Objects.requireNonNull(v);
if (containsVertex(v)) {
return true;
} else if (v == null) {
throw new NullPointerException();
} else {
throw new IllegalArgumentException("no such vertex in graph: " + v.toString());
}
Expand All @@ -136,7 +137,6 @@ protected boolean assertVertexExist(V v)
/**
* Removes all the edges in this graph that are also contained in the specified edge array.
* After this call returns, this graph will contain no edges in common with the specified edges.
* This method will invoke the {@link Graph#removeEdge(Object)} method.
*
* @param edges edges to be removed from this graph.
*
Expand All @@ -145,18 +145,12 @@ protected boolean assertVertexExist(V v)
* @throws NullPointerException if argument is {@code null}
* @throws UnsupportedOperationException if this graph disallows modification
*
* @see Graph#removeEdge(Object)
* @see Graph#containsEdge(Object)
* @see #removeAllEdges(Collection)
*/
protected boolean removeAllEdges(E[] edges)
{
boolean modified = false;

for (E edge : edges) {
modified |= removeEdge(edge);
}

return modified;
Objects.requireNonNull(edges);
return removeAllEdges(Arrays.asList(edges));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ public Object clone()
return g;
} catch (CloneNotSupportedException e) {
// should never get here since we're Cloneable
e.printStackTrace();
throw new RuntimeException("internal error");
throw new Error("internal error", e);
}
}

Expand Down
9 changes: 6 additions & 3 deletions jgrapht-core/src/main/java/org/jgrapht/util/ArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.jgrapht.util;

import java.util.Objects;

/**
* Utility class to simplify handling of arrays.
*
Expand Down Expand Up @@ -60,9 +62,7 @@ public static final <V> void reverse(V[] arr, int from, int to)
public static final void reverse(int[] arr, int from, int to)
{
for (int i = from, j = to; i < j; ++i, --j) {
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
swap(arr, i, j);
}
}

Expand All @@ -79,6 +79,7 @@ public static final void reverse(int[] arr, int from, int to)
*/
public static final <V> void swap(V[] arr, int i, int j)
{
Objects.requireNonNull(arr);
V tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
Expand All @@ -98,6 +99,7 @@ public static final <V> void swap(V[] arr, int i, int j)
*/
public static void swap(double[] arr, int i, int j)
{
Objects.requireNonNull(arr);
double tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
Expand All @@ -117,6 +119,7 @@ public static void swap(double[] arr, int i, int j)
*/
public static void swap(int[] arr, int i, int j)
{
Objects.requireNonNull(arr);
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/**
* Binary operator for edge weights. There are some prewritten operators.
*/
@FunctionalInterface
public interface WeightCombiner
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;

import static org.jgrapht.alg.tour.TwoApproxMetricTSPTest.assertHamiltonian;
import static org.junit.jupiter.api.Assertions.*;

/**
* Unit tests for the {@link FarthestInsertionHeuristicTSP}
Expand All @@ -48,7 +49,7 @@ public void testDirectedGraph()
FarthestInsertionHeuristicTSP<Integer, DefaultWeightedEdge> farthestInsertion =
new FarthestInsertionHeuristicTSP<>();

IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> {
assertThrows(IllegalArgumentException.class, () -> {
farthestInsertion.getTour(graph);
});

Expand All @@ -64,7 +65,7 @@ public void testEmptyGraph()
new DefaultUndirectedWeightedGraph<>(DefaultWeightedEdge.class);
FarthestInsertionHeuristicTSP<Integer, DefaultWeightedEdge> farthestInsertion =
new FarthestInsertionHeuristicTSP<>();
IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> {
assertThrows(IllegalArgumentException.class, () -> {
farthestInsertion.getTour(graph);
});
}
Expand All @@ -81,7 +82,7 @@ public void testNoCompleteGraph()
graph.addVertex(1);
FarthestInsertionHeuristicTSP<Integer, DefaultWeightedEdge> farthestInsertion =
new FarthestInsertionHeuristicTSP<>();
IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> {
assertThrows(IllegalArgumentException.class, () -> {
farthestInsertion.getTour(graph);
});
}
Expand All @@ -99,7 +100,7 @@ public void testGetTour1()
new FarthestInsertionHeuristicTSP<>();
GraphPath<Integer, DefaultEdge> tour = farthestInsertion.getTour(graph);
assertHamiltonian(graph, tour);
Assertions.assertEquals(10, tour.getWeight(), 1e-9);
assertEquals(10, tour.getWeight(), 1e-9);
}

/**
Expand All @@ -115,7 +116,7 @@ public void testGetTour2()
new FarthestInsertionHeuristicTSP<>();
GraphPath<Integer, DefaultEdge> tour = farthestInsertion.getTour(graph);
assertHamiltonian(graph, tour);
Assertions.assertEquals(19, tour.getWeight(), 1e-9);
assertEquals(19, tour.getWeight(), 1e-9);
}

/**
Expand All @@ -134,8 +135,8 @@ public void testDummyGraph5()
var farthestInsH = new FarthestInsertionHeuristicTSP<Integer, DefaultWeightedEdge>();

var tour = farthestInsH.getTour(graph);
Assertions.assertEquals(30, tour.getWeight(), 1e-9);
Assertions.assertArrayEquals(new Integer[]{3, 2, 1, 0, 4, 3},
assertEquals(30, tour.getWeight(), 1e-9);
assertArrayEquals(new Integer[]{3, 2, 1, 0, 4, 3},
tour.getVertexList().toArray(new Integer[0]));
}

Expand All @@ -153,10 +154,10 @@ public void testDummyGraph5WithSubtour()
<Integer, DefaultWeightedEdge>(new GraphWalk<>(graph, List.of(3, 2, 0, 4), -1));

var tour = farthestInsH.getTour(graph);
Assertions.assertEquals(30, tour.getWeight(), 1e-9);
assertEquals(30, tour.getWeight(), 1e-9);

// vertex 1 should be inserted between vertices 2 and 0
Assertions.assertArrayEquals(new Integer[]{3, 2, 1, 0, 4, 3},
assertArrayEquals(new Integer[]{3, 2, 1, 0, 4, 3},
tour.getVertexList().toArray(new Integer[0]));
}

Expand All @@ -180,8 +181,8 @@ public void testDummyGraph10()
Graph<Integer, DefaultWeightedEdge> graph = createGraphFromMatrixDistances(allDist);
var farthestInsertion = new FarthestInsertionHeuristicTSP<Integer, DefaultWeightedEdge>();
var tour = farthestInsertion.getTour(graph);
Assertions.assertEquals(210, tour.getWeight(), 1e-9);
Assertions.assertArrayEquals(new Integer[]{4, 5, 1, 6, 0, 7, 3, 8, 2, 9, 4},
assertEquals(210, tour.getWeight(), 1e-9);
assertArrayEquals(new Integer[]{4, 5, 1, 6, 0, 7, 3, 8, 2, 9, 4},
tour.getVertexList().toArray(new Integer[0]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public Object clone()

return newGraph;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public Object clone()

return newGraph;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ public Object clone()

return newGraph;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ public Object clone()

return newGraph;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ public Object clone()

return newGraph;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public int hashCode()
}

@Override
@SuppressWarnings("rawtypes")
public boolean equals(Object obj)
{
if (this == obj)
Expand All @@ -165,7 +164,7 @@ public boolean equals(Object obj)
return false;
if (getClass() != obj.getClass())
return false;
DefaultAttribute other = (DefaultAttribute) obj;
DefaultAttribute<?> other = (DefaultAttribute<?>) obj;
if (type != other.type)
return false;
if (value == null) {
Expand Down