Skip to content

Commit 181cb15

Browse files
committed
Revised all jgrapht.org.alg.* classes and corresponding test classes. All classes in jgrapht should now be ready for the next java 8 release
1 parent d6cf008 commit 181cb15

File tree

102 files changed

+1154
-1363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1154
-1363
lines changed

jgrapht-core/src/main/java/org/jgrapht/alg/AStarShortestPath.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ public AStarShortestPath(Graph<V, E> graph)
106106
private void initialize(AStarAdmissibleHeuristic<V> admissibleHeuristic)
107107
{
108108
this.admissibleHeuristic = admissibleHeuristic;
109-
openList = new FibonacciHeap<V>();
110-
vertexToHeapNodeMap = new HashMap<V, FibonacciHeapNode<V>>();
111-
closedList = new HashSet<V>();
112-
gScoreMap = new HashMap<V, Double>();
113-
cameFrom = new HashMap<V, E>();
109+
openList = new FibonacciHeap<>();
110+
vertexToHeapNodeMap = new HashMap<>();
111+
closedList = new HashSet<>();
112+
gScoreMap = new HashMap<>();
113+
cameFrom = new HashMap<>();
114114
numberOfExpandedNodes = 0;
115115
}
116116

@@ -140,7 +140,7 @@ public GraphPath<V, E> getShortestPath(
140140

141141
this.initialize(admissibleHeuristic);
142142
gScoreMap.put(sourceVertex, 0.0);
143-
FibonacciHeapNode<V> heapNode = new FibonacciHeapNode<V>(sourceVertex);
143+
FibonacciHeapNode<V> heapNode = new FibonacciHeapNode<>(sourceVertex);
144144
openList.insert(heapNode, 0.0);
145145
vertexToHeapNodeMap.put(sourceVertex, heapNode);
146146

@@ -174,7 +174,7 @@ private void expandNode(FibonacciHeapNode<V> currentNode, V endVertex)
174174
outgoingEdges = graph.edgesOf(currentNode.getData());
175175
} else if (graph instanceof DirectedGraph) {
176176
outgoingEdges =
177-
((DirectedGraph) graph).outgoingEdgesOf(currentNode.getData());
177+
((DirectedGraph<V,E>) graph).outgoingEdgesOf(currentNode.getData());
178178
}
179179

180180
for (E edge : outgoingEdges) {
@@ -200,7 +200,7 @@ private void expandNode(FibonacciHeapNode<V> currentNode, V endVertex)
200200
+ admissibleHeuristic.getCostEstimate(successor, endVertex);
201201
if (!vertexToHeapNodeMap.containsKey(successor)) {
202202
FibonacciHeapNode<V> heapNode =
203-
new FibonacciHeapNode<V>(successor);
203+
new FibonacciHeapNode<>(successor);
204204
openList.insert(heapNode, fScore);
205205
vertexToHeapNodeMap.put(successor, heapNode);
206206
} else {
@@ -227,12 +227,12 @@ private GraphPath<V, E> buildGraphPath(
227227
double pathLength)
228228
{
229229
List<E> edgeList = this.buildPath(targetVertex);
230-
return new GraphPathImpl<V, E>(
231-
graph,
232-
startVertex,
233-
targetVertex,
234-
edgeList,
235-
pathLength);
230+
return new GraphPathImpl<>(
231+
graph,
232+
startVertex,
233+
targetVertex,
234+
edgeList,
235+
pathLength);
236236
}
237237

238238
/**
@@ -256,7 +256,7 @@ private List<E> buildPath(V currentNode)
256256
path.add(cameFrom.get(currentNode));
257257
return path;
258258
} else {
259-
return new ArrayList<E>();
259+
return new ArrayList<>();
260260
}
261261
}
262262

jgrapht-core/src/main/java/org/jgrapht/alg/AbstractPathElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected AbstractPathElement(V vertex)
134134
*/
135135
public List<E> createEdgeListPath()
136136
{
137-
List<E> path = new ArrayList<E>();
137+
List<E> path = new ArrayList<>();
138138
AbstractPathElement<V, E> pathElement = this;
139139

140140
// while start vertex is not reached.

jgrapht-core/src/main/java/org/jgrapht/alg/AbstractPathElementList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ abstract class AbstractPathElementList<V,
6363
/**
6464
* Stored paths, list of <code>AbstractPathElement</code>.
6565
*/
66-
protected ArrayList<T> pathElements = new ArrayList<T>();
66+
protected ArrayList<T> pathElements = new ArrayList<>();
6767

6868
/**
6969
* Target vertex of the paths.

jgrapht-core/src/main/java/org/jgrapht/alg/AllDirectedPaths.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,21 @@ public List<GraphPath<V, E>> getAllPaths(
120120
}
121121

122122
if ((sourceVertices.isEmpty()) || (targetVertices.isEmpty())) {
123-
return new ArrayList();
123+
return Collections.emptyList();
124124
}
125125

126126
// Decorate the edges with the minimum path lengths through them
127127
Map<E, Integer> edgeMinDistancesFromTargets =
128128
edgeMinDistancesBackwards(targetVertices, maxPathLength);
129129

130130
// Generate all the paths
131-
List<GraphPath<V, E>> allPaths =
132-
generatePaths(
133-
sourceVertices,
134-
targetVertices,
135-
simplePathsOnly,
136-
maxPathLength,
137-
edgeMinDistancesFromTargets);
138-
139-
return allPaths;
131+
132+
return generatePaths(
133+
sourceVertices,
134+
targetVertices,
135+
simplePathsOnly,
136+
maxPathLength,
137+
edgeMinDistancesFromTargets);
140138
}
141139

142140
/**
@@ -159,9 +157,9 @@ private Map<E, Integer> edgeMinDistancesBackwards(
159157
* vertices, marking edges and vertices with their minimum
160158
* distances as we go.
161159
*/
162-
Map<E, Integer> edgeMinDistances = new HashMap();
163-
Map<V, Integer> vertexMinDistances = new HashMap();
164-
Queue<V> verticesToProcess = new LinkedList();
160+
Map<E, Integer> edgeMinDistances = new HashMap<>();
161+
Map<V, Integer> vertexMinDistances = new HashMap<>();
162+
Queue<V> verticesToProcess = new LinkedList<>();
165163

166164
// Input sanity checking
167165
if (maxPathLength != null) {
@@ -245,8 +243,8 @@ private List<GraphPath<V, E>> generatePaths(
245243
* vertices, exploring all outgoing edges whose minimum
246244
* distances is small enough.
247245
*/
248-
List<GraphPath<V, E>> completePaths = new ArrayList();
249-
Deque<List<E>> incompletePaths = new LinkedList();
246+
List<GraphPath<V, E>> completePaths = new ArrayList<>();
247+
Deque<List<E>> incompletePaths = new LinkedList<>();
250248

251249
// Input sanity checking
252250
if (maxPathLength != null) {
@@ -265,7 +263,7 @@ private List<GraphPath<V, E>> generatePaths(
265263
assert graph.getEdgeSource(edge).equals(source);
266264

267265
if (edgeMinDistancesFromTargets.containsKey(edge)) {
268-
List<E> path = Arrays.asList(edge);
266+
List<E> path = Collections.singletonList(edge);
269267
incompletePaths.add(path);
270268
}
271269
}
@@ -282,7 +280,7 @@ private List<GraphPath<V, E>> generatePaths(
282280
E leafEdge = incompletePath.get(lengthSoFar - 1);
283281
V leafNode = graph.getEdgeTarget(leafEdge);
284282

285-
Set<V> pathVertices = new HashSet();
283+
Set<V> pathVertices = new HashSet<>();
286284
for (E pathEdge : incompletePath) {
287285
pathVertices.add(graph.getEdgeSource(pathEdge));
288286
pathVertices.add(graph.getEdgeTarget(pathEdge));
@@ -296,7 +294,7 @@ private List<GraphPath<V, E>> generatePaths(
296294
|| ((edgeMinDistancesFromTargets.get(outEdge)
297295
+ lengthSoFar) <= maxPathLength)))
298296
{
299-
List<E> newPath = new ArrayList(incompletePath);
297+
List<E> newPath = new ArrayList<>(incompletePath);
300298
newPath.add(outEdge);
301299

302300
// If requested, make sure this path isn't self-intersecting
@@ -349,7 +347,7 @@ private GraphPath<V, E> makePath(List<E> edges)
349347
V source = graph.getEdgeSource(edges.get(0));
350348
V target = graph.getEdgeTarget(edges.get(edges.size() - 1));
351349
double weight = edges.size();
352-
return new GraphPathImpl<V, E>(graph, source, target, edges, weight);
350+
return new GraphPathImpl<>(graph, source, target, edges, weight);
353351
}
354352
}
355353

jgrapht-core/src/main/java/org/jgrapht/alg/BellmanFordIterator.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BellmanFordIterator<V, E>
7070
* Vertices whose shortest path cost have been improved during the previous
7171
* pass.
7272
*/
73-
private List<V> prevImprovedVertices = new ArrayList<V>();
73+
private List<V> prevImprovedVertices = new ArrayList<>();
7474

7575
private Map<V, BellmanFordPathElement<V, E>> prevVertexData;
7676

@@ -140,7 +140,7 @@ public BellmanFordPathElement<V, E> getPathElement(V endVertex)
140140
}
141141

142142
if (hasNext()) {
143-
List<V> improvedVertices = new ArrayList<V>();
143+
List<V> improvedVertices = new ArrayList<>();
144144
for (int i = this.prevImprovedVertices.size() - 1; i >= 0; i--) {
145145
V vertex = this.prevImprovedVertices.get(i);
146146
for (
@@ -297,7 +297,7 @@ protected BellmanFordPathElement<V, E> putPrevSeenData(
297297
{
298298
if (this.prevVertexData == null) {
299299
this.prevVertexData =
300-
new HashMap<V, BellmanFordPathElement<V, E>>();
300+
new HashMap<>();
301301
}
302302

303303
return this.prevVertexData.put(vertex, data);
@@ -318,7 +318,7 @@ protected BellmanFordPathElement<V, E> putSeenData(
318318
BellmanFordPathElement<V, E> data)
319319
{
320320
if (this.vertexData == null) {
321-
this.vertexData = new HashMap<V, BellmanFordPathElement<V, E>>();
321+
this.vertexData = new HashMap<>();
322322
}
323323

324324
return this.vertexData.put(vertex, data);
@@ -350,23 +350,20 @@ private BellmanFordPathElement<V, E> createSeenData(
350350
getPrevSeenData(
351351
Graphs.getOppositeVertex(graph, edge, vertex));
352352

353-
BellmanFordPathElement<V, E> data =
354-
new BellmanFordPathElement<V, E>(
353+
return new BellmanFordPathElement<>(
355354
graph,
356355
prevPathElement,
357356
edge,
358357
cost,
359358
epsilon);
360-
361-
return data;
362359
}
363360

364361
private void encounterStartVertex()
365362
{
366363
BellmanFordPathElement<V, E> data =
367-
new BellmanFordPathElement<V, E>(
368-
this.startVertex,
369-
epsilon);
364+
new BellmanFordPathElement<>(
365+
this.startVertex,
366+
epsilon);
370367

371368
// first the only vertex considered as improved is the start vertex.
372369
this.prevImprovedVertices.add(this.startVertex);
@@ -426,7 +423,7 @@ private void savePassData(List<V> improvedVertices)
426423
for (V vertex : improvedVertices) {
427424
BellmanFordPathElement<V, E> orig = getSeenData(vertex);
428425
BellmanFordPathElement<V, E> clonedData =
429-
new BellmanFordPathElement<V, E>(orig);
426+
new BellmanFordPathElement<>(orig);
430427
putPrevSeenData(vertex, clonedData);
431428
}
432429

jgrapht-core/src/main/java/org/jgrapht/alg/BellmanFordShortestPath.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ private void lazyCalculate()
183183
{
184184
if (this.iter == null) {
185185
this.iter =
186-
new BellmanFordIterator<V, E>(
187-
this.graph,
188-
this.startVertex,
189-
epsilon);
186+
new BellmanFordIterator<>(
187+
this.graph,
188+
this.startVertex,
189+
epsilon);
190190
}
191191

192192
// at the i-th pass the shortest paths with less (or equal) than i edges
@@ -217,9 +217,9 @@ public static <V, E> List<E> findPathBetween(
217217
V endVertex)
218218
{
219219
BellmanFordShortestPath<V, E> alg =
220-
new BellmanFordShortestPath<V, E>(
221-
graph,
222-
startVertex);
220+
new BellmanFordShortestPath<>(
221+
graph,
222+
startVertex);
223223

224224
return alg.getPathEdgeList(endVertex);
225225
}

jgrapht-core/src/main/java/org/jgrapht/alg/BiconnectivityInspector.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,16 @@ public class BiconnectivityInspector<V, E>
6060
public BiconnectivityInspector(UndirectedGraph<V, E> graph)
6161
{
6262
super();
63-
this.blockCutpointGraph = new BlockCutpointGraph<V, E>(graph);
63+
this.blockCutpointGraph = new BlockCutpointGraph<>(graph);
6464
}
6565

6666
/**
6767
* Returns the biconnected vertex-components of the graph.
6868
*/
6969
public Set<Set<V>> getBiconnectedVertexComponents()
7070
{
71-
Set<Set<V>> biconnectedVertexComponents = new HashSet<Set<V>>();
72-
for (
73-
Iterator<UndirectedGraph<V, E>> iter =
74-
this.blockCutpointGraph.vertexSet().iterator();
75-
iter.hasNext();)
76-
{
77-
UndirectedGraph<V, E> subgraph = iter.next();
71+
Set<Set<V>> biconnectedVertexComponents = new HashSet<>();
72+
for (UndirectedGraph<V, E> subgraph : this.blockCutpointGraph.vertexSet()) {
7873
if (!subgraph.edgeSet().isEmpty()) {
7974
biconnectedVertexComponents.add(subgraph.vertexSet());
8075
}
@@ -95,12 +90,8 @@ public Set<Set<V>> getBiconnectedVertexComponents()
9590
*/
9691
public Set<Set<V>> getBiconnectedVertexComponents(V vertex)
9792
{
98-
Set<Set<V>> vertexComponents = new HashSet<Set<V>>();
99-
for (
100-
Iterator<Set<V>> iter = getBiconnectedVertexComponents().iterator();
101-
iter.hasNext();)
102-
{
103-
Set<V> vertexComponent = iter.next();
93+
Set<Set<V>> vertexComponents = new HashSet<>();
94+
for (Set<V> vertexComponent : getBiconnectedVertexComponents()) {
10495
if (vertexComponent.contains(vertex)) {
10596
vertexComponents.add(vertexComponent);
10697
}

0 commit comments

Comments
 (0)