All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.common.tool.utils.SectionUtil Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.tool.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import top.doudou.base.exception.CustomException;
import top.doudou.common.tool.dto.SectionDto;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description 区间判定的工具类
 * @Author 傻男人 <[email protected]>
 * @Date 2020-10-09 10:14
 * @Version V1.0
 */
@Slf4j
public class SectionUtil implements Serializable {

    /**
     * 比较大小 -1后者大0相等1前者大
     * @param pre
     * @param val
     * @return
     */
    public static int compareTo(BigDecimal pre, BigDecimal val) {
        if (null == pre || null == val ) {
            return 1;
        }
        return pre.compareTo(val);
    }

    /**
     * 判断两个区间是否有重合值
     * @param source
     * @param record
     * @return  false无重合   true有重合
     */
    public static boolean hasRepeat(SectionDto source, SectionDto record) {
        BigDecimal min = record.getMin();
        BigDecimal max = record.getMax();
        boolean hasContainMin = record.isContainMin();
        boolean hasContainMax = record.isContainMax();
        boolean front = false;
        boolean behind = false;
        int frontResult = compareTo(source.getMax(), min);
        if ((frontResult == 0 && source.isContainMax() && hasContainMin) || (frontResult > 0)) {
            front = true;
        }
        int behindResult = compareTo(max, source.getMin());
        if ((behindResult == 0 && source.isContainMin() && hasContainMax) || behindResult > 0) {
            behind = true;
        }
        return front && behind;
    }

    /**
     * 判断区间在list中是否有重合
     * @param source
     * @param record
     * @return  false无重合   true有重合
     */
    public static boolean hasRepeat(SectionDto source, List record) {
        if(CollectionUtils.isEmpty(record)){
            return false;
        }
        for(SectionDto item : record){
            if(hasRepeat(source,item)){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断当前区间是否包含val,并返回区间所在的list中对应的具体值
     * @param val
     * @param record  区间list
     * @param list  区间对应实体的list
     * @return
     */
    public static T getCompareEntity(BigDecimal val, List record, List list) {
        if(CollectionUtils.isEmpty(record) || CollectionUtils.isEmpty(list)){
            throw new CustomException("区间的列表与区间对应实体列表不能为空");
        }
        if(record.size() != list.size()){
            throw new CustomException("区间的列表与区间对应实体列表长度不相等");
        }
        for (int i = 0; i < record.size(); i++) {
            SectionDto item = record.get(i);
            int compare = compareTo(item.getMin(), val);
            if (compare == 1) {
                continue;
            }
            if (compare == 0 && item.getMin().equals(0)) {
                log.info("比较的值为:" + val + "      item:" + item);
                return list.get(i);
            }
            if (compare == -1) {
                int after = compareTo(val, item.getMax());
                if (after == 0 && item.getMax().equals(0)) {
                    log.info("比较的值为:" + val + "      item:" + item);
                    return list.get(i);
                }
                if (after == -1) {
                    log.info("比较的值为:" + val + "      item:" + item);
                    return list.get(i);
                }
            }
        }
        return null;
    }

    /**
     * @description:   比较集合中区间是否有重叠
     * @param list1 待比较集合1
     * @param list2 待比较集合2
     * @return false无重合   true有重合
     */
    public static boolean hasRepeat(List list1, List list2) {
        for (SectionDto item1 : list1) {
            for (SectionDto item2 : list2) {
                if (hasRepeat(item1,item2)) {
                    return true;
                }
            }
        }
        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy