4 changed files with 86 additions and 8 deletions
			
			
		- 
					4common/pom.xml
- 
					70common/src/main/java/com/storeroom/utils/NanoIdUtils.java
- 
					7common/src/main/java/com/storeroom/utils/ValidationUtil.java
- 
					13common/src/test/java/TestNonaId.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(); | |||
|                     } | |||
|                 } | |||
|             } | |||
|         } | |||
|     } | |||
| } | |||
| @ -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()); | |||
|         } | |||
| } | |||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue