package org.springblade.common.utils; import cn.hutool.core.lang.func.Func; import com.alibaba.cloud.commons.lang.StringUtils; import org.springblade.common.constant.RegexConstant; import java.io.*; import java.lang.annotation.Annotation; import java.math.BigDecimal; import java.math.BigInteger; import java.net.JarURLConnection; import java.net.URL; import java.security.MessageDigest; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; import static java.math.BigDecimal.ROUND_CEILING; import static java.math.BigDecimal.ROUND_HALF_UP; /** * @author yangyj * @Date 2022/7/8 11:08 * @description 基础工具类 */ public class BaseUtils { public static Pattern KM = Pattern.compile(RegexConstant.KM_REG); public static double k2d(Object k) { Matcher mt = KM.matcher(k.toString()); if (mt.find()) { return Double.parseDouble(mt.group(1)) * 1000 + Double.parseDouble(mt.group(2)); } return -1; } public static Double milestone(String s){ Pattern pattern=Pattern.compile("(?i)K(\\d+)\\+([\\d.]+)"); Matcher matcher = pattern.matcher(s); if(matcher.find()){ return Double.parseDouble(matcher.group(1))*1000+ Double.parseDouble(matcher.group(2)); } return null; } /** * @return java.util.Map * @Description 将key1(val1)key2(val2)key3(val3)...这种格式的参数转换为Map * @Param [str] * @Author yangyj * @Date 2022.08.12 10:39 **/ public static Map string2Map(String str) { Map result = new HashMap<>(15); if (StringUtils.isNotEmpty(str)) { String[] args = str.split("\\)"); for (String a : args) { if (StringUtils.isNotEmpty(a)) { String[] kv = a.split("\\("); result.put(kv[0], kv[1]); } } } return result; } /** * @return java.lang.Boolean * @Description 基础数据类型批量非空判断 * @Param [args] * @Author yangyj * @Date 2022.08.26 11:14 **/ public static Boolean isNotNull(Object... args) { if (args != null) { for (Object obj : args) { if (obj == null || "".equals(obj)) { return false; } } return true; } return false; } /*将不同长度的字符串转换为固定长度的Long*/ public static long str2Long(String input) { try { MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] hash = md.digest(input.getBytes()); BigInteger bigInt = new BigInteger(1, hash); // 取正整数 return bigInt.longValue() & Long.MAX_VALUE; } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description 判断对象是否为数值 * @Param [value] * @Author yangyj * @Date 2023.01.17 13:48 **/ static final String NUM_REG = "^[+-]?\\d+(\\.\\d+)?$"; public static boolean isNumber(Object value) { if ((value == null) ) { return false; } String stringValue = value.toString().trim(); if (stringValue.isEmpty()) { return false; } if (value instanceof Number) { return true; } return Pattern.matches(NUM_REG,stringValue); } /** * @Description 根据指定大小对LIST进行切分 * @Param [list, chunkSize:每一段长度] * @return java.util.List> * @Author yangyj * @Date 2023.07.03 13:46 **/ public static List> splitList(List list, int chunkSize) { return IntStream.range(0, (list.size() + chunkSize - 1) / chunkSize) .mapToObj(i -> list.subList(i * chunkSize, Math.min((i + 1) * chunkSize, list.size()))) .collect(Collectors.toList()); } /** * @Description 深度拷贝 * @Param [originalList] * @return java.util.List * @Author yangyj * @Date 2023.04.28 14:18 **/ public static List copyList(List originalList) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(originalList); oos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); @SuppressWarnings("unchecked") List copiedList = (List) ois.readObject(); ois.close(); return copiedList; } catch (Exception e) { e.printStackTrace(); } return null; } public static String handleNull(Object obj) { if (null == obj) { return ""; } else { return obj.toString().trim(); } } public static boolean isNotEmpty(Object value) { if(value!=null){ if(value instanceof List&&((List) value).size()>0 ){ return true; }else if(value instanceof Map&&((Map) value).size()>0){ return true; }else { return value.toString().trim().length() > 0; } } return false; } public static boolean isEmpty(Object value){ return !isNotEmpty(value); } public static Double[] scopeParse(Object dev, Object design, Object xN) { if (isNotEmpty(dev)) { Double[] result = new Double[2]; double designD = Double.parseDouble(design.toString()); double xND = Double.parseDouble(xN.toString()); String devStr = dev.toString(); devStr = devStr.replaceAll("[\\[\\]]+", ""); double min = 0; double max = 0; devStr = devStr.replaceAll("\\s+", ""); if (devStr.contains("≤") || devStr.contains("<=") || devStr.contains("<")||devStr.contains("≦")) { devStr = devStr.replace("≤", "").replace("<=", "").replace("≦",""); max = designD + Double.parseDouble(devStr) * xND; } else if (devStr.contains("≥") || devStr.contains(">=") || devStr.contains(">")) { devStr = devStr.replace("≥", "").replace(">=", ""); min = designD + Double.parseDouble(devStr) * xND; max = Double.MAX_VALUE; } else if (devStr.contains(",") || devStr.contains(",")) { String[] arr = devStr.split("[,,]"); min = designD + Double.parseDouble(arr[0]) * xND; max = designD + Double.parseDouble(arr[1]) * xND; } else if (devStr.contains("%")) { devStr = devStr.replace("%", ""); double devD = Math.abs(Double.parseDouble(devStr) * designD / 100); min = designD - devD; max = designD + devD; } else if (devStr.contains("±")) { devStr = devStr.replace("±", ""); double devD = Math.abs(Double.parseDouble(devStr) * xND); min = designD - devD; max = designD + devD; } if(min>max){ double tmp=max; max=min; min=tmp; } result[0] = min; result[1] = max; return result; } return null; } public static Integer handleObj2Integer(Object obj) { if (null == obj) { return 0; } else { double value; try { value = Double.parseDouble(obj.toString()); } catch (Exception ex) { value = 0; } return (int) value; } } /** * @return java.util.List 不能包含0 * @Description specifiedRangeList * @Param [hz:频率, design:设计值, dev:偏差范围, xN 偏差范围单位和设计值单位的比值,例如毫米:厘米=0.1, scale:保存小数位, passRate:合格率[0,1]] * @Author yangyj * @Date 2022.03.31 09:16 **/ public static List rangeList(Object hz, Object design, Object dev, Object xN, Object scale, Object passRate) { List result = new ArrayList<>(); if (isNotNull(design, dev, hz)) { if (isEmpty(scale)) { scale = 0; } if (isEmpty(passRate)) { passRate = 1; } if (isEmpty(xN)) { xN = 1; } Double[] range = scopeParse(dev, design, xN); int scaleI = Integer.parseInt(scale.toString()); int min = 0, max = 0; assert range != null; if (range.length > 0) { min = (int) (range[0] * Math.pow(10, scaleI)); max = (int) (range[1] * Math.pow(10, scaleI)); } Random rd = new Random(); int hzi = new BigDecimal(hz.toString()).multiply(new BigDecimal(passRate.toString())).setScale(0, ROUND_CEILING).intValue(); for (int i = 0; i < hzi; i++) { BigDecimal tb = new BigDecimal(rd.nextInt(max - min + 1) + min).divide(BigDecimal.valueOf(Math.pow(10, scaleI)), scaleI, ROUND_HALF_UP); if(tb.equals(BigDecimal.ZERO)){ i--; }else{ if (scaleI > 0) { result.add(tb.doubleValue()); } else { result.add(tb.intValue()); } } } int total = handleObj2Integer(hz); if (total - hzi > 0) { for (int k = 0; k < total - hzi; k++) { BigDecimal tb; if (rd.nextBoolean()) { tb = new BigDecimal(rd.nextInt(((max - min) / 2)) + max + 1).divide(BigDecimal.valueOf(Math.pow(10, scaleI)), scaleI, ROUND_HALF_UP); } else { tb = new BigDecimal(min - 1 - rd.nextInt(((max - min) / 2))).divide(BigDecimal.valueOf(Math.pow(10, scaleI)), scaleI, ROUND_HALF_UP); } if(tb.equals(BigDecimal.ZERO)){ k--; }else { if (scaleI > 0) { result.add(tb.doubleValue()); } else { result.add(tb.intValue()); } } } if (result.size()>0) { Collections.shuffle(result); } } } return result; } /*是否包含链*/ public static boolean notInChain(List cp,String s){ if(cp!=null&& isNotEmpty(s)){ if(cp.size()==0){ /*空结合*/ return true; }else{ /*没有一个已知路径包含*/ return cp.stream().noneMatch(c->c.contains(s)); } } return false; } public static boolean inChain(List cp,String s){ if(cp!=null&& isNotEmpty(s)){ if(cp.size()==0){ /*空结合*/ return true; }else{ /*没有一个已知路径包含*/ return cp.stream().anyMatch(s::contains); } } return false; } public static boolean inChain(String[] cp,String s){ if(cp!=null&&cp.length>0&& s!=null&&s.length()>0){ return inChain(Arrays.asList(cp), s); } return false; } }