生成24位字符串ID__IdGenerator.java
此工具类用于生成24位字符串ID,唯一不重复。
直接通过 IdGenerator.get() 获取。
源码如下:(点击下载源码 - IdGenerator.java )
1 import java.net.NetworkInterface; 2 import java.nio.ByteBuffer; 3 import java.nio.ByteOrder; 4 import java.util.Enumeration; 5 6 /** 7 * 生成24位字符串ID 8 * 9 */ 10 public class IdGenerator implements Comparable<IdGenerator> { 11 12 /** 13 * 调用该方法获取24位字符串ID 14 * @return 15 */ 16 public static String get() { 17 return new IdGenerator().toString(); 18 } 19 20 public IdGenerator() { 21 _time = _gentime; 22 _machine = _genmachine; 23 synchronized (_incLock) { 24 _inc = _nextInc++; 25 } 26 _new = true; 27 } 28 29 public int hashCode() { 30 return _inc; 31 } 32 33 public String toStringMongod() { 34 byte b[] = toByteArray(); 35 36 StringBuilder buf = new StringBuilder(24); 37 38 for (int i = 0; i < b.length; i++) { 39 int x = b[i] & 0xFF; 40 String s = Integer.toHexString(x); 41 if (s.length() == 1) 42 buf.append("0"); 43 buf.append(s); 44 } 45 46 return buf.toString(); 47 } 48 49 public byte[] toByteArray() { 50 byte b[] = new byte[12]; 51 ByteBuffer bb = ByteBuffer.wrap(b); 52 bb.putInt(_inc); 53 bb.putInt(_machine); 54 bb.putInt(_time); 55 reverse(b); 56 return b; 57 } 58 59 static void reverse(byte[] b) { 60 for (int i = 0; i < b.length / 2; i++) { 61 byte t = b[i]; 62 b[i] = b[b.length - (i + 1)]; 63 b[b.length - (i + 1)] = t; 64 } 65 } 66 67 static String _pos(String s, int p) { 68 return s.substring(p * 2, (p * 2) + 2); 69 } 70 71 public String toString() { 72 return toStringMongod(); 73 } 74 75 public int compareTo(IdGenerator id) { 76 if (id == null) 77 return -1; 78 79 long xx = id.getTime() - getTime(); 80 if (xx > 0) 81 return -1; 82 else if (xx < 0) 83 return 1; 84 85 int x = id._machine - _machine; 86 if (x != 0) 87 return -x; 88 89 x = id._inc - _inc; 90 if (x != 0) 91 return -x; 92 93 return 0; 94 } 95 96 public int getMachine() { 97 return _machine; 98 } 99 100 public long getTime() { 101 long z = _flip(_time); 102 return z * 1000; 103 } 104 105 public int getInc() { 106 return _inc; 107 } 108 109 final int _time; 110 final int _machine; 111 final int _inc; 112 113 boolean _new; 114 115 static int _flip(int x) { 116 byte b[] = new byte[4]; 117 ByteBuffer bb = ByteBuffer.wrap(b); 118 bb.order(ByteOrder.LITTLE_ENDIAN); 119 bb.putInt(x); 120 bb.flip(); 121 bb.order(ByteOrder.BIG_ENDIAN); 122 return bb.getInt(); 123 } 124 125 private static int _nextInc = (new java.util.Random()).nextInt(); 126 private static final String _incLock = new String("IdGenerator._incLock"); 127 128 private static int _gentime = _flip((int) (System.currentTimeMillis() / 1000)); 129 130 static final Thread _timeFixer; 131 private static final int _genmachine; 132 static { 133 try { 134 final int machinePiece; 135 { 136 StringBuilder sb = new StringBuilder(); 137 Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); 138 while (e.hasMoreElements()) { 139 NetworkInterface ni = e.nextElement(); 140 sb.append(ni.toString()); 141 } 142 machinePiece = sb.toString().hashCode() << 16; 143 } 144 145 final int processPiece = java.lang.management.ManagementFactory.getRuntimeMXBean().getName().hashCode() & 0xFFFF; 146 _genmachine = machinePiece | processPiece; 147 } catch (java.io.IOException ioe) { 148 throw new RuntimeException(ioe); 149 } 150 151 _timeFixer = new Thread("IdGenerator-TimeFixer") { 152 public void run() { 153 while (true) { 154 try { 155 Thread.sleep(499); 156 } catch (InterruptedException e) { 157 } 158 _gentime = _flip((int) (System.currentTimeMillis() / 1000)); 159 } 160 } 161 }; 162 _timeFixer.setDaemon(true); 163 _timeFixer.start(); 164 } 165 166 }
欢迎访问 JAVA技术分享 www.2b2b92b.com