BaseUtils.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package org.springblade.common.utils;
  2. import cn.hutool.core.lang.func.Func;
  3. import com.alibaba.cloud.commons.lang.StringUtils;
  4. import org.springblade.common.constant.RegexConstant;
  5. import java.io.*;
  6. import java.lang.annotation.Annotation;
  7. import java.math.BigDecimal;
  8. import java.net.JarURLConnection;
  9. import java.net.URL;
  10. import java.util.*;
  11. import java.util.jar.JarEntry;
  12. import java.util.jar.JarFile;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. import java.util.stream.Collectors;
  16. import java.util.stream.IntStream;
  17. import static java.math.BigDecimal.ROUND_CEILING;
  18. import static java.math.BigDecimal.ROUND_HALF_UP;
  19. /**
  20. * @author yangyj
  21. * @Date 2022/7/8 11:08
  22. * @description 基础工具类
  23. */
  24. public class BaseUtils {
  25. public static Pattern KM = Pattern.compile(RegexConstant.KM_REG);
  26. public static double k2d(Object k) {
  27. Matcher mt = KM.matcher(k.toString());
  28. if (mt.find()) {
  29. return Double.parseDouble(mt.group(1)) * 1000 + Double.parseDouble(mt.group(2));
  30. }
  31. return -1;
  32. }
  33. public static Double milestone(String s){
  34. Pattern pattern=Pattern.compile("(?i)K(\\d+)\\+([\\d.]+)");
  35. Matcher matcher = pattern.matcher(s);
  36. if(matcher.find()){
  37. return Double.parseDouble(matcher.group(1))*1000+ Double.parseDouble(matcher.group(2));
  38. }
  39. return null;
  40. }
  41. /**
  42. * @return java.util.Map<java.lang.String, java.lang.String>
  43. * @Description 将key1(val1)key2(val2)key3(val3)...这种格式的参数转换为Map
  44. * @Param [str]
  45. * @Author yangyj
  46. * @Date 2022.08.12 10:39
  47. **/
  48. public static Map<String, String> string2Map(String str) {
  49. Map<String, String> result = new HashMap<>(15);
  50. if (StringUtils.isNotEmpty(str)) {
  51. String[] args = str.split("\\)");
  52. for (String a : args) {
  53. if (StringUtils.isNotEmpty(a)) {
  54. String[] kv = a.split("\\(");
  55. result.put(kv[0], kv[1]);
  56. }
  57. }
  58. }
  59. return result;
  60. }
  61. /**
  62. * @return java.lang.Boolean
  63. * @Description 基础数据类型批量非空判断
  64. * @Param [args]
  65. * @Author yangyj
  66. * @Date 2022.08.26 11:14
  67. **/
  68. public static Boolean isNotNull(Object... args) {
  69. if (args != null) {
  70. for (Object obj : args) {
  71. if (obj == null || "".equals(obj)) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. /**
  80. * @Description 判断对象是否为数值
  81. * @Param [value]
  82. * @Author yangyj
  83. * @Date 2023.01.17 13:48
  84. **/
  85. static final String NUM_REG = "^[+-]?\\d+(\\.\\d+)?$";
  86. public static boolean isNumber(Object value) {
  87. if ((value == null) ) {
  88. return false;
  89. }
  90. String stringValue = value.toString().trim();
  91. if (stringValue.isEmpty()) {
  92. return false;
  93. }
  94. if (value instanceof Number) {
  95. return true;
  96. }
  97. return Pattern.matches(NUM_REG,stringValue);
  98. }
  99. /**
  100. * @Description 根据指定大小对LIST进行切分
  101. * @Param [list, chunkSize:每一段长度]
  102. * @return java.util.List<java.util.List<T>>
  103. * @Author yangyj
  104. * @Date 2023.07.03 13:46
  105. **/
  106. public static <T> List<List<T>> splitList(List<T> list, int chunkSize) {
  107. return IntStream.range(0, (list.size() + chunkSize - 1) / chunkSize)
  108. .mapToObj(i -> list.subList(i * chunkSize, Math.min((i + 1) * chunkSize, list.size())))
  109. .collect(Collectors.toList());
  110. }
  111. /**
  112. * @Description 深度拷贝
  113. * @Param [originalList]
  114. * @return java.util.List<T>
  115. * @Author yangyj
  116. * @Date 2023.04.28 14:18
  117. **/
  118. public static <T extends Serializable> List<T> copyList(List<T> originalList) {
  119. try {
  120. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  121. ObjectOutputStream oos = new ObjectOutputStream(baos);
  122. oos.writeObject(originalList);
  123. oos.close();
  124. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  125. ObjectInputStream ois = new ObjectInputStream(bais);
  126. @SuppressWarnings("unchecked")
  127. List<T> copiedList = (List<T>) ois.readObject();
  128. ois.close();
  129. return copiedList;
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. return null;
  134. }
  135. public static String handleNull(Object obj) {
  136. if (null == obj) {
  137. return "";
  138. } else {
  139. return obj.toString().trim();
  140. }
  141. }
  142. public static boolean isNotEmpty(Object value) {
  143. if(value!=null){
  144. if(value instanceof List&&((List<?>) value).size()>0 ){
  145. return true;
  146. }else if(value instanceof Map&&((Map<?, ?>) value).size()>0){
  147. return true;
  148. }else {
  149. return value.toString().trim().length() > 0;
  150. }
  151. }
  152. return false;
  153. }
  154. public static boolean isEmpty(Object value){
  155. return !isNotEmpty(value);
  156. }
  157. public static Double[] scopeParse(Object dev, Object design, Object xN) {
  158. if (isNotEmpty(dev)) {
  159. Double[] result = new Double[2];
  160. double designD = Double.parseDouble(design.toString());
  161. double xND = Double.parseDouble(xN.toString());
  162. String devStr = dev.toString();
  163. devStr = devStr.replaceAll("[\\[\\]]+", "");
  164. double min = 0;
  165. double max = 0;
  166. devStr = devStr.replaceAll("\\s+", "");
  167. if (devStr.contains("≤") || devStr.contains("<=") || devStr.contains("<")||devStr.contains("≦")) {
  168. devStr = devStr.replace("≤", "").replace("<=", "").replace("≦","");
  169. max = designD + Double.parseDouble(devStr) * xND;
  170. } else if (devStr.contains("≥") || devStr.contains(">=") || devStr.contains(">")) {
  171. devStr = devStr.replace("≥", "").replace(">=", "");
  172. min = designD + Double.parseDouble(devStr) * xND;
  173. max = Double.MAX_VALUE;
  174. } else if (devStr.contains(",") || devStr.contains(",")) {
  175. String[] arr = devStr.split("[,,]");
  176. min = designD + Double.parseDouble(arr[0]) * xND;
  177. max = designD + Double.parseDouble(arr[1]) * xND;
  178. } else if (devStr.contains("%")) {
  179. devStr = devStr.replace("%", "");
  180. double devD = Math.abs(Double.parseDouble(devStr) * designD / 100);
  181. min = designD - devD;
  182. max = designD + devD;
  183. } else if (devStr.contains("±")) {
  184. devStr = devStr.replace("±", "");
  185. double devD = Math.abs(Double.parseDouble(devStr) * xND);
  186. min = designD - devD;
  187. max = designD + devD;
  188. }
  189. if(min>max){
  190. double tmp=max;
  191. max=min;
  192. min=tmp;
  193. }
  194. result[0] = min;
  195. result[1] = max;
  196. return result;
  197. }
  198. return null;
  199. }
  200. public static Integer handleObj2Integer(Object obj) {
  201. if (null == obj) {
  202. return 0;
  203. } else {
  204. double value;
  205. try {
  206. value = Double.parseDouble(obj.toString());
  207. } catch (Exception ex) {
  208. value = 0;
  209. }
  210. return (int) value;
  211. }
  212. }
  213. /**
  214. * @return java.util.List<java.lang.Object> 不能包含0
  215. * @Description specifiedRangeList
  216. * @Param [hz:频率, design:设计值, dev:偏差范围, xN 偏差范围单位和设计值单位的比值,例如毫米:厘米=0.1, scale:保存小数位, passRate:合格率[0,1]]
  217. * @Author yangyj
  218. * @Date 2022.03.31 09:16
  219. **/
  220. public static List<Object> rangeList(Object hz, Object design, Object dev, Object xN, Object scale, Object passRate) {
  221. List<Object> result = new ArrayList<>();
  222. if (isNotNull(design, dev, hz)) {
  223. if (isEmpty(scale)) {
  224. scale = 0;
  225. }
  226. if (isEmpty(passRate)) {
  227. passRate = 1;
  228. }
  229. if (isEmpty(xN)) {
  230. xN = 1;
  231. }
  232. Double[] range = scopeParse(dev, design, xN);
  233. int scaleI = Integer.parseInt(scale.toString());
  234. int min = 0, max = 0;
  235. assert range != null;
  236. if (range.length > 0) {
  237. min = (int) (range[0] * Math.pow(10, scaleI));
  238. max = (int) (range[1] * Math.pow(10, scaleI));
  239. }
  240. Random rd = new Random();
  241. int hzi = new BigDecimal(hz.toString()).multiply(new BigDecimal(passRate.toString())).setScale(0, ROUND_CEILING).intValue();
  242. for (int i = 0; i < hzi; i++) {
  243. BigDecimal tb = new BigDecimal(rd.nextInt(max - min + 1) + min).divide(BigDecimal.valueOf(Math.pow(10, scaleI)), scaleI, ROUND_HALF_UP);
  244. if(tb.equals(BigDecimal.ZERO)){
  245. i--;
  246. }else{
  247. if (scaleI > 0) {
  248. result.add(tb.doubleValue());
  249. } else {
  250. result.add(tb.intValue());
  251. }
  252. }
  253. }
  254. int total = handleObj2Integer(hz);
  255. if (total - hzi > 0) {
  256. for (int k = 0; k < total - hzi; k++) {
  257. BigDecimal tb;
  258. if (rd.nextBoolean()) {
  259. tb = new BigDecimal(rd.nextInt(((max - min) / 2)) + max + 1).divide(BigDecimal.valueOf(Math.pow(10, scaleI)), scaleI, ROUND_HALF_UP);
  260. } else {
  261. tb = new BigDecimal(min - 1 - rd.nextInt(((max - min) / 2))).divide(BigDecimal.valueOf(Math.pow(10, scaleI)), scaleI, ROUND_HALF_UP);
  262. }
  263. if(tb.equals(BigDecimal.ZERO)){
  264. k--;
  265. }else {
  266. if (scaleI > 0) {
  267. result.add(tb.doubleValue());
  268. } else {
  269. result.add(tb.intValue());
  270. }
  271. }
  272. }
  273. if (result.size()>0) {
  274. Collections.shuffle(result);
  275. }
  276. }
  277. }
  278. return result;
  279. }
  280. }