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