public class UUIDUtils {
private static SecureRandom SEEDER_STATIC = null;
private static byte[] ADDRESS = null;
private static String MID_VALUE_STATIC = null;
private String midValue = null;
private SecureRandom seeder = null;

static {
try {
ADDRESS = InetAddress.getLocalHost().getAddress();
StringBuffer buffer = new StringBuffer(8);
buffer.append(toHex(toInt(ADDRESS), 8));
MID_VALUE_STATIC = buffer.toString();
SEEDER_STATIC = new SecureRandom();
SEEDER_STATIC.nextInt();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public UUIDUtils() {
StringBuffer buffer = new StringBuffer(16);
buffer.append(MID_VALUE_STATIC);
buffer.append(toHex(System.identityHashCode(this), 8));
midValue = buffer.toString();
seeder = new SecureRandom();
seeder.nextInt();
}

/**
* 该方法用来产生一个32位的唯一的标记String
*
* @param obj
* 传入一个参考的对象
* @return
*/
public static String generate(Object obj) {
StringBuffer uid = new StringBuffer(32);

// get the system time
long currentTimeMillis = System.currentTimeMillis();
uid.append(toHex((int) (currentTimeMillis & -1L), 8));

// get the internet address
uid.append(MID_VALUE_STATIC);

// get the object hash value
uid.append(toHex(System.identityHashCode(obj), 8));

// get the random number
uid.append(toHex(getRandom(), 8));

return uid.toString();
}

/**
* 该方法用来产生一个32位的String唯一标记
*/
public String generate() {
StringBuffer uid = new StringBuffer(32);

// get the system time
long currentTimeMillis = System.currentTimeMillis();
uid.append(toHex((int) (currentTimeMillis & -1L), 8));

// get the internet address
uid.append(midValue);

// get the random number
uid.append(toHex(seeder.nextInt(), 8));

return uid.toString();
}

private static String toHex(int value, int length) {
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer buffer = new StringBuffer(length);
int shift = length - 1 << 2;
for (int i = -1; ++i < length;) {
buffer.append(hexDigits[value >> shift & 0xf]);
value <<= 4;
}

return buffer.toString();
}

private static int toInt(byte[] bytes) {
int value = 0;
for (int i = -1; ++i < bytes.length;) {
value <<= 8;
value |= bytes[i];
}

return value;
}

private static synchronized int getRandom() {
return SEEDER_STATIC.nextInt();
}

最新文章

  1. Lua 学习笔记(十一)元表与元方法
  2. 让 http 2来得更猛烈些吧
  3. (转)RVA-相对虚拟地址解释
  4. hdu3410 单调队列
  5. Bitmap的分析与使用.md
  6. iOS 中关闭键盘方法
  7. WCF服务中,[DataMember]属性标记的属性一定要有set访问器
  8. 需要设置jdk的三处位置:
  9. boost.log要点笔记
  10. 转:OWASP发布Web应用程序的十大安全风险
  11. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
  12. UESTC_Little Deer and Blue Cat CDOJ 1025
  13. 1*Json对象声明简单,复合,对象数组
  14. P4语言编程快速开始 实践二
  15. 多线程校验url的种种。。。
  16. 如何修改PCB后更新到原理图(以AD为例)
  17. C#轻量级通通讯组件StriveEngine —— C/S通信开源demo(2) —— 使用二进制协议 (附源码)
  18. 真正的Maven经常使用命令
  19. 《Inside C#》笔记(十二) 委托与事件
  20. Eclipse远程调试Tomcat

热门文章

  1. myeclipse便捷导包方式
  2. Android 65536方法数限制的思考
  3. 【C#】泛型
  4. [Erlang14]怎样模拟节点互连后的各种失败情况?
  5. oracle 触发器序列号自增
  6. 工作中的Buff加成-结构化思考力:第一章:认识结构化思维及其作用
  7. C# 代码 获取桌面路径
  8. NOS服务监控实践
  9. 《Beginning Java 7》 - 7 - abstract class 抽象类 和 interface 接口
  10. oppo手机永久打开USB调试模式