java idgenerator_生成24位字符串ID__IdGenerator.java

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值