其他分享
首页 > 其他分享> > 区间工具类

区间工具类

作者:互联网

工具类:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.math.BigDecimal;

/**
 * 区间工具类
 *
 * @author zql
 * @version 1.0
 * @createTime 2021-06-11 1:18:00
 * @modifyLog
 */
public class IntervalUtil {

    /**
     * 判断numberStr是否在interval区间范围内
     *
     * @param numberStr 数值类型的字符串
     * @param interval 正常的数学区间,包括无穷大等,如:(0, 100]、<50、(-∞,8]、(10%,50%)U(80%,100%)
     * @return true:表示numberStr在区间interval范围内,false:表示numberStr不在区间interval范围内
     */
    public static boolean isInTheInterval(String numberStr, String interval) throws ScriptException {
        // 将区间和numberStr转化为可计算的表达式
        String formula = getFormulaByAllInterval(numberStr, interval, "||");
        System.out.println(formula);
        ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
        try {
            // 计算表达式
            return (Boolean) jse.eval(formula);
        } catch (ScriptException e) {
            System.out.println("计算表达式为:" + formula);
            throw e;
        }
    }

    /**
     * 将所有阀值区间转化为公式:如
     * <pre>
     * [60,100] 转为 numberStr >= 60 && numberStr <= 100
     * (18%,56%)U(86%,100%) 转为 (numberStr > 0.18 && numberStr < 0.56) || (numberStr > 0.86 && numberStr < 1)
     * </pre>
     *
     * @param numberStr 数值类型的字符串
     * @param interval 数学区间
     * @param connector 连接符
     */
    private static String getFormulaByAllInterval(String numberStr, String interval, String connector) {
        StringBuffer sb = new StringBuffer();
        for (String limit : interval.split("U")) {
            sb.append("(");
            String formula = getFormulaByInterval(numberStr, limit, " && ");
            sb.append(formula);
            sb.append(")");
            sb.append(connector);
        }
        String allLimitInvel = sb.toString();
        int index = allLimitInvel.lastIndexOf(connector);
        allLimitInvel = allLimitInvel.substring(0, index);
        return allLimitInvel;
    }

    /**
     * 将整个阀值区间转化为公式:如
     * <pre>
     * [60, 100] 转为 numberStr >= 60 && numberStr <= 100
     * </pre>
     *
     * @param numberStr 数值类型的字符串
     * @param interval 数学区间
     * @param connector 连接符
     */
    private static String getFormulaByInterval(String numberStr, String interval, String connector) {
        StringBuffer sb = new StringBuffer();
        // 如:[60, 100]
        for (String halfInterval : interval.split(",")) {
            // 将半个阀值区间转化为公式
            String formula = getFormulaByHalfInterval(halfInterval, numberStr);
            sb.append(formula).append(connector);
        }
        String limitInvel = sb.toString();
        int index = limitInvel.lastIndexOf(connector);
        limitInvel = limitInvel.substring(0, index);
        return limitInvel;
    }

    /**
     * 将半个阀值区间转化为公式:如
     * <pre>
     * 60) 转为 numberStr < 60
     * ≥88% 转为 numberStr >= 0.88
     * [60 转为 numberStr >= 60
     * <50% 转为 numberStr < 0.5
     * </pre>
     *
     * @param halfInterval 半个阀值区间,如:60)
     * @param numberStr 数值类型的字符串
     * @return
     */
    private static String getFormulaByHalfInterval(String halfInterval, String numberStr) {
        halfInterval = halfInterval.trim();
        // 包含无穷大则不需要公式
        if (halfInterval.contains("∞")) {
            return "1 == 1";
        }
        StringBuffer formula = new StringBuffer();
        String number = "";
        String opera = "";
        // 表示判断方向(如>)在前面 如:≥88%
        String regex = "^([<>≤≥\\[\\(]{1}(-?\\d+.?\\d*\\%?))$";
        if (halfInterval.matches(regex)) {
            opera = halfInterval.substring(0, 1);
            number = halfInterval.substring(1);
        } else {
            opera = halfInterval.substring(halfInterval.length() - 1);
            number = halfInterval.substring(0, halfInterval.length() - 1);
        }
        BigDecimal value = dealPercent(number);
        formula.append(numberStr).append(" ").append(opera).append(" ").append(value);
        String res = formula.toString();
        // 转化特定字符
        res = res.replace("[", ">=");
        res = res.replace("(", ">");
        res = res.replace("]", "<=");
        res = res.replace(")", "<");
        res = res.replace("≤", "<=");
        res = res.replace("≥", ">=");
        return res;
    }

    /**
     * 去除百分号,转为小数
     *
     * @param str 可能含百分号的数字
     * @return
     */
    private static BigDecimal dealPercent(String str) {
        BigDecimal bigDecimal = BigDecimal.ZERO;
        if (str.contains("%")) {
            str = str.substring(0, str.length() - 1);
            bigDecimal = new BigDecimal(str).divide(new BigDecimal(100));
        } else {
            bigDecimal = new BigDecimal(str);
        }
        return bigDecimal;
    }

}

测试类:

import org.junit.Test;

import javax.script.ScriptException;

/**
 * 区间工具测试类
 *
 * @author zql
 * @version 1.0
 * @createTime 2021-06-11 2:24:00
 * @modifyLog
 */
public class IntervalUtilTest {

    @Test
    public void isInTheInterval() throws ScriptException {
        System.out.println(IntervalUtil.isInTheInterval("40", "(0, 100]"));
        System.out.println(IntervalUtil.isInTheInterval("51", "<50"));
        System.out.println(IntervalUtil.isInTheInterval("8", "(-∞,8]"));
        System.out.println(IntervalUtil.isInTheInterval("0.9", "(10%,50%)U(80%,100%)"));
        System.out.println(IntervalUtil.isInTheInterval("88", "[60,100]"));
        System.out.println(IntervalUtil.isInTheInterval("0.79", "≥88%"));
        System.out.println(IntervalUtil.isInTheInterval("0.44", "<50%"));
    }
}

标签:halfInterval,String,param,区间,formula,工具,numberStr,append
来源: https://blog.csdn.net/mr_zql/article/details/117812440