背景
在项目中需要存储区间值数据,并进行重复区间判断。终于找到一种有用的方案,特此记录一下,以防不备之需。
取值范围符号包括:
7-全闭区间
8-左闭右开区间
9-左开右闭区间
10-全开区间
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;
}
private String getMin_entity() {
return min_entity;
}
private String getMax_entity() {
return max_entity;
}
private boolean isLeft_sate() {
return left_sate;
}
private boolean isRight_sate() {
return right_sate;
}
/**
* @description: 创建负区间((负无穷,X])
* @param value 区间最大值
* @param right_sate 区间开闭状态
*/
private static SectionUtil creatFu(String value, boolean right_sate) {
return new SectionUtil("", value, false, right_sate);
}
/**
* @description: 创建正区间[X,正无穷))
* @param value 区间最小值
* @param left_sate 区间开闭状态
*/
private 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
*/
private static SectionUtil creat(String min, boolean left_sate, String max, boolean right_sate) {
return new SectionUtil(min, max, left_sate, right_sate);
}
private 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);
}
//判断字符串是否为空
private 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: 判断当前区间是否和指定区间重合
*/
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;
}
//比较大小
private static int compareToValue(String value1, String value2) {
BigDecimal b1 = new BigDecimal(value1);
BigDecimal b2 = new BigDecimal(value2);
return b1.compareTo(b2);
}
}
总结
通过测试,上述代码可以正确执行。
实际使用时,需要根据自己的需要对实体类的加工。灵活运用,不要硬套上面代码。
参考自:
[1]:https://blog.csdn.net/Li_Tong0607/article/details/123664740
文章介绍了一种在项目中存储区间值数据的方法,并提供了区间重复判断的实现。SectionUtil类包含了区间边界值和状态,支持全闭、左闭右开、左开右闭、全开等四种区间类型,并提供创建和比较区间的方法,用于判断两个区间是否重合。

7878

被折叠的 条评论
为什么被折叠?



