package cn.com.qmth.stmms.ms.marking.service.arbitration;


import java.util.OptionalInt;
import java.util.stream.IntStream;

/**
 * 仲裁
 * Created by zhengmin on 2016/9/22.
 */
public class ArbitrationService {

    /**
     * 仲裁，如果两两差值大于误差，或不存在相同数大于半数，就判定仲裁，返回true
     *
     * @param deviation 误差
     * @param sources
     * @return
     */
    public static boolean arbitrate(int deviation, int... sources) {

        int[] values = new int[(int) Math.pow(sources.length, 2)];
        int i = 0;
        for (int source : sources) {
            for (int inner : sources) {
                values[i] = Math.abs(source - inner);
                i++;
            }
        }
        OptionalInt optionalInt = IntStream.of(values).max();
        int max = optionalInt.orElse(0);
        if (max >= deviation) {
            return true;
        }
        return false;
    }
}
