package com.knecon.fforesight.service.layoutparser.processor.utils;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
@SuppressWarnings("all")
public final class DoubleComparisons {

    private final static float EPSILON = 0.1f;


    public static boolean feq(double f1, double f2) {

        return (Math.abs(f1 - f2) < EPSILON);
    }


    public static float round(double d, int decimalPlace) {

        BigDecimal bd = BigDecimal.valueOf(d);
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.floatValue();
    }


    public static <T> void sort(List<T> list, Comparator<? super T> comparator) {

        try {
            QuickSort.sort(list, comparator);
        } catch (IllegalArgumentException e) {
            // This should not happen since we use QuickSort from PDFBox
            log.warn(e.getMessage());
        }
    }

}

