Skip to content

Commit 83b60c3

Browse files
committed
Code cleanup.
1 parent 19bdd50 commit 83b60c3

1 file changed

Lines changed: 39 additions & 57 deletions

File tree

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
import java.net.MalformedURLException;
4949
import java.net.URI;
5050
import java.util.*;
51-
import java.util.concurrent.*;
51+
import java.util.concurrent.ExecutionException;
52+
import java.util.concurrent.FutureTask;
53+
import java.util.concurrent.TimeUnit;
54+
import java.util.concurrent.TimeoutException;
5255
import java.util.function.Consumer;
5356
import java.util.logging.Level;
5457
import java.util.logging.Logger;
@@ -398,12 +401,9 @@ public void updateAndWait() {
398401
throw new IllegalStateException("You must call init() method before any updates.");
399402
}
400403

401-
final FutureTask update = new FutureTask(new Callable<Boolean>() {
402-
@Override
403-
public Boolean call() throws Exception {
404-
updateNodes();
405-
return true;
406-
}
404+
final FutureTask<Boolean> update = new FutureTask<>(() -> {
405+
updateNodes();
406+
return true;
407407
});
408408

409409
//
@@ -433,7 +433,7 @@ private synchronized void updateNodes() {
433433
INTERACTION WITH VERTICES AND EDGES
434434
*/
435435
/**
436-
* Sets the action that should be performed when a vertex is double clicked.
436+
* Sets the action that should be performed when a vertex is double-clicked.
437437
*
438438
* @param action action to be performed
439439
*/
@@ -442,7 +442,7 @@ public void setVertexDoubleClickAction(Consumer<SmartGraphVertex<V>> action) {
442442
}
443443

444444
/**
445-
* Sets the action that should be performed when an edge is double clicked.
445+
* Sets the action that should be performed when an edge is double-clicked.
446446
*
447447
* @param action action to be performed
448448
*/
@@ -457,7 +457,7 @@ private void initNodes() {
457457

458458
/* create vertex graphical representations */
459459
for (Vertex<V> vertex : listOfVertices()) {
460-
SmartGraphVertexNode<V> vertexAnchor = new SmartGraphVertexNode(vertex, 0, 0,
460+
SmartGraphVertexNode<V> vertexAnchor = new SmartGraphVertexNode<>(vertex, 0, 0,
461461
graphProperties.getVertexRadius(), graphProperties.getVertexAllowUserMove());
462462

463463
vertexNodes.put(vertex, vertexAnchor);
@@ -486,7 +486,7 @@ private void initNodes() {
486486
graphVertexIn.addAdjacentVertex(graphVertexOppositeOut);
487487
graphVertexOppositeOut.addAdjacentVertex(graphVertexIn);
488488

489-
SmartGraphEdgeBase graphEdge = createEdge(edge, graphVertexIn, graphVertexOppositeOut);
489+
SmartGraphEdgeBase<E,V> graphEdge = createEdge(edge, graphVertexIn, graphVertexOppositeOut);
490490

491491
/* Track Edges already placed */
492492
connections.put(edge, new Tuple<>(vertex, oppositeVertex));
@@ -511,27 +511,27 @@ private void initNodes() {
511511
}
512512
}
513513

514-
private SmartGraphEdgeBase createEdge(Edge<E, V> edge, SmartGraphVertexNode<V> graphVertexInbound, SmartGraphVertexNode<V> graphVertexOutbound) {
514+
private SmartGraphEdgeBase<E,V> createEdge(Edge<E, V> edge, SmartGraphVertexNode<V> graphVertexInbound, SmartGraphVertexNode<V> graphVertexOutbound) {
515515
/*
516516
Even if edges are later removed, the corresponding index remains the same. Otherwise, we would have to
517517
regenerate the appropriate edges.
518518
*/
519519
int edgeIndex = 0;
520-
Integer counter = placedEdges.get(new Tuple(graphVertexInbound, graphVertexOutbound));
520+
Integer counter = placedEdges.get(new Tuple<>(graphVertexInbound, graphVertexOutbound));
521521
if (counter != null) {
522522
edgeIndex = counter;
523523
}
524524

525-
SmartGraphEdgeBase graphEdge;
525+
SmartGraphEdgeBase<E,V> graphEdge;
526526

527527
if (getTotalEdgesBetween(graphVertexInbound.getUnderlyingVertex(), graphVertexOutbound.getUnderlyingVertex()) > 1
528528
|| graphVertexInbound == graphVertexOutbound) {
529-
graphEdge = new SmartGraphEdgeCurve(edge, graphVertexInbound, graphVertexOutbound, edgeIndex);
529+
graphEdge = new SmartGraphEdgeCurve<>(edge, graphVertexInbound, graphVertexOutbound, edgeIndex);
530530
} else {
531531
graphEdge = new SmartGraphEdgeLine<>(edge, graphVertexInbound, graphVertexOutbound);
532532
}
533533

534-
placedEdges.put(new Tuple(graphVertexInbound, graphVertexOutbound), ++edgeIndex);
534+
placedEdges.put(new Tuple<>(graphVertexInbound, graphVertexOutbound), ++edgeIndex);
535535

536536
return graphEdge;
537537
}
@@ -555,7 +555,7 @@ private void addVertex(SmartGraphVertexNode<V> v) {
555555
}
556556
}
557557

558-
private void addEdge(SmartGraphEdgeBase e, Edge<E, V> edge) {
558+
private void addEdge(SmartGraphEdgeBase<E,V> e, Edge<E, V> edge) {
559559
//edges to the back
560560
this.getChildren().add(0, (Node) e);
561561
edgeNodes.put(edge, e);
@@ -606,7 +606,7 @@ private void insertNodes() {
606606

607607
if(existing == null) {
608608
/*
609-
Updates may be coming too fast and we can get out of sync.
609+
Updates may be coming too fast, and we can get out of sync.
610610
The opposite vertex exists in the (di)graph, but we have not yet
611611
created it for the panel. Therefore, its position is unknown,
612612
so place the vertex representation in the middle.
@@ -623,7 +623,7 @@ The opposite vertex exists in the (di)graph, but we have not yet
623623
}
624624
}
625625

626-
SmartGraphVertexNode newVertex = new SmartGraphVertexNode<>(vertex,
626+
SmartGraphVertexNode<V> newVertex = new SmartGraphVertexNode<>(vertex,
627627
x, y, graphProperties.getVertexRadius(), graphProperties.getVertexAllowUserMove());
628628

629629
//track new nodes
@@ -639,14 +639,14 @@ The opposite vertex exists in the (di)graph, but we have not yet
639639
for (Edge<E, V> edge : unplottedEdges) {
640640

641641
Vertex<V>[] vertices = edge.vertices();
642-
Vertex<V> u = vertices[0]; //oubound if digraph, by javadoc requirement
642+
Vertex<V> u = vertices[0]; //outbound if digraph, by javadoc requirement
643643
Vertex<V> v = vertices[1]; //inbound if digraph, by javadoc requirement
644644

645645
SmartGraphVertexNode<V> graphVertexOut = vertexNodes.get(u);
646646
SmartGraphVertexNode<V> graphVertexIn = vertexNodes.get(v);
647647

648648
/*
649-
Updates may be coming too fast and we can get out of sync.
649+
Updates may be coming too fast, and we can get out of sync.
650650
Skip and wait for another update call, since they will surely
651651
be coming at this pace.
652652
*/
@@ -657,7 +657,7 @@ The opposite vertex exists in the (di)graph, but we have not yet
657657
graphVertexOut.addAdjacentVertex(graphVertexIn);
658658
graphVertexIn.addAdjacentVertex(graphVertexOut);
659659

660-
SmartGraphEdgeBase graphEdge = createEdge(edge, graphVertexIn, graphVertexOut);
660+
SmartGraphEdgeBase<E,V> graphEdge = createEdge(edge, graphVertexIn, graphVertexOut);
661661

662662
if (this.edgesWithArrows) {
663663
SmartArrow arrow = new SmartArrow(this.graphProperties.getEdgeArrowSize());
@@ -684,7 +684,7 @@ private void removeNodes() {
684684
//remove edges (graphical elements) that were removed from the underlying graph
685685
Collection<Edge<E, V>> removedEdges = removedEdges();
686686
for (Edge<E, V> e : removedEdges) {
687-
SmartGraphEdgeBase edgeToRemove = edgeNodes.get(e);
687+
SmartGraphEdgeBase<E,V> edgeToRemove = edgeNodes.get(e);
688688
edgeNodes.remove(e);
689689
removeEdge(edgeToRemove); //remove from panel
690690

@@ -712,7 +712,7 @@ private void removeNodes() {
712712

713713
}
714714

715-
private void removeEdge(SmartGraphEdgeBase e) {
715+
private void removeEdge(SmartGraphEdgeBase<E,V> e) {
716716
getChildren().remove((Node) e);
717717

718718
SmartArrow attachedArrow = e.getAttachedArrow();
@@ -726,7 +726,7 @@ private void removeEdge(SmartGraphEdgeBase e) {
726726
}
727727
}
728728

729-
private void removeVertex(SmartGraphVertexNode v) {
729+
private void removeVertex(SmartGraphVertexNode<V> v) {
730730
getChildren().remove(v);
731731

732732
Text attachedLabel = v.getAttachedLabel();
@@ -752,7 +752,7 @@ private void updateLabels() {
752752
});
753753

754754
theGraph.edges().forEach((e) -> {
755-
SmartGraphEdgeBase edgeNode = edgeNodes.get(e);
755+
SmartGraphEdgeBase<E,V> edgeNode = edgeNodes.get(e);
756756
if (edgeNode != null) {
757757
SmartLabel label = edgeNode.getAttachedLabel();
758758
if (label != null) {
@@ -774,7 +774,7 @@ private String generateVertexLabel(V vertex) {
774774
return value.toString();
775775
}
776776
}
777-
} catch (SecurityException | IllegalAccessException | IllegalArgumentException |InvocationTargetException ex) {
777+
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
778778
Logger.getLogger(SmartGraphPanel.class.getName()).log(Level.SEVERE, null, ex);
779779
}
780780

@@ -830,21 +830,15 @@ private boolean areAdjacent(SmartGraphVertexNode<V> v, SmartGraphVertexNode<V> u
830830
}
831831

832832
private void updateForces() {
833-
vertexNodes.values().forEach((v) -> {
834-
v.updateDelta();
835-
});
833+
vertexNodes.values().forEach((v) -> v.updateDelta());
836834
}
837835

838836
private void applyForces() {
839-
vertexNodes.values().forEach((v) -> {
840-
v.moveFromForces();
841-
});
837+
vertexNodes.values().forEach((v) -> v.moveFromForces());
842838
}
843839

844840
private void resetForces() {
845-
vertexNodes.values().forEach((v) -> {
846-
v.resetForces();
847-
});
841+
vertexNodes.values().forEach((v) -> v.resetForces());
848842
}
849843

850844
private int getTotalEdgesBetween(Vertex<V> v, Vertex<V> u) {
@@ -861,19 +855,11 @@ private int getTotalEdgesBetween(Vertex<V> v, Vertex<V> u) {
861855
}
862856

863857
private List<Edge<E, V>> listOfEdges() {
864-
List<Edge<E, V>> list = new LinkedList<>();
865-
for (Edge<E, V> edge : theGraph.edges()) {
866-
list.add(edge);
867-
}
868-
return list;
858+
return new LinkedList<>(theGraph.edges());
869859
}
870860

871861
private List<Vertex<V>> listOfVertices() {
872-
List<Vertex<V>> list = new LinkedList<>();
873-
for (Vertex<V> vertex : theGraph.vertices()) {
874-
list.add(vertex);
875-
}
876-
return list;
862+
return new LinkedList<>(theGraph.vertices());
877863
}
878864

879865
/**
@@ -927,7 +913,7 @@ private Collection<Edge<E, V>> removedEdges() {
927913
Collection<Edge<E, V>> graphEdges = theGraph.edges();
928914
Collection<SmartGraphEdgeBase<E,V>> plotted = edgeNodes.values();
929915

930-
for (SmartGraphEdgeBase e : plotted) {
916+
for (SmartGraphEdgeBase<E,V> e : plotted) {
931917
if (!graphEdges.contains(e.getUnderlyingEdge())) {
932918
removed.add(e.getUnderlyingEdge());
933919
}
@@ -1068,7 +1054,7 @@ public SmartStylableNode getStylableLabel(Vertex<V> v) {
10681054
* @return stylable element (label)
10691055
*/
10701056
public SmartStylableNode getStylableLabel(Edge<E,V> e) {
1071-
SmartGraphEdgeBase edge = edgeNodes.get(e);
1057+
SmartGraphEdgeBase<E,V> edge = edgeNodes.get(e);
10721058

10731059
return edge != null ? edge.getStylableLabel() : null;
10741060
}
@@ -1096,7 +1082,7 @@ private void loadStylesheet(URI cssFile) {
10961082

10971083
/**
10981084
* Enables the double click action on this pane.
1099-
*
1085+
* <br/>
11001086
* This method identifies the node that was clicked and, if any, calls the
11011087
* appropriate consumer, i.e., vertex or edge consumers.
11021088
*/
@@ -1115,13 +1101,12 @@ private void enableDoubleClickListener() {
11151101
}
11161102

11171103
if (node instanceof SmartGraphVertex) {
1118-
SmartGraphVertex v = (SmartGraphVertex) node;
1104+
SmartGraphVertex<V> v = (SmartGraphVertex<V>) node;
11191105
vertexClickConsumer.accept(v);
11201106
} else if (node instanceof SmartGraphEdge) {
1121-
SmartGraphEdge e = (SmartGraphEdge) node;
1107+
SmartGraphEdge<E,V> e = (SmartGraphEdge<E,V>) node;
11221108
edgeClickConsumer.accept(e);
11231109
}
1124-
11251110
}
11261111
}
11271112
});
@@ -1132,7 +1117,7 @@ private void enableDoubleClickListener() {
11321117
*
11331118
* @param <T> the type of the tuple
11341119
*/
1135-
private class Tuple<T> {
1120+
private static class Tuple<T> {
11361121

11371122
private final T first;
11381123
private final T second;
@@ -1165,10 +1150,7 @@ public boolean equals(Object obj) {
11651150
if (!Objects.equals(this.first, other.first)) {
11661151
return false;
11671152
}
1168-
if (!Objects.equals(this.second, other.second)) {
1169-
return false;
1170-
}
1171-
return true;
1153+
return Objects.equals(this.second, other.second);
11721154
}
11731155
}
11741156

0 commit comments

Comments
 (0)