首页 技术 正文
技术 2022年11月21日
0 收藏 719 点赞 4,969 浏览 2626 个字

此工具类用于生成24位字符串ID,唯一不重复。
直接通过 IdGenerator.get() 获取。

源码如下:(点击下载源码 – 生成24位字符串ID__IdGenerator.javaIdGenerator.java )

import java.net.NetworkInterface;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Enumeration;/**
* 生成24位字符串ID
*
*/
public class IdGenerator implements Comparable<IdGenerator> { /**
* 调用该方法获取24位字符串ID
* @return
*/
public static String get() {
return new IdGenerator().toString();
} public IdGenerator() {
_time = _gentime;
_machine = _genmachine;
synchronized (_incLock) {
_inc = _nextInc++;
}
_new = true;
} public int hashCode() {
return _inc;
} public String toStringMongod() {
byte b[] = toByteArray(); StringBuilder buf = new StringBuilder(24); for (int i = 0; i < b.length; i++) {
int x = b[i] & 0xFF;
String s = Integer.toHexString(x);
if (s.length() == 1)
buf.append("0");
buf.append(s);
} return buf.toString();
} public byte[] toByteArray() {
byte b[] = new byte[12];
ByteBuffer bb = ByteBuffer.wrap(b);
bb.putInt(_inc);
bb.putInt(_machine);
bb.putInt(_time);
reverse(b);
return b;
} static void reverse(byte[] b) {
for (int i = 0; i < b.length / 2; i++) {
byte t = b[i];
b[i] = b[b.length - (i + 1)];
b[b.length - (i + 1)] = t;
}
} static String _pos(String s, int p) {
return s.substring(p * 2, (p * 2) + 2);
} public String toString() {
return toStringMongod();
} public int compareTo(IdGenerator id) {
if (id == null)
return -1; long xx = id.getTime() - getTime();
if (xx > 0)
return -1;
else if (xx < 0)
return 1; int x = id._machine - _machine;
if (x != 0)
return -x; x = id._inc - _inc;
if (x != 0)
return -x; return 0;
} public int getMachine() {
return _machine;
} public long getTime() {
long z = _flip(_time);
return z * 1000;
} public int getInc() {
return _inc;
} final int _time;
final int _machine;
final int _inc; boolean _new; static int _flip(int x) {
byte b[] = new byte[4];
ByteBuffer bb = ByteBuffer.wrap(b);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(x);
bb.flip();
bb.order(ByteOrder.BIG_ENDIAN);
return bb.getInt();
} private static int _nextInc = (new java.util.Random()).nextInt();
private static final String _incLock = new String("IdGenerator._incLock"); private static int _gentime = _flip((int) (System.currentTimeMillis() / 1000)); static final Thread _timeFixer;
private static final int _genmachine;
static {
try {
final int machinePiece;
{
StringBuilder sb = new StringBuilder();
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
sb.append(ni.toString());
}
machinePiece = sb.toString().hashCode() << 16;
} final int processPiece = java.lang.management.ManagementFactory.getRuntimeMXBean().getName().hashCode() & 0xFFFF;
_genmachine = machinePiece | processPiece;
} catch (java.io.IOException ioe) {
throw new RuntimeException(ioe);
} _timeFixer = new Thread("IdGenerator-TimeFixer") {
public void run() {
while (true) {
try {
Thread.sleep(499);
} catch (InterruptedException e) {
}
_gentime = _flip((int) (System.currentTimeMillis() / 1000));
}
}
};
_timeFixer.setDaemon(true);
_timeFixer.start();
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899