package com.smartcodeltd.jenkinsci.plugins.buildmonitor.order;

import hudson.model.AbstractProject;

import java.io.Serializable;
import java.util.Comparator;
import java.util.List;

public class ByEstimatedDuration implements Comparator<AbstractProject<?, ?>>, Serializable {

    @Override
    public int compare(AbstractProject<?, ?> a, AbstractProject<?,?> b) {
        return compareEstimatedDuration(a, b);
    }

    /**
     * Returns a sum of the estimated duration for a project and all upstream projects
     *
     * @return time
     */
    private long getTotalEstimatedDuration(AbstractProject<?, ?> project) {
        long time = project.getEstimatedDuration();

        if(!project.getUpstreamProjects().isEmpty()) {
            List<AbstractProject> upStreamProjects = project.getUpstreamProjects();
            for (AbstractProject upStreamProject : upStreamProjects) {
                time += getTotalEstimatedDuration((AbstractProject<?, ?>) upStreamProject);
            }
        }
        return time;
    }

    private int compareEstimatedDuration(AbstractProject<?, ?> a, AbstractProject<?, ?> b) {
        if(getTotalEstimatedDuration(a) < getTotalEstimatedDuration(b)) {
            return -1;
        } else if (getTotalEstimatedDuration(a) > getTotalEstimatedDuration(b)) {
            return 1;
        } else {
            return 0;
        }

    }
}

@@ -1132,115 +1132,27 @@
     
-if(getTotalEstimatedDuration(a) %3C getTotalEstimatedDuration(b)) %7B%0A            return -1;%0A        %7D else if 
+return Long.compare
 (get
@@ -1180,10 +1180,9 @@
 n(a)
- %3E
+,
  get
@@ -1211,81 +1211,9 @@
 (b))
- %7B%0A            return 1;%0A        %7D else %7B%0A            return 0;%0A        %7D
+;
 %0A%0A  

Use Long.compare where possible