package io.jenkins.blueocean.service.embedded.rest;

import hudson.model.Job;
import hudson.util.RunList;
import io.jenkins.blueocean.commons.ServiceException;
import io.jenkins.blueocean.rest.model.BluePipeline;
import io.jenkins.blueocean.rest.model.BlueRun;
import io.jenkins.blueocean.rest.model.BlueRunContainer;
import jenkins.model.Jenkins;

import java.util.Iterator;
import java.util.List;

/**
 * @author Vivek Pandey
 */
public class RunContainerImpl extends BlueRunContainer {

    private final Job job;
    private final BluePipeline pipeline;

    public RunContainerImpl(BluePipeline pipeline, Job job) {
        this.job = job;
        this.pipeline = pipeline;
    }


    @Override
    public BlueRun get(String name) {
        List<Job> projects = Jenkins.getActiveInstance().getAllItems(Job.class);
        for (Job p : projects) {
            if (!p.getFullName().equals(job.getFullName())) {
                continue;
            }
            RunList<? extends hudson.model.Run> runList = p.getBuilds();

            hudson.model.Run run = null;
            if (name != null) {
                for (hudson.model.Run r : runList) {
                    if (r.getId().equals(name)) {
                        run = r;
                        break;
                    }
                }
                if (run == null) {
                    throw new ServiceException.NotFoundException(
                        String.format("Run %s not found in organization %s and pipeline %s",
                            name, pipeline.getOrganization(), job.getName()));
                }
            } else {
                run = runList.getLastBuild();
            }
            return  AbstractRunImpl.getBlueRun(run);
        }
        throw new ServiceException.NotFoundException(String.format("Run id %s not found for organization %s, pipeline: %s",
            name, pipeline.getOrganization(), job.getName()));
    }

    @Override
    public Iterator<BlueRun> iterator() {
        return RunSearch.findRuns(job).iterator();
    }

    @Override
    public BluePipeline getPipeline(String name) {
        return pipeline;
    }
}

@@ -311,24 +311,25 @@
 tainer;%0A
+%0A
 import j
 enkins.m
@@ -324,30 +324,32 @@
 rt j
-enkins.model.Jenkins;%0A
+avax.annotation.Nonnull;
 %0Aimp
@@ -375,31 +375,8 @@
 tor;
-%0Aimport java.util.List;
 %0A%0A/*
@@ -560,16 +560,25 @@
 nerImpl(
+@Nonnull 
 BluePipe
@@ -591,16 +591,25 @@
 ipeline,
+ @Nonnull
  Job job
@@ -734,228 +734,8 @@
 ) %7B%0A
-        List%3CJob%3E projects = Jenkins.getActiveInstance().getAllItems(Job.class);%0A        for (Job p : projects) %7B%0A            if (!p.getFullName().equals(job.getFullName())) %7B%0A                continue;%0A            %7D%0A    
     
@@ -784,17 +784,19 @@
 nList = 
-p
+job
 .getBuil
@@ -802,20 +802,16 @@
 lds();%0A%0A
-    
         
@@ -843,28 +843,24 @@
 ll;%0A        
-  
-  
 if (name != 
@@ -859,36 +859,32 @@
 name != null) %7B%0A
-    
             for 
@@ -928,28 +928,24 @@
             
-  
-  
 if (r.getId(
@@ -978,28 +978,24 @@
             
-    
 run = r;%0A   
@@ -1011,20 +1011,16 @@
         
- 
-   
 break;%0A 
@@ -1026,36 +1026,32 @@
                 
-    
 %7D%0A              
@@ -1044,34 +1044,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -1093,28 +1093,24 @@
             
- 
-   
 throw new Se
@@ -1143,20 +1143,16 @@
 eption(%0A
-    
         
@@ -1248,36 +1248,32 @@
                 
-    
 name, pipeline.g
@@ -1307,20 +1307,16 @@
 me()));%0A
-    
         
@@ -1329,20 +1329,16 @@
         
-    
 %7D else %7B
@@ -1338,20 +1338,16 @@
  else %7B%0A
-    
         
@@ -1388,33 +1388,26 @@
         
-    %7D%0A   
+%7D%0A
         
  return 
@@ -1394,25 +1394,24 @@
   %7D%0A        
- 
 return  Abst
@@ -1443,205 +1443,8 @@
 n);%0A
-        %7D%0A        throw new ServiceException.NotFoundException(String.format(%22Run id %25s not found for organization %25s, pipeline: %25s%22,%0A            name, pipeline.getOrganization(), job.getName()));%0A
     

Remove iteration in RunContainerImpl.get