区间重合判断 java_java判断多个区间是否有重合

importjava.math.BigDecimal;importjava.util.ArrayList;importjava.util.List;/*** @description: 区间工具类

*@author: wyj

* @time: 2020/3/1 15:05*/

public classSectionUtil {//最小值

privateString min_entity;//最大值

privateString max_entity;//左侧括号状态:false -开区间 true-- 闭区间

private boolean left_sate = false;//右侧括号状态:false -开区间 true-- 闭区间

private boolean right_sate = false;privateSectionUtil() {

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

}publicString getMin_entity() {returnmin_entity;

}publicString getMax_entity() {returnmax_entity;

}public booleanisLeft_sate() {returnleft_sate;

}public booleanisRight_sate() {returnright_sate;

}/*** @description: 创建负区间((负无穷,X])

*@paramvalue 区间最大值

*@paramright_sate 区间开闭状态

* @Date: 2020/3/2 14:37*/

public static SectionUtil creatFu(String value, booleanright_sate) {return new SectionUtil("", value, false, right_sate);

}/*** @description: 创建正区间[X,正无穷))

*@paramvalue 区间最小值

*@paramleft_sate 区间开闭状态

* @Date: 2020/3/2 14:37*/

public static SectionUtil creatZheng(String value, booleanleft_sate) {return new SectionUtil(value, "", left_sate, false);

}/*** @description: 创建闭合区间([X,Y])

*@parammin 区间最小值

*@parammax 区间最大值

*@paramleft_sate 区间左侧开闭状态

*@paramright_sate 区间右侧开闭状态

*@return* @Date: 2020/3/2 14:41*/

public static SectionUtil creat(String min, boolean left_sate, String max, booleanright_sate) {return newSectionUtil(min, max, left_sate, right_sate);

}/*** @description: 将实体类转换成区间集合

*@paramrecord 待转换的实体类

*@return转换后的区间集合类(不等于时转换后为2个区间,所以采用集合)

* @Date: 2020/3/2 14:19*/

public static ListgetSections(ReqRespResearchProductQuestionnaireItem record) {

List 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;

}returnlist;

}public intcompareTo(String first_value, String second_value) {//first_value为空表示为正无穷,second_value为空表示为负无穷

if (isBlank(first_value) ||isBlank(second_value)) {return 1;

}returncompareToValue(first_value,second_value);

}//判断字符串是否为空

public static booleanisBlank(String str) {intstrLen;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;

}/***@paramrecord 判断区间是否有重合

*@returntrue-有重合 false -无重合

* @description: 判断当前区间是否和指定区间重合

* @Date: 2020/3/2 10:20*/

public booleanisChonghe(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: 比较集合中区间是否有重叠

*@paramlist1 待比较集合1

*@paramlist2 待比较集合2

*@return* @Date: 2020/3/2 11:49*/

public static boolean isChonghe(List list1, Listlist2) {boolean chonghed = false;for(SectionUtil item1 : list1) {for(SectionUtil item2 : list2) {

chonghed=item1.isChonghe(item2);if(chonghed) {return true;

}

}

}returnchonghed;

}//比较大小

public static intcompareToValue(String value1, String value2) {

BigDecimal b1= newBigDecimal(value1);

BigDecimal b2= newBigDecimal(value2);returnb1.compareTo(b2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值