1 importjava.net.NetworkInterface;2 importjava.nio.ByteBuffer;3 importjava.nio.ByteOrder;4 importjava.util.Enumeration;5
6 /**
7 * 生成24位字符串ID8 *9 */
10 public class IdGenerator implements Comparable{11
12 /**
13 * 调用该方法获取24位字符串ID14 *@return
15 */
16 public staticString get() {17 return newIdGenerator().toString();18 }19
20 publicIdGenerator() {21 _time =_gentime;22 _machine =_genmachine;23 synchronized(_incLock) {24 _inc = _nextInc++;25 }26 _new = true;27 }28
29 public inthashCode() {30 return_inc;31 }32
33 publicString 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 returnbuf.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 returnb;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, intp) {68 return s.substring(p * 2, (p * 2) + 2);69 }70
71 publicString toString() {72 returntoStringMongod();73 }74
75 public intcompareTo(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 intgetMachine() {97 return_machine;98 }99
100 public longgetTime() {101 long z =_flip(_time);102 return z * 1000;103 }104
105 public intgetInc() {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(intx) {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 returnbb.getInt();123 }124
125 private static int _nextInc = (newjava.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 finalThread _timeFixer;131 private static final int_genmachine;132 static{133 try{134 final intmachinePiece;135 {136 StringBuilder sb = newStringBuilder();137 Enumeration 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 newRuntimeException(ioe);149 }150
151 _timeFixer = new Thread("IdGenerator-TimeFixer") {152 public voidrun() {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 }