java 生成code工具类
package utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.*;
public class TicketCodeUtils {
private static final char[] BASE = new char[]{'H', 'V', 'E', '8', 'S', '2', 'D', 'Z', 'X', '9', 'C', '7', 'P',
'5', 'I', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'W', 'Y', 'L', 'T', 'N', '6', 'B', 'G', 'Q'};
private static final char SUFFIX_CHAR = 'A';
private static final int BIN_LEN = BASE.length;
private static final int CODE_LEN = 10;
public static String code() {
long startCode = 1L;
char[] buf = new char[BIN_LEN];
int charPos = BIN_LEN;
while (startCode / BIN_LEN > 0) {
int index = (int) (startCode % BIN_LEN);
buf[--charPos] = BASE[index];
startCode /= BIN_LEN;
}
buf[--charPos] = BASE[(int) (startCode % BIN_LEN)];
String result = new String(buf, charPos, BIN_LEN - charPos);
int len = result.length();
if (len < CODE_LEN) {
StringBuilder sb = new StringBuilder();
sb.append(SUFFIX_CHAR);
Random random = new Random();
for (int i = 0; i < CODE_LEN - len - 1; i++) {
sb.append(BASE[random.nextInt(BIN_LEN)]);
}
result += sb.toString();
}
return result;
}
public static List<String> orderCode(int num) {
HashSet<String> codes = new HashSet<>();
while (codes.size() < num) {
String code = code();
code = "T" + code.substring(1, 8);
codes.add(code);
}
return new ArrayList<>(codes);
}
public static void main(String[] args) {
List<String> list = orderCode(2);
System.out.println(list);
}
public static List<String> customerOrderCode(int num) {
HashSet<String> codes = new HashSet<>();
while (codes.size() < num) {
String code = code();
codes.add(code);
}
return new ArrayList<>(codes);
}
public static String ticketTypeCode(List<String> list) {
Map<String, String> map = new HashMap<>();
if (!CollectionUtils.isEmpty(list)) {
for (String str : list) {
map.put(str, str);
}
}
while (true) {
String code = code();
code = "C" + code.substring(1, 5);
if (StringUtils.isBlank(map.get(code))) {
return code;
}
}
}
public static String decodeBASE64(String code){
return new String(Base64.getDecoder().decode(code));
}
public static String encodeBASE64(String code) {
return new String(Base64.getEncoder().encode(code.getBytes()));
}
}
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>