From e5bf0d08d05f5c58224fe28cdf743a1bae88e3f0 Mon Sep 17 00:00:00 2001 From: fuliqi <fuliqi@qq.com> Date: 星期四, 30 十一月 2023 16:48:55 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- ycl-platform/src/main/java/com/ycl/common/util/CommonUtils.java | 375 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 375 insertions(+), 0 deletions(-) diff --git a/ycl-platform/src/main/java/com/ycl/common/util/CommonUtils.java b/ycl-platform/src/main/java/com/ycl/common/util/CommonUtils.java new file mode 100644 index 0000000..a6ff4b9 --- /dev/null +++ b/ycl-platform/src/main/java/com/ycl/common/util/CommonUtils.java @@ -0,0 +1,375 @@ +package com.ycl.common.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CommonUtils { + /** + * 妫�鏌ュ璞℃槸鍚︿负绌� + * + * @param obj + * java浠绘剰绫诲瀷 + * @return + */ + @SuppressWarnings("rawtypes") + public static boolean isEmpty(Object obj) { + if (obj == null) { + return true; + + } else if (obj instanceof String && (obj.toString().trim().equals(""))) { + return true; + + } else if (obj instanceof Number && ((Number) obj).doubleValue() < 0) { + return true; + + } else if (obj instanceof Collection && ((Collection) obj).isEmpty()) { + return true; + + } else if (obj instanceof Map && ((Map) obj).isEmpty()) { + return true; + + } else if (obj instanceof Object[] && ((Object[]) obj).length == 0) { + return true; + + } + return false; + } + + /** + * 妫�鏌涓璞℃槸鍚︿负绌� + * + * @param obj + * @return + */ + public static boolean isEmpty(Object... obj) { + boolean res = false; + for (Object o : obj) { + if (isEmpty(o)) { + res = true; + break; + } + } + return res; + } + + /** + * + * + * @param obj + * @return + */ + @SuppressWarnings("rawtypes") + public static boolean hasLength(Object obj, int length) { + + if (obj == null) { + return false; + + } else if (obj instanceof String) { + return obj.toString().trim().length() == length; + + } else if (obj instanceof Collection) { + return ((Collection) obj).size() == length; + + } else if (obj instanceof Map) { + return ((Map) obj).size() == length; + + } else if (obj instanceof Object[]) { + return ((Object[]) obj).length == length; + + } + + return false; + + } + + /** + * 妫�鏌ュ璞℃槸鍚︿笉涓虹┖ + * + * @param obj + * @return + */ + public static boolean isNotEmpty(Object obj) { + return !isEmpty(obj); + } + + /** + * 妫�鏌ュ璞℃槸鍚︿笉涓虹┖ + * + * @param obj + * @return + */ + public static boolean isNotEmpty(Object... obj) { + boolean res = true; + for (Object o : obj) { + if (isEmpty(o)) { + res = false; + } + } + return res; + } + + + + /** + * 鍏嬮殕涓�涓璞� + * + * @param obj + * @return + * @throws Exception + */ + public static Object cloneObject(Object obj) throws Exception { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(byteOut); + out.writeObject(obj); + ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); + ObjectInputStream in = new ObjectInputStream(byteIn); + return in.readObject(); + } + + /** + * 鍒ゆ柇鏄惁涓轰腑鏂� + * + * @param c + * @return + */ + public static boolean isChinese(char c) { + Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); + if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS + || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS + || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A + || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION + || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION + || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { + return true; + } + return false; + } + + public static boolean isChinese(String str) { + if (null != str && str.length() > 0) { + char[] chares = str.toCharArray(); + for (char c : chares) { + if (isChinese(c)) + return true; + } + } else + return false; + + return false; + } + + /** + * 鍒ゆ柇鏄惁涓轰腑鏂囦贡鐮� + * + * @param strName + * @return + */ + public static boolean hasMessyCode(String target) { + Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*"); + Matcher m = p.matcher(target); + String after = m.replaceAll(""); + String temp = after.replaceAll("}", ""); + char[] ch = temp.trim().toCharArray(); + float chLength = ch.length; + float count = 0; + for (int i = 0; i < ch.length; i++) { + char c = ch[i]; + if (!Character.isLetterOrDigit(c)) { + + if (!isChinese(c)) { + count = count + 1; + } + } + } + float result = count / chLength; + if (result > 0.4) { + return true; + } else { + return false; + } + } + + /** + * 鐢熸垚N浣嶉殢鏈烘暟 + * + * @return + */ + public static String getRandom(int N) { + int[] rand = new int[N]; + StringBuffer numString = new StringBuffer(); + for (int i = 0; i < rand.length; i++) { + rand[i] = (Integer) new Random().nextInt(9); + numString.append(rand[i]); + } + return numString.toString(); + } + + /** + * 鍚岀被瀵硅薄灞炴�у鍒�(private,public) 澶嶅埗鍑烘潵鐨勪负鏂板璞� + * + * @param object + * @return + * @throws Exception + */ + public static Object copyThisToNewOne(Object object) throws Exception { + Class<?> classType = object.getClass(); + Object obj = classType.getConstructor(new Class[] {}).newInstance(new Object[] {}); + Field[] fields = classType.getDeclaredFields(); + for (Field field : fields) { + if (Modifier.isPrivate(field.getModifiers())) {// 鍙渶瑕佺鏈夊瓧娈� + String name = field.getName(); + String firstLetter = name.substring(0, 1).toUpperCase(); + String getMethodName = "get" + firstLetter + name.substring(1); + String setMethodName = "set" + firstLetter + name.substring(1); + Method getMethod = classType.getMethod(getMethodName, new Class[] {}); + Method setMethod = classType.getMethod(setMethodName, new Class[] { field.getType() }); + Object value = getMethod.invoke(object, new Object[] {}); + if (value != null) { + setMethod.invoke(obj, new Object[] { value }); + } + } else { + // System.out.println(field.get(object)); + obj.getClass().getField(field.getName()).set((Object) obj, field.get(object)); + } + } + return obj; + } + + /** + * 鍚岀被瀵硅薄灞炴�у鍒�(private,public) 鎶�2涓凡鏈夌殑瀵硅薄灞炴�ц繘琛屽鍒� 姝ゆ柟娉晄rc浠h〃鏉ユ簮鏁版嵁瀵硅薄锛� + * result浠h〃瑕佹妸src涓殑灞炴�у鍒跺埌鐨勫璞� + * + * @param object + * @return + * @throws Exception + */ + public static Object copyThisToAnother(Object src, Object result) throws Exception { + Class<?> classType = src.getClass(); + Object obj = classType.getConstructor(new Class[] {}).newInstance(new Object[] {}); + Field[] fields = classType.getDeclaredFields(); + for (Field field : fields) { + if (Modifier.isPrivate(field.getModifiers())) {// 鍙渶瑕佺鏈夊瓧娈� + String name = field.getName(); + String firstLetter = name.substring(0, 1).toUpperCase(); + String getMethodName = "get" + firstLetter + name.substring(1); + String setMethodName = "set" + firstLetter + name.substring(1); + Method getMethod = classType.getMethod(getMethodName, new Class[] {}); + Method setMethod = classType.getMethod(setMethodName, new Class[] { field.getType() }); + Object value = getMethod.invoke(src, new Object[] {}); + if (value != null) { + setMethod.invoke(result, new Object[] { value }); + } + } else { + // System.out.println(field.get(src)); + obj.getClass().getField(field.getName()).set(result, field.get(src)); + } + } + return result; + } + + public static String getRandomString(int length) { // length琛ㄧず鐢熸垚瀛楃涓茬殑闀垮害 + String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < length; i++) { + int number = random.nextInt(base.length()); + sb.append(base.charAt(number)); + } + return sb.toString(); + } + + public static String getUpperStringRandom(int length) { // length琛ㄧず鐢熸垚瀛楃涓茬殑闀垮害 + + return getRandomString(length).toUpperCase(); + } + + /** + * 鏁板瓧闅忔満鐮佺爜 + * + * @param length + * @return + */ + public static String getDigitRandom(int length) { // length琛ㄧず鐢熸垚瀛楃涓茬殑闀垮害 + String base = "0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < length; i++) { + int number = random.nextInt(base.length()); + sb.append(base.charAt(number)); + } + return sb.toString(); + } + + public static String getFormatParam(Object... obj) { // length琛ㄧず鐢熸垚瀛楃涓茬殑闀垮害 + + StringBuffer sb = new StringBuffer(); + for (Object o : obj) { + if (CommonUtils.isEmpty(sb.toString())) + sb.append(o.toString()); + else + sb.append("," + o.toString()); + + } + return sb.toString(); + } + + /** + * 鑾峰彇涓�涓椂闂存牸寮忕殑缂栧彿 + * + * @param random + * @return yyyyMMddHHmmss + random(bit) + */ + public static String getIdentityByTime(int random) { + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + Date date = new Date(); + return dateFormat.format(date) + getDigitRandom(random); + } + + public static String getUUID(boolean hasSplit) { + UUID uuid = UUID.randomUUID(); + return hasSplit ? uuid.toString() : uuid.toString().replace("-", ""); + } + + public static <T> List<T> removeNull(List<? extends T> oldList) { + oldList.removeAll(Collections.singleton(null)); + Iterator iterator = oldList.iterator(); + while (iterator.hasNext()) { + if (checkIsEtmy(iterator.next())) { + iterator.remove(); + } + } + return (List<T>) oldList; + } + + public static boolean checkIsEtmy(Object o) { + boolean flag = true; + //鑾峰彇鍙傛暟绫� + Class cls = o.getClass(); + //灏嗗弬鏁扮被杞崲涓哄搴斿睘鎬ф暟閲忕殑Field绫诲瀷鏁扮粍锛堝嵆璇ョ被鏈夊灏戜釜灞炴�у瓧娈� N 杞崲鍚庣殑鏁扮粍闀垮害鍗充负 N锛� + Field[] fields = cls.getDeclaredFields(); + for(int i = 0;i < fields.length; i ++){ + Field f = fields[i]; + f.setAccessible(true); + //f.getName()寰楀埌瀵瑰簲瀛楁鐨勫睘鎬у悕锛宖.get(o)寰楀埌瀵瑰簲瀛楁灞炴�у��,f.getGenericType()寰楀埌瀵瑰簲瀛楁鐨勭被鍨� + try { + if (f.get(o)!=null&&!"".equals(f.get(o).toString())) { + flag = false; + } + } catch (IllegalArgumentException | IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return flag; + } +} -- Gitblit v1.8.0