2024-01-25 11:38:38 +08:00
|
|
|
package com.engine.salary.util.db;
|
|
|
|
|
|
2024-08-07 09:06:09 +08:00
|
|
|
import java.util.UUID;
|
2024-01-25 11:38:38 +08:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
|
|
|
|
public class IdGenerator {
|
|
|
|
|
private static AtomicLong next = new AtomicLong(1L);
|
|
|
|
|
public static final int ID_LENGTH_36 = 36;
|
|
|
|
|
|
|
|
|
|
public IdGenerator() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static long generate() {
|
|
|
|
|
return System.currentTimeMillis() + next.getAndIncrement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String generateId() {
|
|
|
|
|
return System.currentTimeMillis() + String.valueOf(next.getAndIncrement());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String generateStrId() {
|
|
|
|
|
return System.currentTimeMillis() + generateStrId(36);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String generateStrId(int idLength) {
|
|
|
|
|
if (idLength >= 1 && idLength <= 36) {
|
|
|
|
|
char[] srcChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', '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'};
|
|
|
|
|
char[] chars = new char[idLength];
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < idLength; ++i) {
|
|
|
|
|
if (i != 8 && i != 13 && i != 18 && i != 23) {
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
chars[i] = srcChars[(int)(Math.random() * 26.0D) % 26];
|
|
|
|
|
} else {
|
|
|
|
|
chars[i] = srcChars[(int)(Math.random() * 36.0D) % 36];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
chars[i] = '_';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new String(chars);
|
|
|
|
|
} else {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-07 09:06:09 +08:00
|
|
|
|
|
|
|
|
public static String getUUID() {
|
|
|
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
|
}
|
2024-01-25 11:38:38 +08:00
|
|
|
}
|