/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection;

import java.io.Serializable;
import java.util.Comparator;

/**
 * An abstract class for comparison primitive int values.
 *
 * @see java.util.Comparator
 */
public interface IntComparator extends Comparator<Integer>, Serializable {
    
    IntComparator ASC = Integer::compare;
    
    IntComparator DESC = ASC.reversed();
    
    int compare(int a, int b);

    @Override
    default int compare(Integer a, Integer b) {
        return compare(a.intValue(), b.intValue());
    }
    
    @Override
    default IntComparator reversed() {
        IntComparator that = this;
        return (int a, int b) -> -that.compare(a, b);
    }
    
    static IntComparator wrap(Comparator<? super Integer> comparator) {
        if (comparator == null || comparator instanceof IntComparator) {
            return (IntComparator) comparator;
        } else {
            return (int a, int b) -> comparator.compare(a, b);
        }
    }

    @SuppressWarnings("unchecked")    
    static IntComparator $wrap(Comparator<?> comparator) {
        return wrap((Comparator)comparator);
    }
    
}
