BaseUtils.java 12 KB

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