SectionUtil.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package com.template.common.jh;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6. * @description: 区间工具类
  7. * @author: wyj
  8. * @time: 2020/3/1 15:05
  9. */
  10. public class SectionUtil {
  11. //最小值
  12. private String min_entity;
  13. //最大值
  14. private String max_entity;
  15. //左侧括号状态:false -开区间 true-- 闭区间
  16. private boolean left_sate = false;
  17. //右侧括号状态:false -开区间 true-- 闭区间
  18. private boolean right_sate = false;
  19. private SectionUtil() {
  20. }
  21. public SectionUtil(String min_entity, String max_entity, boolean left_sate, boolean right_sate) {
  22. this.min_entity = min_entity;
  23. this.max_entity = max_entity;
  24. this.left_sate = left_sate;
  25. this.right_sate = right_sate;
  26. }
  27. public String getMin_entity() {
  28. return min_entity;
  29. }
  30. public String getMax_entity() {
  31. return max_entity;
  32. }
  33. public boolean isLeft_sate() {
  34. return left_sate;
  35. }
  36. public boolean isRight_sate() {
  37. return right_sate;
  38. }
  39. /**
  40. * @description: 创建负区间((负无穷,X])
  41. * @param value 区间最大值
  42. * @param right_sate 区间开闭状态
  43. * @Date: 2020/3/2 14:37
  44. */
  45. public static SectionUtil creatFu(String value, boolean right_sate) {
  46. return new SectionUtil("", value, false, right_sate);
  47. }
  48. /**
  49. * @description: 创建正区间[X,正无穷))
  50. * @param value 区间最小值
  51. * @param left_sate 区间开闭状态
  52. * @Date: 2020/3/2 14:37
  53. */
  54. public static SectionUtil creatZheng(String value, boolean left_sate) {
  55. return new SectionUtil(value, "", left_sate, false);
  56. }
  57. /**
  58. * @description: 创建闭合区间([X,Y])
  59. * @param min 区间最小值
  60. * @param max 区间最大值
  61. * @param left_sate 区间左侧开闭状态
  62. * @param right_sate 区间右侧开闭状态
  63. * @return
  64. * @Date: 2020/3/2 14:41
  65. */
  66. public static SectionUtil creat(String min, boolean left_sate, String max, boolean right_sate) {
  67. return new SectionUtil(min, max, left_sate, right_sate);
  68. }
  69. /**
  70. * @description: 将实体类转换成区间集合
  71. * @param record 待转换的实体类
  72. * @return 转换后的区间集合类(不等于时转换后为2个区间,所以采用集合)
  73. * @Date: 2020/3/2 14:19
  74. */
  75. public static List<SectionUtil> getSections(ReqRespResearchProductQuestionnaireItem record) {
  76. List<SectionUtil> list = new ArrayList<>();
  77. String record_max = record.getMaxValue();
  78. String record_min = record.getMinValue();
  79. switch (record.getSymbol()) {
  80. case 1:
  81. list.add(creatZheng(record_max, false));
  82. break;
  83. case 2:
  84. list.add(creatFu(record_max, false));
  85. break;
  86. case 3:
  87. list.add(creat(record_max, true, record_max, true));
  88. break;
  89. case 4:
  90. list.add(creatFu(record_max, false));
  91. list.add(creatZheng(record_max, false));
  92. break;
  93. case 5:
  94. list.add(creatZheng(record_max, true));
  95. break;
  96. case 6:
  97. list.add(creatFu(record_max, true));
  98. break;
  99. case 7:
  100. list.add(creat(record_min, true, record_max, true));
  101. break;
  102. case 8:
  103. list.add(creat(record_min, true, record_max, false));
  104. break;
  105. case 9:
  106. list.add(creat(record_min, false, record_max, true));
  107. break;
  108. case 10:
  109. list.add(creat(record_min, false, record_max, false));
  110. break;
  111. }
  112. return list;
  113. }
  114. public int compareTo(String first_value, String second_value) {
  115. //first_value为空表示为正无穷,second_value为空表示为负无穷
  116. if (isBlank(first_value) || isBlank(second_value)) {
  117. return 1;
  118. }
  119. return compareToValue(first_value,second_value);
  120. }
  121. //判断字符串是否为空
  122. public static boolean isBlank(String str) {
  123. int strLen;
  124. if (str == null || (strLen = str.length()) == 0) {
  125. return true;
  126. }
  127. for (int i = 0; i < strLen; i++) {
  128. if ((Character.isWhitespace(str.charAt(i)) == false)) {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * @param record 判断区间是否有重合
  136. * @return true-有重合 false -无重合
  137. * @description: 判断当前区间是否和指定区间重合
  138. * @Date: 2020/3/2 10:20
  139. */
  140. public boolean isChonghe(SectionUtil record) {
  141. String min_entity = record.getMin_entity();
  142. String max_entity = record.getMax_entity();
  143. boolean left_sate = record.isLeft_sate();
  144. boolean right_sate = record.isRight_sate();
  145. boolean left_isok = false;
  146. boolean right_isok = false;
  147. //重合条件,第一个区间最大值大于第二个区间最小值并且第一个区间的最小值小于第二个区间的最大值
  148. //注意传值顺序,第一个值为第一个区间的最大值(此处不能反)
  149. int first_result = compareTo(this.max_entity, min_entity);
  150. if ((first_result == 0 && this.right_sate && left_sate) || (first_result > 0)) {
  151. left_isok = true;
  152. }
  153. //注意传值顺序,第一个值为第二个区间的最大值(此处不能反)
  154. int second_result = compareTo(max_entity, this.min_entity);
  155. //此处本应该是second_result<0,但由于上一步参数传递时时反正传递,故此此处为second_result>0
  156. if ((second_result == 0 && this.left_sate && right_sate) || second_result > 0) {
  157. right_isok = true;
  158. }
  159. return left_isok && right_isok;
  160. }
  161. /**
  162. * @description: 比较集合中区间是否有重叠
  163. * @param list1 待比较集合1
  164. * @param list2 待比较集合2
  165. * @return
  166. * @Date: 2020/3/2 11:49
  167. */
  168. public static boolean isChonghe(List<SectionUtil> list1, List<SectionUtil> list2) {
  169. boolean chonghed = false;
  170. for (SectionUtil item1 : list1) {
  171. for (SectionUtil item2 : list2) {
  172. chonghed = item1.isChonghe(item2);
  173. if (chonghed) {
  174. return true;
  175. }
  176. }
  177. }
  178. return chonghed;
  179. }
  180. //比较大小
  181. public static int compareToValue(String value1, String value2) {
  182. BigDecimal b1 = new BigDecimal(value1);
  183. BigDecimal b2 = new BigDecimal(value2);
  184. return b1.compareTo(b2);
  185. }
  186. /**
  187. * @description: 判断集合中区间是否重叠
  188. * @param list 待判断集合
  189. * @return fasle-无重叠 true-有重叠
  190. * @Date: 2020/3/3 14:51
  191. */
  192. public static boolean compareSection(List<ReqRespResearchProductQuestionnaireItem> list) {
  193. for (int i = 0; i < list.size(); i++) {
  194. ReqRespResearchProductQuestionnaireItem record = list.get(i);
  195. for (int j = i + 1; j < list.size(); j++) {
  196. ReqRespResearchProductQuestionnaireItem item = list.get(j);
  197. //判断区间是否有交叉
  198. List<SectionUtil> records = SectionUtil.getSections(record);
  199. List<SectionUtil> items = SectionUtil.getSections(item);
  200. boolean chonghe = SectionUtil.isChonghe(records, items);
  201. if (chonghe) {
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. }
  208. public static void main(String[] args) {
  209. List<ReqRespResearchProductQuestionnaireItem> all = new ArrayList<>();
  210. ReqRespResearchProductQuestionnaireItem re1 = new ReqRespResearchProductQuestionnaireItem("110","120", (byte) 7);
  211. ReqRespResearchProductQuestionnaireItem re2 = new ReqRespResearchProductQuestionnaireItem("90","112", (byte) 7);
  212. ReqRespResearchProductQuestionnaireItem re3 = new ReqRespResearchProductQuestionnaireItem("80","89", (byte) 7);
  213. ReqRespResearchProductQuestionnaireItem re4 = new ReqRespResearchProductQuestionnaireItem("60","79", (byte) 7);
  214. ReqRespResearchProductQuestionnaireItem re5 = new ReqRespResearchProductQuestionnaireItem("0","59", (byte) 7);
  215. all.add(re1);
  216. all.add(re2);
  217. all.add(re3);
  218. all.add(re4);
  219. all.add(re5);
  220. logger.info(compareSection(all));
  221. }
  222. }