封装各类生成惟一性ID算法的工具类.

import java.security.SecureRandom;
import java.util.UUID;
/**
 * 封装各类生成惟一性ID算法的工具类.
 */
public abstract class Identities {
    private static SecureRandom random = new SecureRandom();
    /**
     * 封装JDK自带的UUID, 经过Random数字生成, 中间有-分割.
     */
    public static String uuid() {
        return uuid(true);
    }
    /**
     * 封装JDK自带的UUID, 经过Random数字生成, 中间无-分割.
     */
    public static String uuid(boolean hasPrefix) {
        String uuid = UUID.randomUUID().toString();
        if (!hasPrefix) {
            uuid = uuid.replaceAll("-", "");
        }
        return uuid;
    }
    /**
     * 使用SecureRandom随机生成Long.
     */
    public static long getLong() {
        return Math.abs(random.nextLong());
    }
    /**
     * 基于Base62编码的SecureRandom随机生成bytes.
     */
    public static String getBase62(int length) {
        byte[] randomBytes = new byte[length];
        random.nextBytes(randomBytes);
        return Encodes.encodeBase62(randomBytes);
    }
}
相关文章
相关标签/搜索