123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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.net.JarURLConnection;
- import java.net.URL;
- 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<java.lang.String, java.lang.String>
- * @Description 将key1(val1)key2(val2)key3(val3)...这种格式的参数转换为Map
- * @Param [str]
- * @Author yangyj
- * @Date 2022.08.12 10:39
- **/
- public static Map<String, String> string2Map(String str) {
- Map<String, String> 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;
- }
- /**
- * @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<java.util.List<T>>
- * @Author yangyj
- * @Date 2023.07.03 13:46
- **/
- public static <T> List<List<T>> splitList(List<T> 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<T>
- * @Author yangyj
- * @Date 2023.04.28 14:18
- **/
- public static <T extends Serializable> List<T> copyList(List<T> 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<T> copiedList = (List<T>) 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<java.lang.Object> 不能包含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<Object> rangeList(Object hz, Object design, Object dev, Object xN, Object scale, Object passRate) {
- List<Object> 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;
- }
- }
|