/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

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