PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/queue/AbstractQueueSorterImpl.java

http://github.com/hudson/hudson
Java | 52 lines | 24 code | 6 blank | 22 comment | 4 complexity | 1f29207e0665ac9d4f21ade693224347 MD5 | raw file
 1package hudson.model.queue;
 2
 3import hudson.model.Queue.BuildableItem;
 4
 5import java.util.Collections;
 6import java.util.Comparator;
 7import java.util.List;
 8
 9/**
10 * Partial implementation of {@link QueueSorter} in terms of {@link Comparator}.
11 *
12 * @author Kohsuke Kawaguchi
13 * @since 1.343
14 */
15public abstract class AbstractQueueSorterImpl extends QueueSorter implements Comparator<BuildableItem> {
16    @Override
17    public void sortBuildableItems(List<BuildableItem> buildables) {
18        Collections.sort(buildables,this); // sort is ascending order
19    }
20
21    /**
22     * Override this method to provide the ordering of the sort.
23     *
24     * <p>
25     * if lhs should be build before rhs, return a negative value. Or put another way, think of the comparison
26     * as a process of converting a {@link BuildableItem} into a number, then doing num(lhs)-num(rhs).
27     *
28     * <p>
29     * The default implementation does FIFO.
30     */
31    public int compare(BuildableItem lhs, BuildableItem rhs) {
32        return compare(lhs.buildableStartMilliseconds,rhs.buildableStartMilliseconds);
33    }
34
35    /**
36     * sign(a-b).
37     */
38    protected static int compare(long a, long b) {
39        if (a>b)    return 1;
40        if (a<b)    return -1;
41        return 0;
42    }
43
44    /**
45     * sign(a-b).
46     */
47    protected static int compare(int a, int b) {
48        if (a>b)    return 1;
49        if (a<b)    return -1;
50        return 0;
51    }
52}