All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.lulihu.ToolKit Maven / Gradle / Ivy

package net.lulihu;

import lombok.extern.slf4j.Slf4j;
import net.lulihu.ObjectKit.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * 常用方法集合类
 */
@Slf4j
public class ToolKit {

    /**
     * 获取令牌字符串
     *
     * @param jamStr 干扰字符串
     */
    public static String getToken(String jamStr) {
        byte[] bytes = StrKit.bytes(jamStr + IDGeneratorKit.getStr(), Charset.defaultCharset());
        return EncryptionKit.base64Encode(bytes);
    }


    /**
     * 获取指定位数的16进制数
     *
     * @param len 位数
     */
    public static String getRandomHexString(int len) {
        return HexKit.getRandomHexString(len);
    }

    private static final String[] baseStrings = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
            "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private static final int baseLen = baseStrings.length;

    /**
     * 获取随机位数的字符串
     */
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(baseLen);
            sb.append(baseStrings[number]);
        }
        return sb.toString();
    }


    /**
     * 获取异常的具体信息
     */
    public static String getExceptionMsg(Throwable e) {
        StringWriter sw = new StringWriter();
        try {
            e.printStackTrace(new PrintWriter(sw));
        } finally {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return sw.getBuffer().toString().replaceAll("\\$", "T");
    }

    /**
     * 比较两个对象是否相等。
* 相同的条件有两个,满足其一即可:
* 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2) * * @param obj1 对象1 * @param obj2 对象2 * @return 是否相等 */ public static boolean equals(Object obj1, Object obj2) { return ObjectKit.equals(obj1, obj2); } /** * 计算对象长度; * 字符串调用其length函数
* 集合类调用其size函数
* 数组调用其length属性
* 其他可遍历对象遍历计算长度 * * @param obj 被计算长度的对象 * @return 长度 */ public static int length(Object obj) { if (obj == null) { return 0; } if (obj instanceof CharSequence) { return ((CharSequence) obj).length(); } if (obj instanceof Collection) { return ((Collection) obj).size(); } if (obj instanceof Map) { return ((Map) obj).size(); } int count; if (obj instanceof Iterator) { Iterator iter = (Iterator) obj; count = 0; while (iter.hasNext()) { count++; iter.next(); } return count; } if (obj instanceof Enumeration) { Enumeration enumeration = (Enumeration) obj; count = 0; while (enumeration.hasMoreElements()) { count++; enumeration.nextElement(); } return count; } if (obj.getClass().isArray()) { return Array.getLength(obj); } return -1; } /** * 对象中是否包含指定元素 * * @param obj 对象 * @param element 元素 * @return 是否包含 */ public static boolean contains(Object obj, Object element) { if (obj == null) { return false; } if (obj instanceof String) return element != null && ((String) obj).contains(element.toString()); if (obj instanceof Collection) return ((Collection) obj).contains(element); if (obj instanceof Map) return ((Map) obj).values().contains(element); if (obj instanceof Iterator) { Iterator iter = (Iterator) obj; while (iter.hasNext()) { Object o = iter.next(); if (equals(o, element)) { return true; } } return false; } if (obj instanceof Enumeration) { Enumeration enumeration = (Enumeration) obj; while (enumeration.hasMoreElements()) { Object o = enumeration.nextElement(); if (equals(o, element)) { return true; } } return false; } if (obj.getClass().isArray()) { int len = Array.getLength(obj); for (int i = 0; i < len; i++) { Object o = Array.get(obj, i); if (equals(o, element)) { return true; } } } return false; } /** * 是否为数字 * * @param obj true为是反之不是 */ public static boolean isNum(Object obj) { try { Integer.parseInt(obj.toString()); } catch (Exception e) { return false; } return true; } /** * 格式化文本 * * @param template 文本模板,被替换的部分用 {} 表示 * @param values 参数值 * @return 格式化后的文本 */ public static String format(String template, Object... values) { return StrKit.format(template, values); } /** * 格式化文本 * * @param template 文本模板,被替换的部分用 {key} 表示 * @param map 参数值对 * @return 格式化后的文本 */ public static String format(String template, Map map) { return StrKit.format(template, map); } /** * map的key转为小写 * * @param map map * @return Map */ public static Map caseInsensitiveMap(Map map) { return MapKit.caseInsensitiveMap(map); } /** * 获取map中第一个数据值 * * @param Key的类型 * @param Value的类型 * @param map 数据源 * @return 返回的值 */ public static V getFirstOrNull(Map map) { return MapKit.getFirstOrNull(map); } /** * 创建StringBuilder对象 * * @return StringBuilder对象 */ public static StringBuilder builder(String... strs) { return StrKit.builder(strs); } /** * 去掉指定后缀 * * @param str 字符串 * @param suffix 后缀 * @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串 */ public static String removeSuffix(String str, String suffix) { return StrKit.removeSuffix(str, suffix); } /** * 首字母大写 */ public static String firstLetterToUpper(String val) { return StrKit.firstCharToUpperCase(val); } /** * 首字母小写 */ public static String firstLetterToLower(String val) { return StrKit.firstCharToLowerCase(val); } /** * 判断是否是windows操作系统 */ public static Boolean isWinOs() { return NativeKit.isWinOs(); } /** * 获取临时目录 */ public static String getTempPath() { return FileKit.getTempPath(); } /** * 获取项目路径 */ public static String getWebRootPath(String filePath) throws URISyntaxException { String path = Objects.requireNonNull(ToolKit.class.getClassLoader().getResource("")).toURI().getPath(); path = path.replace("/WEB-INF/classes/", ""); path = path.replace("/target/classes/", ""); path = path.replace("file:/", ""); if (StrKit.isEmpty(filePath)) { return path; } else { return path + "/" + filePath; } } /** * 获取文件后缀名 不包含点 */ public static String getFileSuffix(String fileWholeName) { if (StrKit.isEmpty(fileWholeName)) return null; int lastIndexOf = fileWholeName.lastIndexOf("."); return fileWholeName.substring(lastIndexOf + 1); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy