001package org.junit.runner.manipulation;
002
003import java.util.Comparator;
004
005import org.junit.runner.Description;
006
007/**
008 * A <code>Sorter</code> orders tests. In general you will not need
009 * to use a <code>Sorter</code> directly. Instead, use {@link org.junit.runner.Request#sortWith(Comparator)}.
010 *
011 * @since 4.0
012 */
013public class Sorter implements Comparator<Description> {
014    /**
015     * NULL is a <code>Sorter</code> that leaves elements in an undefined order
016     */
017    public static final Sorter NULL = new Sorter(new Comparator<Description>() {
018        public int compare(Description o1, Description o2) {
019            return 0;
020        }
021    });
022
023    private final Comparator<Description> comparator;
024
025    /**
026     * Creates a <code>Sorter</code> that uses <code>comparator</code>
027     * to sort tests
028     *
029     * @param comparator the {@link Comparator} to use when sorting tests
030     */
031    public Sorter(Comparator<Description> comparator) {
032        this.comparator = comparator;
033    }
034
035    /**
036     * Sorts the test in <code>runner</code> using <code>comparator</code>
037     */
038    public void apply(Object object) {
039        if (object instanceof Sortable) {
040            Sortable sortable = (Sortable) object;
041            sortable.sort(this);
042        }
043    }
044
045    public int compare(Description o1, Description o2) {
046        return comparator.compare(o1, o2);
047    }
048}