package leigh.eo.sort;

import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.sort.SortCardinality;

import java.util.Locale;

public class EONumericComparator extends EOComparator {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(EONumericComparator.class);

    public EONumericComparator(String key, SortCardinality cardinality, Locale locale) {
        super(key, cardinality, locale);
    }

    @Override
    public int compare(EO o1, EO o2) {
        Double v1 = o1.getValueDouble(getKey());
        Double v2 = o2.getValueDouble(getKey());

        logger.debug("compare(). [key: {}] [v1: {}] [v2: {}]", getKey(), v1, v2);

        int result = Double.compare(v1, v2);

        switch (getCardinality()) {
            case ASCENDING:
                return result;
            case DESCENDING:
                if (result == 0) return 0;
                logger.debug("Descending case. [ret: {}] [orig: {}]", -result, result);
                return -result;
        }

        // Should never reach...
        throw new UnsupportedOperationException("Unknown cardinality.");
    }
}
