| package com.zy.acs.common.utils; | 
|   | 
| import com.zy.acs.framework.common.Cools; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.Iterator; | 
| import java.util.List; | 
| import java.util.Optional; | 
| import java.util.function.BiConsumer; | 
| import java.util.function.Function; | 
|   | 
| /** | 
|  * Created by vincent on 2023/3/14 | 
|  */ | 
| public class Utils { | 
|   | 
|     /** | 
|      * List转为树形结构 | 
|      * | 
|      * @param data           List | 
|      * @param parentId       顶级的parentId | 
|      * @param parentIdMapper 获取parentId的Function | 
|      * @param idMapper       获取id的Function | 
|      * @param consumer       赋值children的Consumer | 
|      * @param <T>            数据的类型 | 
|      * @param <R>            parentId的类型 | 
|      * @return List<T> | 
|      */ | 
|     public static <T, R> List<T> toTreeData(List<T> data, | 
|                                             R parentId, | 
|                                             Function<? super T, ? extends R> parentIdMapper, | 
|                                             Function<? super T, ? extends R> idMapper, | 
|                                             BiConsumer<T, List<T>> consumer) { | 
|         List<T> result = new ArrayList<>(); | 
|         for (T d : data) { | 
|             R dParentId = parentIdMapper.apply(d); | 
|             if (parentId.equals(dParentId)) { | 
|                 R dId = idMapper.apply(d); | 
|                 List<T> children = toTreeData(data, dId, parentIdMapper, idMapper, consumer); | 
|                 if(!children.isEmpty()) { | 
|                     consumer.accept(d, children); | 
|                 } | 
|                 result.add(d); | 
|             } | 
|         } | 
|         return result; | 
|     } | 
|   | 
|     public static <T> void treeRemove(List<T> list, String condition, Function<? super T, ? extends String> fetcher, Function<T, List<T>> childrenGetter) { | 
|         Iterator<T> iterator = list.iterator(); | 
|         while (iterator.hasNext()) { | 
|             T next = iterator.next(); | 
|             List<T> children = childrenGetter.apply(next); | 
|             if (children != null && !children.isEmpty()) { | 
|                 treeRemove(children, condition, fetcher, childrenGetter); | 
|             } else { | 
|                 if (!fetcher.apply(next).contains(condition)) { | 
|                     iterator.remove(); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 数组倒序 | 
|      */ | 
|     public static <T> byte[] reverse(byte[] bytes) { | 
|         if (bytes == null) return null; | 
|         for (int start = 0, end = bytes.length - 1; start < end; start++, end--) { | 
|             byte temp = bytes[end]; | 
|             bytes[end] = bytes[start]; | 
|             bytes[start] = temp; | 
|         } | 
|         return bytes; | 
|     } | 
|   | 
|     /** | 
|      * 截取数组 | 
|      * @param bytes 原数组 | 
|      * @param pos   定位(截取后包含定位点数据) | 
|      * @param len   长度 | 
|      * @return new arr | 
|      */ | 
|     public static byte[] slice(byte[] bytes, int pos, int len) { | 
|         if (bytes == null || bytes.length == 0 || len == 0) { | 
|             return new byte[0]; | 
|         } | 
|         if (pos + len > bytes.length) { | 
|             throw new RuntimeException("com.zy.acs.common.utils ArrayIndexOutOfBoundsException\n" + | 
|                     "原数组 bytes 长度为 " + bytes.length + ",截取长度超过原数组!"); | 
|         } | 
|         byte[] arr = new byte[len]; | 
|         System.arraycopy(bytes, pos, arr, 0, len); | 
|         return arr; | 
|     } | 
|   | 
|     public static byte[] sliceWithReverse(byte[] bytes, int pos, int len) { | 
|         byte[] slice = slice(bytes, pos, len); | 
|         reverse(slice); | 
|         return slice; | 
|     } | 
|   | 
|     public static byte[] merge(Object... objects) { | 
|         int len = 0; | 
|         for (Object object : objects) { | 
|             if (object instanceof byte[]) { | 
|                 byte[] bytes = (byte[]) object; | 
|                 len += bytes.length; | 
|             } | 
|             if (object instanceof Byte) { | 
|                 len++; | 
|             } | 
|         } | 
|         byte[] arr = new byte[len]; | 
|         int idx = 0; | 
|         for (Object object : objects) { | 
|             if (object instanceof byte[]) { | 
|                 byte[] bytes = (byte[]) object; | 
|                 System.arraycopy(bytes, 0, arr, idx, bytes.length); | 
|                 idx += bytes.length; | 
|             } | 
|             if (object instanceof Byte) { | 
|                 byte[] bytes = new byte[] {(Byte) object}; | 
|                 System.arraycopy(bytes, 0, arr, idx, bytes.length); | 
|                 idx += bytes.length; | 
|             } | 
|         } | 
|         return arr; | 
|     } | 
|   | 
|     public static <T> String join(T[] array, String seq) { | 
|         StringBuilder sb = new StringBuilder(); | 
|         if (array != null) { | 
|             for (int i = 0; i < array.length; i++) { | 
|                 sb.append(array[i]); | 
|                 if (i < array.length - 1) { | 
|                     sb.append(seq); | 
|                 } | 
|             } | 
|         } | 
|         return sb.toString(); | 
|     } | 
|   | 
|     public static String zeroFill(String msg, Integer len) { | 
|         len = Optional.ofNullable(len).orElse(16); | 
|         if (msg.length() == len){ | 
|             return msg; | 
|         } else if (msg.length() > len){ | 
|             return msg.substring(0, 16); | 
|         } else { | 
|             StringBuilder msgBuilder = new StringBuilder(msg); | 
|             for (int i = 0; i<len-msg.length(); i++){ | 
|                 msgBuilder.insert(0,"0"); | 
|             } | 
|             return msgBuilder.toString(); | 
|         } | 
|     } | 
|   | 
|     public static String removePrefix(String str, String prefix) { | 
|         if (!Cools.isEmpty(str) && !Cools.isEmpty(prefix)) { | 
|             if (str.startsWith(prefix)) { | 
|                 return str.substring(prefix.length()); | 
|             } else { | 
|                 return str; | 
|             } | 
|         } else { | 
|             return str; | 
|         } | 
|     } | 
|   | 
|     public static String removeSuffix(String str, String suffix) { | 
|         if (!Cools.isEmpty(str) && !Cools.isEmpty(suffix)) { | 
|             if (str.endsWith(suffix)) { | 
|                 return str.substring(0, str.indexOf(suffix)); | 
|             } else { | 
|                 return str; | 
|             } | 
|         } else { | 
|             return str; | 
|         } | 
|     } | 
|   | 
|     public static String toSymbolCase(String str, char symbol) { | 
|         if (str == null) { | 
|             return null; | 
|         } else { | 
|             int length = str.length(); | 
|             StringBuilder sb = new StringBuilder(); | 
|   | 
|             for(int i = 0; i < length; ++i) { | 
|                 char c = str.charAt(i); | 
|                 if (Character.isUpperCase(c)) { | 
|                     Character preChar = i > 0 ? str.charAt(i - 1) : null; | 
|                     Character nextChar = i < str.length() - 1 ? str.charAt(i + 1) : null; | 
|                     if (null != preChar) { | 
|                         if (symbol == preChar) { | 
|                             if (null == nextChar || Character.isLowerCase(nextChar)) { | 
|                                 c = Character.toLowerCase(c); | 
|                             } | 
|                         } else if (Character.isLowerCase(preChar)) { | 
|                             sb.append(symbol); | 
|                             if (null == nextChar || Character.isLowerCase(nextChar)) { | 
|                                 c = Character.toLowerCase(c); | 
|                             } | 
|                         } else if (null == nextChar || Character.isLowerCase(nextChar)) { | 
|                             sb.append(symbol); | 
|                             c = Character.toLowerCase(c); | 
|                         } | 
|                     } else if (null == nextChar || Character.isLowerCase(nextChar)) { | 
|                         c = Character.toLowerCase(c); | 
|                     } | 
|                 } | 
|   | 
|                 sb.append(c); | 
|             } | 
|   | 
|             return sb.toString(); | 
|         } | 
|     } | 
|   | 
|     public static String toCamelCase(CharSequence name) { | 
|         if (null == name) { | 
|             return null; | 
|         } else { | 
|             String name2 = name.toString(); | 
|             if (name2.contains("_")) { | 
|                 int length = name2.length(); | 
|                 StringBuilder sb = new StringBuilder(length); | 
|                 boolean upperCase = false; | 
|   | 
|                 for(int i = 0; i < length; ++i) { | 
|                     char c = name2.charAt(i); | 
|                     if (c == '_') { | 
|                         upperCase = true; | 
|                     } else if (upperCase) { | 
|                         sb.append(Character.toUpperCase(c)); | 
|                         upperCase = false; | 
|                     } else { | 
|                         sb.append(Character.toLowerCase(c)); | 
|                     } | 
|                 } | 
|   | 
|                 return sb.toString(); | 
|             } else { | 
|                 return name2; | 
|             } | 
|         } | 
|     } | 
|   | 
|     public static String sub(String str, Integer maxLen) { | 
|         if (str.length() > maxLen) { | 
|             return str.substring(0, maxLen); | 
|         } | 
|         return str; | 
|     } | 
|   | 
|     public static String generateSeqNum(String lastSeqNum) { | 
|         if (Cools.isEmpty(lastSeqNum)) { | 
|             return zeroFill("1", 4); | 
|         } else { | 
|             int i = Integer.parseInt(lastSeqNum); | 
|             if (i >= 9999) { | 
|                 return zeroFill("1", 4); | 
|             } else { | 
|                 return zeroFill(String.valueOf(i+1), 4); | 
|             } | 
|         } | 
|     } | 
|   | 
|     // pos start in 0 | 
|     public static boolean getBit(byte b, int position) { | 
|         if (position < 0 || position > 7) { | 
|             throw new IllegalArgumentException("Bit position must be between 0 and 7"); | 
|         } | 
|         int mask = 1 << position; | 
|         return (b & mask) != 0; | 
|     } | 
|   | 
|     public static <T> List<T> singletonList(T o) { | 
|         List<T> list = new ArrayList<>(); | 
|         list.add(o); | 
|         return list; | 
|     } | 
|   | 
|     public static String convertToSnakeCaseForOrderByStr(String input) { | 
|         String[] parts = input.split("\\s+"); | 
|         String fieldName = parts[0]; | 
|         String order = parts.length > 1 ? parts[1] : ""; | 
|   | 
|         String snakeCaseField = fieldName.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase(); | 
|   | 
|         return snakeCaseField + (order.isEmpty() ? "" : " " + order); | 
|     } | 
|   | 
| } |