工作纪实33-给定数据区间对应的指标类型,根据当前值命中数据区间

背景: 给定一个数值value和数据连续区间[0,10,100],判断对应值是否达标

思路:左侧右侧各加入一个值,循环判断数据区间

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;

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

/**
 * @author liulei44
 * @date 2023/5/9 3:17 PM
 */
@Getter
@AllArgsConstructor
public enum IndicateRiskLevelV2Enum {

    EXCELLENT(0, "优秀"),
    NORMAL(1, "达标"),
    UNDER(2, "不达标"),
    ;

    private final int level;
    private final String describe;

    /**
     * 是否有风险
     */
    public boolean risk() {
        return this.level < 2;
    }

    /**
     * 根据value适配枚举
     */
    public static IndicateRiskLevelV2Enum getByLevel(Integer value) {
        for (IndicateRiskLevelV2Enum typeEnum : IndicateRiskLevelV2Enum.values()) {
            if (Objects.equals(typeEnum.level, value)) {
                return typeEnum;
            }
        }
        return null;
    }

    /**
     * 风险分析
     * 二分法
     *
     * @param value      当前值
     * @param riskRanges 风险类型的各个区间
     * @return 风险值
     */
    public static IndicateRiskLevelV2Enum analysis(BigDecimal value, List<BigDecimal> riskRanges) {
        riskRanges.add(0, BigDecimal.valueOf(-Integer.MAX_VALUE));
        riskRanges.add(BigDecimal.valueOf(Integer.MAX_VALUE));
        for (int i = 0; i < riskRanges.size(); i++) {
            if (i == riskRanges.size() - 1) {
                return UNDER;
            }
            BigDecimal left = riskRanges.get(i);
            BigDecimal right = riskRanges.get(i + 1);
            if (value.compareTo(left) > 0 && value.compareTo(right) <= 0) {
                return shotByIndex(i - 1);
            }
        }
        throw new RuntimeException("未配置的指标风险区间");
    }

    /**
     * 区间命中对应的枚举
     */
    private static IndicateRiskLevelV2Enum shotByIndex(int index) {
        switch (index) {
            case 1:
                return IndicateRiskLevelV2Enum.NORMAL;
            case 2:
                return IndicateRiskLevelV2Enum.EXCELLENT;
            default:
                return IndicateRiskLevelV2Enum.UNDER;
        }
    }

    public static void main(String[] args) {
        List<BigDecimal> range = Lists.newArrayList(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.valueOf(20));
        System.out.println(analysis(BigDecimal.valueOf(-1), range));
        range = Lists.newArrayList(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.valueOf(20));
        System.out.println(analysis(BigDecimal.valueOf(10), range));
        range = Lists.newArrayList(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.valueOf(20));
        System.out.println(analysis(BigDecimal.valueOf(15), range));
        range = Lists.newArrayList(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.valueOf(20));
        System.out.println(analysis(BigDecimal.valueOf(20), range));
        range = Lists.newArrayList(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.valueOf(20));
        System.out.println(analysis(BigDecimal.valueOf(30), range));
    }
    ```

你可能感兴趣的:(工作纪实,java,maven)