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 Sorter NULL = new Sorter(new Comparator<Description>() {
018        public int compare(Description o1, Description o2) {
019            return 0;
020        }
021    });
022    private final Comparator<Description> fComparator;
023
024    /**
025     * Creates a <code>Sorter</code> that uses <code>comparator</code>
026     * to sort tests
027     *
028     * @param comparator the {@link Comparator} to use when sorting tests
029     */
030    public Sorter(Comparator<Description> comparator) {
031        fComparator = comparator;
032    }
033
034    /**
035     * Sorts the test in <code>runner</code> using <code>comparator</code>
036     */
037    public void apply(Object object) {
038        if (object instanceof Sortable) {
039            Sortable sortable = (Sortable) object;
040            sortable.sort(this);
041        }
042    }
043
044    public int compare(Description o1, Description o2) {
045        return fComparator.compare(o1, o2);
046    }
047}