Files
IM-server-new/broker/src/main/java/win/liyufan/im/IDUtils.java
T

48 lines
1.3 KiB
Java
Raw Normal View History

2019-03-16 17:04:23 +08:00
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package win.liyufan.im;
2021-12-14 12:18:14 +08:00
import java.util.HashMap;
import java.util.HashSet;
2019-03-16 17:04:23 +08:00
public class IDUtils {
2021-12-14 12:18:14 +08:00
private static final int RADIX = 36;
private static final int IDLEN = 9;
private static final int[] FACTOR = {29,19,1,11,7,17,13,23,5};
private static char getChar(long number) {
2019-03-16 17:04:23 +08:00
if (number < 26) {
2021-12-14 12:18:14 +08:00
return (char)('a' + number);
} else {
return (char)('0' + number - 26);
2019-03-16 17:04:23 +08:00
}
}
2021-12-14 12:18:14 +08:00
public static String toUid(long id) {
2019-03-16 17:04:23 +08:00
StringBuilder sb = new StringBuilder();
2021-12-14 12:18:14 +08:00
for (int i = 0; i < IDLEN; i++) {
long bit = ((id % RADIX) * FACTOR[i] + FACTOR[i] + 5)%RADIX;
sb.append(getChar(bit));
id = id / RADIX;
2019-03-16 17:04:23 +08:00
}
return sb.toString();
}
2021-12-14 12:18:14 +08:00
2019-03-16 17:04:23 +08:00
public static void main(String[] args) {
2021-12-14 12:18:14 +08:00
HashSet<String> idset = new HashSet<>();
for (long i = 0; i < 1000000000000L; i++) {
2019-03-16 17:04:23 +08:00
String uid = toUid(i);
2021-12-14 12:18:14 +08:00
if(!idset.add(uid)) {
System.out.println("error, dupliate id");
System.exit(-1);
}
2019-03-16 17:04:23 +08:00
}
}
}