From c635d78b479510ebe2556a420948effcd30a0731 Mon Sep 17 00:00:00 2001 From: skyouc Date: 星期六, 21 十二月 2024 18:40:43 +0800 Subject: [PATCH] 新建德森项目分支 --- zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/Cools.java | 736 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 368 insertions(+), 368 deletions(-) diff --git a/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/Cools.java b/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/Cools.java index f8f4a79..0c27ede 100644 --- a/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/Cools.java +++ b/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/Cools.java @@ -1,368 +1,368 @@ -package com.zy.asrs.framework.common; - -import com.zy.asrs.framework.annotations.CoolTranslate; - -import java.lang.reflect.*; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Created by vincent on 2019-06-09 - */ -public class Cools { - - public static boolean isEmpty(Object... objects) { - for (Object obj : objects){ - if (isEmpty(obj)){ - return true; - } - } - return false; - } - - @SuppressWarnings("rawtypes") - public static boolean isEmpty(Object o) { - if (o == null) { - return true; - } - if (o instanceof String) { - if (o.toString().trim().equals("")) { - return true; - } - } else if (o instanceof List) { - if (((List) o).size() == 0) { - return true; - } - } else if (o instanceof Map) { - if (((Map) o).size() == 0) { - return true; - } - } else if (o instanceof Set) { - if (((Set) o).size() == 0) { - return true; - } - } else if (o instanceof Object[]) { - if (((Object[]) o).length == 0) { - return true; - } - } else if (o instanceof int[]) { - if (((int[]) o).length == 0) { - return true; - } - } else if (o instanceof long[]) { - if (((long[]) o).length == 0) { - return true; - } - } - return false; - } - - public static int sqlLimitIndex(Integer pageIndex, Integer pageSize){ - return (pageIndex - 1) * pageSize; - } - - public static String enToken(String username, String password){ - return AesUtils.encrypt(username, zerofill(password, 16)); - } - - public static String deTokn(String token, String password){ - return AesUtils.decrypt(token, zerofill(password, 16)); - } - - public static String zerofill(String msg, Integer count){ - if (msg.length() == count){ - return msg; - } else if (msg.length() > count){ - return msg.substring(0, 16); - } else { - StringBuilder msgBuilder = new StringBuilder(msg); - for (int i = 0; i<count-msg.length(); i++){ - msgBuilder.append("0"); - } - return msgBuilder.toString(); - } - } - - /** - * 鎴彇瀛楃涓�(榛樿end=true) - * @param str 琚埅瀛楃涓� - * @param end true:鏈�鍚庝竴涓瓧绗� / false:绗竴涓瓧绗� - */ - public static String deleteChar(String str, boolean end){ - if (isEmpty(str)){ - return ""; - } - if (end){ - return str.substring(0, str.length()-1); - } else { - return str.substring(1); - } - } - - public static String deleteChar(String str){ - return deleteChar(str, true); - } - - - /** - * map 杞� 瀵硅薄 - */ - public static <T> T conver(Map<? extends String, ?> map, Class<T> cls){ - T instance = null; - try { - Constructor<T> constructor = cls.getDeclaredConstructor(); - boolean constructorAccessible = constructor.isAccessible(); - constructor.setAccessible(true); - instance = constructor.newInstance(); - constructor.setAccessible(constructorAccessible); - } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { - e.printStackTrace(); - } - Class<?> prototype = cls; - do { - for (Field field : prototype.getDeclaredFields()){ - if (Modifier.isFinal(field.getModifiers()) - || Modifier.isStatic(field.getModifiers()) - || Modifier.isTransient(field.getModifiers())){ - continue; - } - String fieldName = field.getName(); - Object val = null; - if (map.containsKey(fieldName)){ - val = map.get(fieldName); - } - if (val != null){ - boolean fieldAccessible = field.isAccessible(); - field.setAccessible(true); - Class<?> type = field.getType(); - try { - Constructor<?> constructor = type.getDeclaredConstructor(String.class); - boolean constructorAccessible = constructor.isAccessible(); - constructor.setAccessible(true); - field.set(instance, constructor.newInstance(String.valueOf(val))); - constructor.setAccessible(constructorAccessible); - } catch (IllegalAccessException - | InstantiationException - | InvocationTargetException - | NoSuchMethodException e) { - System.err.println("convert error ===> Class["+prototype+"],Field:["+fieldName+"],Type:["+type+"],Value:["+val+"]"); - } - field.setAccessible(fieldAccessible); - } - } - prototype = prototype.getSuperclass(); - } while (!Object.class.equals(prototype)); - return instance; - } - - /** - * 瀵硅薄 杞� map - */ - public static Map<String, Object> conver(Object obj){ - Class<?> cls = obj.getClass(); - Field[] fields = getAllFields(cls); - Map<String, Object> map = new HashMap<>(); - for (Field field : fields) { - if (Modifier.isFinal(field.getModifiers()) - || Modifier.isStatic(field.getModifiers()) - || Modifier.isTransient(field.getModifiers())){ - continue; - } - String key = field.getName(); - boolean flag = field.isAccessible(); - field.setAccessible(true); - Object val = null; - try { - val = field.get(obj); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - field.setAccessible(flag); - if (val != null){ - map.put(key, val); - } - } - return map; - } - - /** - * 鑾峰彇鎸囧畾Class锛堝強鍏禨uperClass锛夌殑鎴愬憳鍙橀噺 - */ - public static Field[] getAllFields(Class<?> cls){ - return getAllFields(cls, null); - } - - /** - * 閫掑綊鍚堝苟鍩虹被Field - */ - private static Field[] getAllFields(Class<?> cls, Field[] params){ - Field[] fields = (params == null) ? cls.getDeclaredFields() : params; - Class<?> superCls = cls.getSuperclass(); - if (superCls == null || superCls == Object.class){ - return fields; - } - Field[] superClsFields = superCls.getDeclaredFields(); - fields = addAll(fields, superClsFields); - return getAllFields(superCls, fields); - } - - /** - * 鏍规嵁fieldName鑾峰彇Field瀵硅薄 - */ - public static Field getField(Class<?> cls, String fieldName) { - Field[] allFields = getAllFields(cls); - for (Field field : allFields) { - if (field.getName().equals(fieldName)) { - return field; - } - } - return null; - } - - /** - * 鑾峰彇瀵硅薄涓煇涓狥ield鐨勫�� - */ - public static Object getFieldValue(Object obj, Field field) { - if (null == field) { - return null; - } else { - if (obj instanceof Class) { - obj = null; - } - if (!field.isAccessible()) { - field.setAccessible(true); - } - try { - return field.get(obj); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - } - - /** - * 鏁扮粍鍙犲姞 - */ - @SuppressWarnings("unchecked") - public static <T> T[] addAll(T[] array1, T... array2) { - if (array1 == null) { - return clone(array2); - } else if (array2 == null) { - return clone(array1); - } else { - Class<?> cls = array1.getClass().getComponentType(); - T[] joinedArray = (T[]) Array.newInstance(cls, array1.length + array2.length); - System.arraycopy(array1, 0, joinedArray, 0, array1.length); - - try { - System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); - return joinedArray; - } catch (ArrayStoreException e) { - Class<?> type2 = array2.getClass().getComponentType(); - if (!cls.isAssignableFrom(type2)) { - throw new RuntimeException("Cannot store " + type2.getName() + " in an array of " + cls.getName(), e); - } else { - throw e; - } - } - } - } - - /** - * 鍏嬮殕 - */ - private static <T> T[] clone(T[] array) { - return array == null ? null : array.clone(); - } - - /** - * map鎿嶄綔 - */ - public static CoolMap add(String key,Object value){ - CoolMap map = new CoolMap(); - map.put(key, value); - return map; - } - - public static class CoolMap extends HashMap<String, Object>{ - - public CoolMap add(String key,Object value){ - this.put(key, value); - return this; - } - - public CoolMap $(String key,Object value){ - this.put(key, value); - return this; - } - - } - - public static String md5(String string){ - try{ - MessageDigest md5 = MessageDigest.getInstance("MD5"); - byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8)); - char[] chars = new char[bytes.length * 2]; - for (int i = 0; i < bytes.length; i++) { - int b = bytes[i]; - chars[i * 2] = hexDigits[(b & 0xF0) >> 4]; - chars[i * 2 + 1] = hexDigits[b & 0x0F]; - } - return new String(chars).toLowerCase(); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("md5鍔犲瘑澶辫触,str=".concat(string)); - } - } - - private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - - public static Map<String, Object> translate(Object obj){ - Class<?> cls = obj.getClass(); - Field[] fields = getAllFields(cls); - Map<String, Object> map = new HashMap<>(); - for (Field field : fields) { - String key = field.getName(); - if (field.isAnnotationPresent(CoolTranslate.class)){ - CoolTranslate annotation = field.getAnnotation(CoolTranslate.class); - if (!isEmpty(annotation.value())) { - key = annotation.value(); - } - } - boolean flag = field.isAccessible(); - field.setAccessible(true); - Object val = null; - try { - val = field.get(obj); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - field.setAccessible(flag); - if (val != null){ - map.put(key, val); - } - } - return map; - } - - public static boolean eq(String str, String str0) { - if (Cools.isEmpty(str) && Cools.isEmpty(str0)) { - return true; - } - if (Cools.isEmpty(str) && !Cools.isEmpty(str0)) { - return false; - } - if (Cools.isEmpty(str0) && !Cools.isEmpty(str)) { - return false; - } - if (str.equals(str0)) { - return true; - } - return false; - } - -} +package com.zy.asrs.framework.common; + +import com.zy.asrs.framework.annotations.CoolTranslate; + +import java.lang.reflect.*; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created by vincent on 2019-06-09 + */ +public class Cools { + + public static boolean isEmpty(Object... objects) { + for (Object obj : objects){ + if (isEmpty(obj)){ + return true; + } + } + return false; + } + + @SuppressWarnings("rawtypes") + public static boolean isEmpty(Object o) { + if (o == null) { + return true; + } + if (o instanceof String) { + if (o.toString().trim().equals("")) { + return true; + } + } else if (o instanceof List) { + if (((List) o).size() == 0) { + return true; + } + } else if (o instanceof Map) { + if (((Map) o).size() == 0) { + return true; + } + } else if (o instanceof Set) { + if (((Set) o).size() == 0) { + return true; + } + } else if (o instanceof Object[]) { + if (((Object[]) o).length == 0) { + return true; + } + } else if (o instanceof int[]) { + if (((int[]) o).length == 0) { + return true; + } + } else if (o instanceof long[]) { + if (((long[]) o).length == 0) { + return true; + } + } + return false; + } + + public static int sqlLimitIndex(Integer pageIndex, Integer pageSize){ + return (pageIndex - 1) * pageSize; + } + + public static String enToken(String username, String password){ + return AesUtils.encrypt(username, zerofill(password, 16)); + } + + public static String deTokn(String token, String password){ + return AesUtils.decrypt(token, zerofill(password, 16)); + } + + public static String zerofill(String msg, Integer count){ + if (msg.length() == count){ + return msg; + } else if (msg.length() > count){ + return msg.substring(0, 16); + } else { + StringBuilder msgBuilder = new StringBuilder(msg); + for (int i = 0; i<count-msg.length(); i++){ + msgBuilder.append("0"); + } + return msgBuilder.toString(); + } + } + + /** + * 鎴彇瀛楃涓�(榛樿end=true) + * @param str 琚埅瀛楃涓� + * @param end true:鏈�鍚庝竴涓瓧绗� / false:绗竴涓瓧绗� + */ + public static String deleteChar(String str, boolean end){ + if (isEmpty(str)){ + return ""; + } + if (end){ + return str.substring(0, str.length()-1); + } else { + return str.substring(1); + } + } + + public static String deleteChar(String str){ + return deleteChar(str, true); + } + + + /** + * map 杞� 瀵硅薄 + */ + public static <T> T conver(Map<? extends String, ?> map, Class<T> cls){ + T instance = null; + try { + Constructor<T> constructor = cls.getDeclaredConstructor(); + boolean constructorAccessible = constructor.isAccessible(); + constructor.setAccessible(true); + instance = constructor.newInstance(); + constructor.setAccessible(constructorAccessible); + } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + Class<?> prototype = cls; + do { + for (Field field : prototype.getDeclaredFields()){ + if (Modifier.isFinal(field.getModifiers()) + || Modifier.isStatic(field.getModifiers()) + || Modifier.isTransient(field.getModifiers())){ + continue; + } + String fieldName = field.getName(); + Object val = null; + if (map.containsKey(fieldName)){ + val = map.get(fieldName); + } + if (val != null){ + boolean fieldAccessible = field.isAccessible(); + field.setAccessible(true); + Class<?> type = field.getType(); + try { + Constructor<?> constructor = type.getDeclaredConstructor(String.class); + boolean constructorAccessible = constructor.isAccessible(); + constructor.setAccessible(true); + field.set(instance, constructor.newInstance(String.valueOf(val))); + constructor.setAccessible(constructorAccessible); + } catch (IllegalAccessException + | InstantiationException + | InvocationTargetException + | NoSuchMethodException e) { + System.err.println("convert error ===> Class["+prototype+"],Field:["+fieldName+"],Type:["+type+"],Value:["+val+"]"); + } + field.setAccessible(fieldAccessible); + } + } + prototype = prototype.getSuperclass(); + } while (!Object.class.equals(prototype)); + return instance; + } + + /** + * 瀵硅薄 杞� map + */ + public static Map<String, Object> conver(Object obj){ + Class<?> cls = obj.getClass(); + Field[] fields = getAllFields(cls); + Map<String, Object> map = new HashMap<>(); + for (Field field : fields) { + if (Modifier.isFinal(field.getModifiers()) + || Modifier.isStatic(field.getModifiers()) + || Modifier.isTransient(field.getModifiers())){ + continue; + } + String key = field.getName(); + boolean flag = field.isAccessible(); + field.setAccessible(true); + Object val = null; + try { + val = field.get(obj); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + field.setAccessible(flag); + if (val != null){ + map.put(key, val); + } + } + return map; + } + + /** + * 鑾峰彇鎸囧畾Class锛堝強鍏禨uperClass锛夌殑鎴愬憳鍙橀噺 + */ + public static Field[] getAllFields(Class<?> cls){ + return getAllFields(cls, null); + } + + /** + * 閫掑綊鍚堝苟鍩虹被Field + */ + private static Field[] getAllFields(Class<?> cls, Field[] params){ + Field[] fields = (params == null) ? cls.getDeclaredFields() : params; + Class<?> superCls = cls.getSuperclass(); + if (superCls == null || superCls == Object.class){ + return fields; + } + Field[] superClsFields = superCls.getDeclaredFields(); + fields = addAll(fields, superClsFields); + return getAllFields(superCls, fields); + } + + /** + * 鏍规嵁fieldName鑾峰彇Field瀵硅薄 + */ + public static Field getField(Class<?> cls, String fieldName) { + Field[] allFields = getAllFields(cls); + for (Field field : allFields) { + if (field.getName().equals(fieldName)) { + return field; + } + } + return null; + } + + /** + * 鑾峰彇瀵硅薄涓煇涓狥ield鐨勫�� + */ + public static Object getFieldValue(Object obj, Field field) { + if (null == field) { + return null; + } else { + if (obj instanceof Class) { + obj = null; + } + if (!field.isAccessible()) { + field.setAccessible(true); + } + try { + return field.get(obj); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + + /** + * 鏁扮粍鍙犲姞 + */ + @SuppressWarnings("unchecked") + public static <T> T[] addAll(T[] array1, T... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } else { + Class<?> cls = array1.getClass().getComponentType(); + T[] joinedArray = (T[]) Array.newInstance(cls, array1.length + array2.length); + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + + try { + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } catch (ArrayStoreException e) { + Class<?> type2 = array2.getClass().getComponentType(); + if (!cls.isAssignableFrom(type2)) { + throw new RuntimeException("Cannot store " + type2.getName() + " in an array of " + cls.getName(), e); + } else { + throw e; + } + } + } + } + + /** + * 鍏嬮殕 + */ + private static <T> T[] clone(T[] array) { + return array == null ? null : array.clone(); + } + + /** + * map鎿嶄綔 + */ + public static CoolMap add(String key,Object value){ + CoolMap map = new CoolMap(); + map.put(key, value); + return map; + } + + public static class CoolMap extends HashMap<String, Object>{ + + public CoolMap add(String key,Object value){ + this.put(key, value); + return this; + } + + public CoolMap $(String key,Object value){ + this.put(key, value); + return this; + } + + } + + public static String md5(String string){ + try{ + MessageDigest md5 = MessageDigest.getInstance("MD5"); + byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8)); + char[] chars = new char[bytes.length * 2]; + for (int i = 0; i < bytes.length; i++) { + int b = bytes[i]; + chars[i * 2] = hexDigits[(b & 0xF0) >> 4]; + chars[i * 2 + 1] = hexDigits[b & 0x0F]; + } + return new String(chars).toLowerCase(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("md5鍔犲瘑澶辫触,str=".concat(string)); + } + } + + private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + public static Map<String, Object> translate(Object obj){ + Class<?> cls = obj.getClass(); + Field[] fields = getAllFields(cls); + Map<String, Object> map = new HashMap<>(); + for (Field field : fields) { + String key = field.getName(); + if (field.isAnnotationPresent(CoolTranslate.class)){ + CoolTranslate annotation = field.getAnnotation(CoolTranslate.class); + if (!isEmpty(annotation.value())) { + key = annotation.value(); + } + } + boolean flag = field.isAccessible(); + field.setAccessible(true); + Object val = null; + try { + val = field.get(obj); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + field.setAccessible(flag); + if (val != null){ + map.put(key, val); + } + } + return map; + } + + public static boolean eq(String str, String str0) { + if (Cools.isEmpty(str) && Cools.isEmpty(str0)) { + return true; + } + if (Cools.isEmpty(str) && !Cools.isEmpty(str0)) { + return false; + } + if (Cools.isEmpty(str0) && !Cools.isEmpty(str)) { + return false; + } + if (str.equals(str0)) { + return true; + } + return false; + } + +} -- Gitblit v1.9.1