java判斷多個區間是否有重合


前言

  先講一下大致場景,項目中一個問卷可以配置多個區間,要求保存信息時,區間不能重復,如下所示,當選擇大於或小於等於時只有后邊文本框有值;

范圍符號:1-大於/2-小於/3-等於/4-不等於/5-大於等於/6-小於等於/7-全閉區間/8-左閉右開區間/9-左開右閉區間/10-全開區間

如:!=4 表示(負無窮,4)&&(4,正無窮)

代碼信息

工具類代碼如下:

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

/**
 * @description: 區間工具類
 * @author: wyj
 * @time: 2020/3/1 15:05
 */

public class SectionUtil {
    //最小值
    private String min_entity;
    //最大值
    private String max_entity;
    //左側括號狀態:false -開區間  true-- 閉區間
    private boolean left_sate = false;
    //右側括號狀態:false -開區間  true-- 閉區間
    private boolean right_sate = false;
    private SectionUtil() {

    }

    public SectionUtil(String min_entity, String max_entity, boolean left_sate, boolean right_sate) {
        this.min_entity = min_entity;
        this.max_entity = max_entity;
        this.left_sate = left_sate;
        this.right_sate = right_sate;
    }

    public String getMin_entity() {
        return min_entity;
    }

    public String getMax_entity() {
        return max_entity;
    }

    public boolean isLeft_sate() {
        return left_sate;
    }

    public boolean isRight_sate() {
        return right_sate;
    }

    /**
     * @description: 創建負區間((負無窮,X])
     * @param value 區間最大值
     * @param right_sate 區間開閉狀態
     * @Date: 2020/3/2 14:37
     */
    public static SectionUtil creatFu(String value, boolean right_sate) {

        return new SectionUtil("", value, false, right_sate);
    }
    /**
     * @description: 創建正區間[X,正無窮))
     * @param value 區間最小值
     * @param left_sate 區間開閉狀態
     * @Date: 2020/3/2 14:37
     */
    public static SectionUtil creatZheng(String value, boolean left_sate) {

        return new SectionUtil(value, "", left_sate, false);
    }
    /**
     * @description: 創建閉合區間([X,Y])
     * @param min   區間最小值
     * @param max   區間最大值
     * @param left_sate 區間左側開閉狀態
     * @param right_sate 區間右側開閉狀態
     * @return
     * @Date: 2020/3/2 14:41
     */
    public static SectionUtil creat(String min, boolean left_sate, String max, boolean right_sate) {

        return new SectionUtil(min, max, left_sate, right_sate);
    }
    /**
     * @description:  將實體類轉換成區間集合
     * @param record  待轉換的實體類
     * @return 轉換后的區間集合類(不等於時轉換后為2個區間,所以采用集合)
     * @Date: 2020/3/2 14:19
     */
    public static List<SectionUtil> getSections(ReqRespResearchProductQuestionnaireItem record) {
        List<SectionUtil> list = new ArrayList<>();
        String record_max = record.getMaxValue();
        String record_min = record.getMinValue();
        switch (record.getSymbol()) {
            case 1:
                list.add(creatZheng(record_max, false));
                break;
            case 2:
                list.add(creatFu(record_max, false));
                break;
            case 3:
                list.add(creat(record_max, true, record_max, true));
                break;
            case 4:
                list.add(creatFu(record_max, false));
                list.add(creatZheng(record_max, false));
                break;
            case 5:
                list.add(creatZheng(record_max, true));
                break;
            case 6:
                list.add(creatFu(record_max, true));
                break;
            case 7:
                list.add(creat(record_min, true, record_max, true));
                break;
            case 8:
                list.add(creat(record_min, true, record_max, false));
                break;
            case 9:
                list.add(creat(record_min, false, record_max, true));
                break;
            case 10:
                list.add(creat(record_min, false, record_max, false));
                break;
        }
        return list;
    }



    public int compareTo(String first_value, String second_value) {
        //first_value為空表示為正無窮,second_value為空表示為負無窮
        if (isBlank(first_value) || isBlank(second_value)) {
            return 1;
        }
        return compareToValue(first_value,second_value);
    }
    //判斷字符串是否為空
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param record 判斷區間是否有重合
     * @return true-有重合  false -無重合
     * @description: 判斷當前區間是否和指定區間重合
     * @Date: 2020/3/2 10:20
     */
    public boolean isChonghe(SectionUtil record) {
        String min_entity = record.getMin_entity();
        String max_entity = record.getMax_entity();
        boolean left_sate = record.isLeft_sate();
        boolean right_sate = record.isRight_sate();
        boolean left_isok = false;
        boolean right_isok = false;
        //重合條件,第一個區間最大值大於第二個區間最小值並且第一個區間的最小值小於第二個區間的最大值
        //注意傳值順序,第一個值為第一個區間的最大值(此處不能反)
        int first_result = compareTo(this.max_entity, min_entity);
        if ((first_result == 0 && this.right_sate && left_sate) || (first_result > 0)) {
            left_isok = true;
        }
        //注意傳值順序,第一個值為第二個區間的最大值(此處不能反)
        int second_result = compareTo(max_entity, this.min_entity);
        //此處本應該是second_result<0,但由於上一步參數傳遞時時反正傳遞,故此此處為second_result>0
        if ((second_result == 0 && this.left_sate && right_sate) || second_result > 0) {
            right_isok = true;
        }
        return left_isok && right_isok;
    }
    /**
     * @description:   比較集合中區間是否有重疊
     * @param list1 待比較集合1
     * @param list2 待比較集合2
     * @return
     * @Date: 2020/3/2 11:49
     */
    public static boolean isChonghe(List<SectionUtil> list1, List<SectionUtil> list2) {
        boolean chonghed = false;
        for (SectionUtil item1 : list1) {
            for (SectionUtil item2 : list2) {
                chonghed = item1.isChonghe(item2);
                if (chonghed) {
                    return true;
                }
            }
        }
        return chonghed;
    }
    //比較大小
    public static int compareToValue(String value1, String value2) {
        BigDecimal b1 = new BigDecimal(value1);
        BigDecimal b2 = new BigDecimal(value2);
        return b1.compareTo(b2);
    }
}
View Code

實體類代碼:

import java.io.Serializable;

public class ReqRespResearchProductQuestionnaireItem implements Serializable {
    private static final long serialVersionUID = -2850886454687095222L;
    private String minValue;
    private String maxValue;
    //"范圍符號:1-大於/2-小於/3-等於/4-不等於/5-大於等於/6-小於等於/7-全閉區間/8-左閉右開區間/9-左開右閉區間/10-全開區間"
    private Byte symbol;

    public ReqRespResearchProductQuestionnaireItem( String minValue, String maxValue, Byte symbol) {
        this.minValue = minValue;
        this.maxValue = maxValue;
        this.symbol = symbol;
    }

    public String getMinValue() {
        return minValue;
    }

    public void setMinValue(String minValue) {
        this.minValue = minValue;
    }

    public String getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(String maxValue) {
        this.maxValue = maxValue;
    }

    public Byte getSymbol() {
        return symbol;
    }

    public void setSymbol(Byte symbol) {
        this.symbol = symbol;
    }
}
View Code

使用方法:

 

 /**
     * @description:   判斷集合中區間是否重疊 
     * @param list  待判斷集合
     * @return  fasle-無重疊 true-有重疊
     * @Date: 2020/3/3 14:51
     */    
    private static boolean compareSection(List<ReqRespResearchProductQuestionnaireItem> list) {
        for (int i = 0; i < list.size(); i++) {
            ReqRespResearchProductQuestionnaireItem record = list.get(i);
            for (int j = i + 1; j < list.size(); j++) {
                ReqRespResearchProductQuestionnaireItem item = list.get(j);
                //判斷區間是否有交叉
                List<SectionUtil> records = SectionUtil.getSections(record);
                List<SectionUtil> items = SectionUtil.getSections(item);
                boolean chonghe = SectionUtil.isChonghe(records, items);
                if (chonghe) {
                    return true;
                }
            }
        }
        return false;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM