1 package org.junit.runner.manipulation; 2 3 import java.util.Comparator; 4 5 import org.junit.runner.Description; 6 7 /** 8 * A <code>Sorter</code> orders tests. In general you will not need 9 * to use a <code>Sorter</code> directly. Instead, use {@link org.junit.runner.Request#sortWith(Comparator)}. 10 * 11 * @since 4.0 12 */ 13 public class Sorter implements Comparator<Description> { 14 /** 15 * NULL is a <code>Sorter</code> that leaves elements in an undefined order 16 */ 17 public static final Sorter NULL = new Sorter(new Comparator<Description>() { 18 public int compare(Description o1, Description o2) { 19 return 0; 20 } 21 }); 22 23 private final Comparator<Description> comparator; 24 25 /** 26 * Creates a <code>Sorter</code> that uses <code>comparator</code> 27 * to sort tests 28 * 29 * @param comparator the {@link Comparator} to use when sorting tests 30 */ 31 public Sorter(Comparator<Description> comparator) { 32 this.comparator = comparator; 33 } 34 35 /** 36 * Sorts the test in <code>runner</code> using <code>comparator</code> 37 */ 38 public void apply(Object object) { 39 if (object instanceof Sortable) { 40 Sortable sortable = (Sortable) object; 41 sortable.sort(this); 42 } 43 } 44 45 public int compare(Description o1, Description o2) { 46 return comparator.compare(o1, o2); 47 } 48 } 49