diff --git a/common/pom.xml b/common/pom.xml index a67276b..a920517 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -13,7 +13,7 @@ 公共模块 - 5.3.4 + 5.8.0 @@ -25,7 +25,7 @@ org.hibernate.validator hibernate-validator - 6.1.5.Final + 7.0.4.Final diff --git a/common/src/main/java/com/storeroom/utils/NanoIdUtils.java b/common/src/main/java/com/storeroom/utils/NanoIdUtils.java new file mode 100644 index 0000000..9e2e9ec --- /dev/null +++ b/common/src/main/java/com/storeroom/utils/NanoIdUtils.java @@ -0,0 +1,70 @@ +package com.storeroom.utils; + +import java.security.SecureRandom; +import java.util.Random; + +/** + * NanoId 工具类 + */ +public final class NanoIdUtils { + + private NanoIdUtils() { + + } + + public static final SecureRandom DEFAULT_NUMBER_GENERATOR = new SecureRandom(); + public static final char[] DEFAULT_ALPHABET = "0123456789ABCDEF".toCharArray(); + + + public static final int DEFAULT_SIZE = 21; + + public static String randomNanoId() { + return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, DEFAULT_SIZE); + } + + + /** + * 生成随机ID + * @param random 随机字符串 + * @param alphabet 字母 + * @param size 长度 + * @return 随机id + */ + public static String randomNanoId(final Random random, final char[] alphabet, final int size) { + if (random == null) { + /*随机数不能为空*/ + throw new IllegalArgumentException("random cannot be null"); + } + if (alphabet == null) { + /*字母不能为空*/ + throw new IllegalArgumentException("alphabet cannot be null."); + } + + if (alphabet.length == 0 || alphabet.length >= 256) { + /* 字母必须保护1-255个字符*/ + throw new IllegalArgumentException("alphabet must contain between 1 and 255 symbols."); + } + + if (size <= 0) { + /*长度必须大于0*/ + throw new IllegalArgumentException("size must be greater than zero."); + } + final int mask = (2 << (int) Math.floor(Math.log(alphabet.length - 1) / Math.log(2))) - 1; + final int step = (int) Math.ceil(1.6 * mask * size / alphabet.length); + + final StringBuilder idBuilder = new StringBuilder(); + while (true) { + final byte[] bytes = new byte[step]; + random.nextBytes(bytes); + for (int i = 0; i < step; i++) { + final int alphabetIndex = bytes[i] & mask; + if (alphabetIndex < alphabet.length) { + idBuilder.append(alphabet[alphabetIndex]); + if (idBuilder.length() == size) { + return idBuilder.toString(); + } + } + } + } + } +} diff --git a/common/src/main/java/com/storeroom/utils/ValidationUtil.java b/common/src/main/java/com/storeroom/utils/ValidationUtil.java index d46bba4..a93186d 100644 --- a/common/src/main/java/com/storeroom/utils/ValidationUtil.java +++ b/common/src/main/java/com/storeroom/utils/ValidationUtil.java @@ -20,10 +20,5 @@ public class ValidationUtil { } } - /** - * 验证是否为邮箱 - */ - public static boolean isEmail(String email) { - return new EmailValidator().isValid(email, null); - } + } diff --git a/common/src/test/java/TestNonaId.java b/common/src/test/java/TestNonaId.java new file mode 100644 index 0000000..4f99d7f --- /dev/null +++ b/common/src/test/java/TestNonaId.java @@ -0,0 +1,13 @@ +import com.storeroom.utils.NanoIdUtils; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +public class TestNonaId { + @Test + public void createId(){ + String nonaid= NanoIdUtils.randomNanoId(); + System.out.println(nonaid); + System.out.println(UUID.randomUUID()); + } +}