From 84322e415f7e00526c03bb0e879087934ed8a508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8A=9B?= Date: Tue, 24 May 2022 11:08:39 +0800 Subject: [PATCH] fix bug of NanoIdUtils --- .../java/com/storeroom/utils/NanoIdUtils.java | 92 ++++++++++--------- common/src/test/java/TestNonaId.java | 1 + 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/common/src/main/java/com/storeroom/utils/NanoIdUtils.java b/common/src/main/java/com/storeroom/utils/NanoIdUtils.java index 9e2e9ec..26523bc 100644 --- a/common/src/main/java/com/storeroom/utils/NanoIdUtils.java +++ b/common/src/main/java/com/storeroom/utils/NanoIdUtils.java @@ -1,5 +1,7 @@ package com.storeroom.utils; +import cn.hutool.core.lang.id.NanoId; + import java.security.SecureRandom; import java.util.Random; @@ -19,52 +21,52 @@ public final class NanoIdUtils { public static final int DEFAULT_SIZE = 21; public static String randomNanoId() { - return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, DEFAULT_SIZE); + return NanoId.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(); - } - } - } - } - } +// /** +// * 生成随机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/test/java/TestNonaId.java b/common/src/test/java/TestNonaId.java index 4f99d7f..15f7e67 100644 --- a/common/src/test/java/TestNonaId.java +++ b/common/src/test/java/TestNonaId.java @@ -1,3 +1,4 @@ +import cn.hutool.core.lang.id.NanoId; import com.storeroom.utils.NanoIdUtils; import org.junit.jupiter.api.Test;