| | |
| | | package com.ruoyi.common.utils;
|
| | |
|
| | | import java.lang.reflect.Field;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Collection;
|
| | | import java.util.HashSet;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | |
|
| | | import cn.hutool.core.util.StrUtil;
|
| | | import cn.hutool.extra.pinyin.PinyinUtil;
|
| | | import org.springframework.util.AntPathMatcher;
|
| | | import com.ruoyi.common.constant.Constants;
|
| | | import com.ruoyi.common.core.text.StrFormatter;
|
| | |
| | |
|
| | | /** 下划线 */
|
| | | private static final char SEPARATOR = '_';
|
| | |
|
| | | /** 星号 */
|
| | | private static final char ASTERISK = '*';
|
| | |
|
| | | /**
|
| | | * 获取参数不为空值
|
| | |
| | | }
|
| | |
|
| | | /**
|
| | | * 替换指定字符串的指定区间内字符为"*"
|
| | | *
|
| | | * @param str 字符串
|
| | | * @param startInclude 开始位置(包含)
|
| | | * @param endExclude 结束位置(不包含)
|
| | | * @return 替换后的字符串
|
| | | */
|
| | | public static String hide(CharSequence str, int startInclude, int endExclude)
|
| | | {
|
| | | if (isEmpty(str))
|
| | | {
|
| | | return NULLSTR;
|
| | | }
|
| | | final int strLength = str.length();
|
| | | if (startInclude > strLength)
|
| | | {
|
| | | return NULLSTR;
|
| | | }
|
| | | if (endExclude > strLength)
|
| | | {
|
| | | endExclude = strLength;
|
| | | }
|
| | | if (startInclude > endExclude)
|
| | | {
|
| | | // 如果起始位置大于结束位置,不替换
|
| | | return NULLSTR;
|
| | | }
|
| | | final char[] chars = new char[strLength];
|
| | | for (int i = 0; i < strLength; i++)
|
| | | {
|
| | | if (i >= startInclude && i < endExclude)
|
| | | {
|
| | | chars[i] = ASTERISK;
|
| | | }
|
| | | else
|
| | | {
|
| | | chars[i] = str.charAt(i);
|
| | | }
|
| | | }
|
| | | return new String(chars);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 截取字符串
|
| | | *
|
| | | * @param str 字符串
|
| | |
| | | }
|
| | |
|
| | | /**
|
| | | * 在字符串中查找第一个出现的 `open` 和最后一个出现的 `close` 之间的子字符串
|
| | | * |
| | | * @param str 要截取的字符串
|
| | | * @param open 起始字符串
|
| | | * @param close 结束字符串
|
| | | * @return 截取结果
|
| | | */
|
| | | public static String substringBetweenLast(final String str, final String open, final String close)
|
| | | {
|
| | | if (isEmpty(str) || isEmpty(open) || isEmpty(close))
|
| | | {
|
| | | return NULLSTR;
|
| | | }
|
| | | final int start = str.indexOf(open);
|
| | | if (start != INDEX_NOT_FOUND)
|
| | | {
|
| | | final int end = str.lastIndexOf(close);
|
| | | if (end != INDEX_NOT_FOUND)
|
| | | {
|
| | | return str.substring(start + open.length(), end);
|
| | | }
|
| | | }
|
| | | return NULLSTR;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 判断是否为空,并且不是空白字符
|
| | | * |
| | | * @param str 要判断的value
|
| | | * @return 结果
|
| | | */
|
| | | public static boolean hasText(String str)
|
| | | {
|
| | | return (str != null && !str.isEmpty() && containsText(str));
|
| | | }
|
| | |
|
| | | private static boolean containsText(CharSequence str)
|
| | | {
|
| | | int strLen = str.length();
|
| | | for (int i = 0; i < strLen; i++)
|
| | | {
|
| | | if (!Character.isWhitespace(str.charAt(i)))
|
| | | {
|
| | | return true;
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 格式化文本, {} 表示占位符<br>
|
| | | * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
|
| | | * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
|
| | |
| | | *
|
| | | * @param str 字符串
|
| | | * @param sep 分隔符
|
| | | * @return list集合
|
| | | */
|
| | | public static final List<String> str2List(String str, String sep)
|
| | | {
|
| | | return str2List(str, sep, true, false);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 字符串转list
|
| | | * |
| | | * @param str 字符串
|
| | | * @param sep 分隔符
|
| | | * @param filterBlank 过滤纯空白
|
| | | * @param trim 去掉首尾空白
|
| | | * @return list集合
|
| | |
| | | }
|
| | |
|
| | | return list;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
|
| | | *
|
| | | * @param collection 给定的集合
|
| | | * @param array 给定的数组
|
| | | * @return boolean 结果
|
| | | */
|
| | | public static boolean containsAny(Collection<String> collection, String... array)
|
| | | {
|
| | | if (isEmpty(collection) || isEmpty(array))
|
| | | {
|
| | | return false;
|
| | | }
|
| | | else
|
| | | {
|
| | | for (String str : array)
|
| | | {
|
| | | if (collection.contains(str))
|
| | | {
|
| | | return true;
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | }
|
| | |
|
| | | /**
|
| | | * 驼峰式命名法 例如:user_name->userName
|
| | | * 驼峰式命名法
|
| | | * 例如:user_name->userName
|
| | | */
|
| | | public static String toCamelCase(String s)
|
| | | {
|
| | | if (s == null)
|
| | | {
|
| | | return null;
|
| | | }
|
| | | if (s.indexOf(SEPARATOR) == -1)
|
| | | {
|
| | | return s;
|
| | | }
|
| | | s = s.toLowerCase();
|
| | | StringBuilder sb = new StringBuilder(s.length());
|
| | |
| | | {
|
| | | return (T) obj;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
|
| | | * |
| | | * @param num 数字对象
|
| | | * @param size 字符串指定长度
|
| | | * @return 返回数字的字符串格式,该字符串为指定长度。
|
| | | */
|
| | | public static final String padl(final Number num, final int size)
|
| | | {
|
| | | return padl(num.toString(), size, '0');
|
| | | }
|
| | |
|
| | | /**
|
| | | * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。
|
| | | * |
| | | * @param s 原始字符串
|
| | | * @param size 字符串指定长度
|
| | | * @param c 用于补齐的字符
|
| | | * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。
|
| | | */
|
| | | public static final String padl(final String s, final int size, final char c)
|
| | | {
|
| | | final StringBuilder sb = new StringBuilder(size);
|
| | | if (s != null)
|
| | | {
|
| | | final int len = s.length();
|
| | | if (s.length() <= size)
|
| | | {
|
| | | for (int i = size - len; i > 0; i--)
|
| | | {
|
| | | sb.append(c);
|
| | | }
|
| | | sb.append(s);
|
| | | }
|
| | | else
|
| | | {
|
| | | return s.substring(len - size, len);
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | for (int i = size; i > 0; i--)
|
| | | {
|
| | | sb.append(c);
|
| | | }
|
| | | }
|
| | | return sb.toString();
|
| | | }
|
| | |
|
| | | public static String padRight(String str, int length) {
|
| | | if (str == null) str = "";
|
| | | if (str.length() >= length) return str;
|
| | | // 用空格补齐
|
| | | return String.format("%-" + length + "s", str);
|
| | | }
|
| | |
|
| | |
|
| | | public static boolean containsChinese(String str) {
|
| | | if (str == null) return false;
|
| | | for (char c : str.toCharArray()) {
|
| | | if (Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN) {
|
| | | return true;
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | public static String getProcessNo(String processName) {
|
| | | if (StrUtil.isBlank(processName)) {
|
| | | return "";
|
| | | }
|
| | |
|
| | | // 判断是否包含中文
|
| | | if (StringUtils.containsChinese(processName)) {
|
| | | // 中文:拼音首字母
|
| | | return StrUtil.toUpperCase(PinyinUtil.getFirstLetter(processName, ""));
|
| | | } else {
|
| | | // 英文:取大写字母 目前工序名称没有英文的形式 只能处理ExxxBxxx
|
| | | StringBuilder sb = new StringBuilder();
|
| | | for (char c : processName.toCharArray()) {
|
| | | if (Character.isUpperCase(c)) {
|
| | | sb.append(c);
|
| | | }
|
| | | }
|
| | | return sb.toString();
|
| | | }
|
| | | }
|
| | |
|
| | | public static void fillStringNull(Object obj) {
|
| | | if (obj == null) return;
|
| | |
|
| | | Class<?> clazz = obj.getClass();
|
| | |
|
| | | // 基本类型 / 包装类 / String 不处理
|
| | | if (clazz.isPrimitive() ||
|
| | | clazz == String.class ||
|
| | | Number.class.isAssignableFrom(clazz) ||
|
| | | clazz == Boolean.class ||
|
| | | clazz == Character.class) {
|
| | | return;
|
| | | }
|
| | |
|
| | | for (Field field : clazz.getDeclaredFields()) {
|
| | | field.setAccessible(true);
|
| | | try {
|
| | | Object value = field.get(obj);
|
| | |
|
| | | // 1️⃣ String 类型:null → ""
|
| | | if (field.getType() == String.class) {
|
| | | if (value == null) {
|
| | | field.set(obj, "");
|
| | | }
|
| | | }
|
| | | // 2️⃣ List 递归处理
|
| | | else if (value instanceof List) {
|
| | | for (Object item : (List<?>) value) {
|
| | | fillStringNull(item);
|
| | | }
|
| | | }
|
| | | // 3️⃣ 其他对象递归
|
| | | else if (value != null && !isJdkClass(value.getClass())) {
|
| | | fillStringNull(value);
|
| | | }
|
| | |
|
| | | } catch (IllegalAccessException ignored) {}
|
| | | }
|
| | | }
|
| | |
|
| | | private static boolean isJdkClass(Class<?> clazz) {
|
| | | return clazz.getPackage() != null &&
|
| | | clazz.getPackage().getName().startsWith("java.");
|
| | | }
|
| | | } |