兑换码(Java)
类似游戏兑换码格式,或者自定义格式的兑换码生成。
支持自定义格式,支持添加对象参数。
package com.krls.simulator;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class T333 {
/**
* 对象转Map
*
* @param object
* @return
* @throws IllegalAccessException
*/
public static Map<String, Object> beanToMap(Object object) throws IllegalAccessException {
if (object instanceof Map) {
return (Map<String, Object>) object;
}
Map<String, Object> map = new HashMap<>(16);
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
map.put(field.getName(), field.get(object));
}
return map;
}
public static String replaceAttr(String str, Object obj) {
if (obj == null) {
return str;
}
try {
Map<String, Object> map = beanToMap(obj);
if (!CollectionUtils.isEmpty(map)) {
for (Map.Entry<String, Object> entity : map.entrySet()) {
String val = entity.getValue() == null ? "" : entity.getValue().toString();
str = str.replaceAll("\\$\\{" + entity.getKey() + "}", val);
}
}
} catch (IllegalAccessException e) {
}
return str;
}
public static String replaceRandom(String source, Map<String, String> map) {
String reg = "\\[([^.]-[^.])+]\\{[0-9]*}";
Matcher matcher = Pattern.compile(reg).matcher(source);
int i = 0;
// System.out.println(source);
while (matcher.find()) {
String group = matcher.group();
String radius = group.substring(group.indexOf("\\[") + 2, group.indexOf("]"));
String num = group.substring(group.indexOf("{") + 1, group.indexOf("}"));
String key = "$" + i + "_" + Integer.parseInt(num);
map.put(key, getRandomStr(radius));
source = source.substring(0, source.indexOf(group)) + "$" + i++ + source.substring(source.indexOf(group) + group.length());
}
return source;
}
public static String getRandomStr(String str) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < str.length(); i = i + 3) {
int len = (int) str.charAt(i + 2) - (int) str.charAt(i);
for (int j = 0; j <= len; j++) {
buffer.append((char) ((int) str.charAt(i) + j));
}
}
return buffer.toString();
}
public static List<String> getRandomStr(String str, int len, int size) {
Random random = new Random();
List<String> list = new ArrayList<>();
StringBuilder cache = new StringBuilder();
for (int i = 0; i < len * size; i++) {
int temp = random.nextInt(str.length());
cache.append(str.charAt(temp));
if ((i + 1) % len == 0) {
list.add(cache.toString());
cache = new StringBuilder();
}
}
return list;
}
/**
* 兑换码生成(V1.0)
*
* @param pattern => [0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{4} ${randomNo}-[0-9A-Z]{5}
* @param obj => object(id=? name? randomNo?)
* @param size =>
* @return
*/
public static List<String> getRandomCode(String pattern, Object obj, int size) {
pattern = replaceAttr(pattern, obj);
Map<String, String> map = new HashMap<>(16);
pattern = replaceRandom(pattern, map);
// size = size > 2 << 4 ? size * 3 / 2 : size << 1;
Map<Integer, String> result = new HashMap<>(16);
for (int i = 0; i < size; i++) {
result.put(i, pattern);
}
if (!CollectionUtils.isEmpty(map)) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
String[] arr = key.split("_");
List<String> randoms = getRandomStr(val, Integer.parseInt(arr[1]), size);
for (int i = 0; i < randoms.size(); i++) {
result.put(i, result.get(i).replace(arr[0], randoms.get(i)));
}
}
}
return new ArrayList<>(result.values());
}
}