morristech
5/8/2018 - 12:26 PM

ComparatorBatchedCallback.java



import android.support.annotation.Nullable;
import android.support.v7.util.SortedList;

import java.util.Comparator;

/**
 * Created by Nikola D. on 8/29/2016.
 */

public class ComparatorBatchedCallback<T> extends SortedList.BatchedCallback<T> {
    @Nullable
    private Comparator<T> mComparator;

    /**
     * Creates a new BatchedCallback that wraps the provided Callback.
     *
     * @param wrappedCallback The Callback which should received the data change callbacks.
     *                        Other method calls (e.g. {@link #compare(Object, Object)} from
     *                        the SortedList are directly forwarded to this Callback.
     */
    public ComparatorBatchedCallback(SortedList.Callback<T> wrappedCallback) {
        super(wrappedCallback);
    }

    @Override
    public int compare(T o1, T o2) {
        return mComparator != null ? mComparator.compare(o1, o2) : super.compare(o1, o2);
    }

    public void setComparator(@Nullable Comparator<T> comparator) {
        mComparator = comparator;
    }
}