com.github.javaclub.sword.algorithm.UniqueCoder Maven / Gradle / Ivy
The newest version!
/*
* @(#)UniqueCoder.java 2020-8-29
*
* Copyright (c) 2020. All Rights Reserved.
*
*/
package com.github.javaclub.sword.algorithm;
import java.util.Arrays;
import com.github.javaclub.sword.core.Strings;
import com.github.javaclub.sword.core.Systems;
/**
* UniqueCoder
*
* @author Gerald Chen
* @version $Id: UniqueCoder.java 2020-8-29 0:22:10 Exp $
*/
public final class UniqueCoder {
public static UniqueCoder INSTANCE = new UniqueCoder();
/**
* 随机字符串
*/
private static final char[] CHARS = new char[] {
'F', '5', 'L', 'G', 'C', '3', '0', 'W', 'X',
'9', 'Z', 'M', '6', 'R', '7', 'Y', 'T', 'O',
'2', 'H', 'V', 'E', 'S', '8', 'D', 'J', '1',
'4', 'U', 'A', 'N', 'B', 'I', 'K', 'Q', 'P'
};
private final static int CHARS_LENGTH = CHARS.length;
/**
* 邀请码长度
*/
private static int CODE_LENGTH = 6; // 废弃, 默认由方法传入
/**
* 随机数据
*/
private static long SLAT = 1234561L;
/**
* PRIME1 与 CHARS 的长度 L互质,可保证 ( id * PRIME1) % L 在 [0,L)上均匀分布
*/
private final static int PRIME1 = 3;
/**
* PRIME2 与 CODE_LENGTH 互质,可保证 ( index * PRIME2) % CODE_LENGTH 在 [0,CODE_LENGTH)上均匀分布
*/
private final static int PRIME2 = 11;
private UniqueCoder() {
}
public void resetSlat(int codeLenth, long slat) {
SLAT = slat;
}
/**
* 生成唯一码
*
* @param id 唯一的id主键. 支持的最大值为: (36^length - {@link #SLAT})/{@link #PRIME1}
* @return code
*/
public String generate(Long id, int length) {
// 补位
id = id * PRIME1 + SLAT;
//将 id 转换成32进制的值
long[] b = new long[length];
// 32进制数
b[0] = id;
for (int i = 0; i < length - 1; i++) {
b[i + 1] = b[i] / CHARS_LENGTH;
// 按位扩散
b[i] = (b[i] + i * b[0]) % CHARS_LENGTH;
}
long tmp = 0;
for (int i = 0; i < length - 2; i++) {
tmp += b[i];
}
b[length - 1] = tmp * PRIME1 % CHARS_LENGTH;
// 进行混淆
long[] codeIndexArray = new long[length];
for (int i = 0; i < length; i++) {
codeIndexArray[i] = b[i * PRIME2 % length];
}
StringBuilder buffer = new StringBuilder();
Arrays.stream(codeIndexArray).boxed().map(Long::intValue).map(t -> CHARS[t]).forEach(buffer::append);
return buffer.toString();
}
/**
* 将唯一码解密成原来的ID
*/
public Long converseAsId(String code) {
if(Strings.isBlank(code)) {
return null;
}
int CODE_LENGTH = code.length();
// 将字符还原成对应数字
long[] a = new long[CODE_LENGTH];
for (int i = 0; i < CODE_LENGTH; i++) {
char c = code.charAt(i);
int index = findIndex(c);
if (index == -1) {
// 异常字符串
return null;
}
a[i * PRIME2 % CODE_LENGTH] = index;
}
long[] b = new long[CODE_LENGTH];
for (int i = CODE_LENGTH - 2; i >= 0; i--) {
b[i] = (a[i] - a[0]*i + CHARS_LENGTH * i) % CHARS_LENGTH;
}
long res = 0;
for (int i = CODE_LENGTH - 2; i >= 0; i--) {
res += b[i];
res *= (i > 0 ? CHARS_LENGTH : 1);
}
return (res - SLAT) / PRIME1;
}
/**
* 查找对应字符的index
*/
public static int findIndex(char c) {
for (int i = 0; i < CHARS_LENGTH; i++) {
if (CHARS[i] == c) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
long idx = 1L;
for (long j = idx; j <= 9999999999999999L; j++) {
String code = UniqueCoder.INSTANCE.generate(j, 6);
Long num = UniqueCoder.INSTANCE.converseAsId(code);
Systems.format("{} => {} => {}", j, code, num);
}
long j = 9999999999999999L;
String code = UniqueCoder.INSTANCE.generate(j, 12);
Long num = UniqueCoder.INSTANCE.converseAsId(code);
Systems.format("{} => {} => {}", j, code, num);
}
}