88import io .jenkins .plugins .pipelinegraphview .utils .PipelineNodeUtil ;
99import java .io .IOException ;
1010import java .util .ArrayList ;
11- import java .util .Collections ;
11+ import java .util .Collection ;
1212import java .util .LinkedHashMap ;
1313import java .util .List ;
1414import java .util .Map ;
@@ -72,9 +72,9 @@ public void build() {
7272 logger .debug ("Building graph" );
7373 }
7474 if (execution != null ) {
75- LinkedHashMap < String , FlowNode > nodes = getAllNodes ();
75+ Collection < FlowNode > nodes = getAllNodes ();
7676 NodeRelationshipFinder finder = new NodeRelationshipFinder ();
77- LinkedHashMap <String , NodeRelationship > relationships = finder .getNodeRelationships (nodes );
77+ Map <String , NodeRelationship > relationships = finder .getNodeRelationships (nodes );
7878 GraphBuilder builder = new GraphBuilder (nodes , relationships , this .run , this .execution );
7979 if (isDebugEnabled ) {
8080 logger .debug ("Original nodes:" );
@@ -100,18 +100,17 @@ public void build() {
100100 /**
101101 * Gets all the nodes that are reachable in the graph.
102102 */
103- private LinkedHashMap < String , FlowNode > getAllNodes () {
103+ private List < FlowNode > getAllNodes () {
104104 heads = execution .getCurrentHeads ();
105105 final DepthFirstScanner scanner = new DepthFirstScanner ();
106106 scanner .setup (heads );
107107
108108 // nodes that we've visited
109- final LinkedHashMap <String , FlowNode > nodeMap = new LinkedHashMap <>();
110-
109+ final List <FlowNode > nodes = new ArrayList <>();
111110 for (FlowNode n : scanner ) {
112- nodeMap . put ( n . getId (), n );
111+ nodes . add ( n );
113112 }
114- return nodeMap ;
113+ return nodes ;
115114 }
116115
117116 @ NonNull
@@ -123,7 +122,7 @@ public List<FlowNodeWrapper> getStageSteps(String startNodeId) {
123122 stageSteps .add (wrappedStep );
124123 }
125124 }
126- Collections .sort (stageSteps , new FlowNodeWrapper .NodeComparator ());
125+ stageSteps .sort (new FlowNodeWrapper .NodeComparator ());
127126 if (isDebugEnabled ) {
128127 logger .debug ("Returning {} steps for node '{}'" , stageSteps .size (), startNodeId );
129128 }
@@ -142,7 +141,7 @@ public Map<String, List<FlowNodeWrapper>> getAllSteps() {
142141 @ NonNull
143142 public List <FlowNodeWrapper > getPipelineNodes () {
144143 List <FlowNodeWrapper > stageNodes = new ArrayList <>(this .stageNodeMap .values ());
145- Collections .sort (stageNodes , new FlowNodeWrapper .NodeComparator ());
144+ stageNodes .sort (new FlowNodeWrapper .NodeComparator ());
146145 return stageNodes ;
147146 }
148147
@@ -157,7 +156,7 @@ public boolean isDeclarative() {
157156 }
158157
159158 private static class GraphBuilder {
160- private final Map < String , FlowNode > nodeMap ;
159+ private final Collection < FlowNode > nodes ;
161160 private final Map <String , NodeRelationship > relationships ;
162161 private final WorkflowRun run ;
163162
@@ -182,11 +181,11 @@ private static class GraphBuilder {
182181 * in the same graph.
183182 */
184183 public GraphBuilder (
185- @ NonNull Map < String , FlowNode > nodeMap ,
184+ Collection < FlowNode > nodes ,
186185 @ NonNull Map <String , NodeRelationship > relationships ,
187186 @ NonNull WorkflowRun run ,
188187 @ NonNull FlowExecution execution ) {
189- this .nodeMap = nodeMap ;
188+ this .nodes = nodes ;
190189 this .relationships = relationships ;
191190 this .run = run ;
192191 this .inputAction = run .getAction (InputAction .class );
@@ -195,9 +194,7 @@ public GraphBuilder(
195194 }
196195
197196 protected List <FlowNodeWrapper > getNodes () {
198- return wrappedNodeMap .entrySet ().stream ()
199- .map (entrySet -> entrySet .getValue ())
200- .collect (Collectors .toList ());
197+ return new ArrayList <>(wrappedNodeMap .values ());
201198 }
202199
203200 /*
@@ -307,11 +304,11 @@ private boolean isStartNode(FlowNodeWrapper n) {
307304 }
308305 Map <String , FlowNodeWrapper > stepMap = this .wrappedNodeMap .entrySet ().stream ()
309306 .filter (e -> shouldBeInStepMap (e .getValue ()))
310- .collect (Collectors .toMap (e -> e . getKey (), e -> e . getValue () ));
307+ .collect (Collectors .toMap (Map . Entry :: getKey , Map . Entry :: getValue ));
311308
312309 Map <String , FlowNodeWrapper > stageMap = this .getStageMapping ();
313- List <FlowNodeWrapper > nodeList = new ArrayList <FlowNodeWrapper >(stepMap .values ());
314- Collections .sort (nodeList , new FlowNodeWrapper .NodeComparator ());
310+ List <FlowNodeWrapper > nodeList = new ArrayList <>(stepMap .values ());
311+ nodeList .sort (new FlowNodeWrapper .NodeComparator ());
315312 for (FlowNodeWrapper step : nodeList ) {
316313 FlowNodeWrapper firstParent = step .getFirstParent ();
317314 // Remap parentage of steps that aren't children of stages (e.g. are in Step
@@ -341,8 +338,9 @@ private boolean isExceptionStep(FlowNodeWrapper n) {
341338 * Builds a graph from the list of nodes and relationships given to the class.
342339 */
343340 private void buildGraph () {
344- List <FlowNode > nodeList = new ArrayList <FlowNode >(nodeMap .values ());
345- Collections .sort (nodeList , new FlowNodeWrapper .FlowNodeComparator ());
341+ List <FlowNode > nodeList = nodes .stream ()
342+ .sorted (new FlowNodeWrapper .FlowNodeComparator ())
343+ .toList ();
346344 // If the Pipeline ended with an unhandled exception, then we want to catch the
347345 // node which threw it.
348346 BlockEndNode <?> nodeThatThrewException = null ;
@@ -404,7 +402,7 @@ private void buildGraph() {
404402 * to the graph, we use the end node that we were given to act as the step
405403 * - this might need additional logic when getting the log for the exception.
406404 */
407- if (node instanceof BlockEndNode <?> && nodeMap . values () .size () <= 2 ) {
405+ if (node instanceof BlockEndNode <?> && nodes .size () <= 2 ) {
408406 if (isDebugEnabled ) {
409407 logger .debug ("getUnhandledException => Returning node: {}" , node .getId ());
410408 }
0 commit comments