生成24位字符串ID__IdGenerator.java
2018-02-26 08:54
218 查看
生成24位字符串ID__IdGenerator.java
此工具类用于生成24位字符串ID,唯一不重复。直接通过 IdGenerator.get() 获取。 源码如下:(点击下载源码 -
![](http://www.2b2b92b.com/resources/ueditor/dialogs/attachment/fileTypeImages/icon_txt.gif)
IdGenerator.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++) { c09b 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(); } }
相关文章推荐
- 生成24位字符串ID__IdGenerator.java
- 生成24位字符串ID__IdGenerator.java
- java随机生成字符串,用于设定表的主键id
- [Java--算法]--生成全局唯一的Id(IdGen)
- Hibernate中自带ID生成策略中属性“ generator”的含义
- java生成随机字符串(A-Z0-9)
- Java生成随机字符串的代码
- java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题
- java生成自己定义的表ID
- 【转】Java生成对应字符串的MD5密码模块
- Java 短ID 随机字符串
- 史上最全的java随机数/字符串生成算法(转)
- C# MD5 与 java MD5 生成的字符串不一致问题
- JAVA生成n位随机字符串,数字+英文字母+标点符号
- java唯一无序id生成
- java-高并发-高并发分布式系统中生成全局唯一Id汇总
- xml字符串和java实体类相互转换JaxbXmlUtil工具类 附java实体类生成soap接口报文案例
- java 拼接子串的方式做字符串替换生成json
- android菜鸟进修之路一layout里添加xml文件没有在R.java里生成ID
- ASP.NET 4.0 ListView等容器控件中获取ClientID值与HTML中自动生成ID字符串不一样问题。