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();
}

UUID生成工具的更多相关文章

  1. uuid生成工具类

    public class UUIDTool { public static String getUUID() { return UUID.randomUUID().toString().replace ...

  2. 【C#附源码】数据库文档生成工具支持(Excel+Html)

    [2015] 很多时候,我们在生成数据库文档时,使用某些工具,可效果总不理想,不是内容不详细,就是表现效果一般般.很多还是word.html的.看着真是别扭.本人习惯用Excel,所以闲暇时,就简单的 ...

  3. 微软开源全新的文档生成工具DocFX

    微软放弃Sandcastle有些年头了,微软最近开源了全新的文档生成工具DocFX,目前支持C#和VB,类似JSDoc或Sphinx,可以从源代码中提取注释生成文档之外,而且还有语法支持你加入其他的文 ...

  4. css sprite,css雪碧图生成工具V3.0更新

    V3.0主要改进 1.增加了单独添加单张图片以及删除单张图片的功能 2.增加了生成.sprite文件用以保存雪碧图信息 3.增加了打开.sprite文件功能 什么是css sprite CSS spr ...

  5. DBImport v3.44 中文版发布:数据库数据互导及文档生成工具(IT人员必备)

    前言: 距离上一个版本V3.3版本的文章发布,已经是1年10个月前的事了. 其实版本一直在更新,但也没什么大的功能更新,总体比较稳定,所以也不怎么写文介绍了. 至于工作上的事,之前有半年时间跑去学英语 ...

  6. C/C++ makefile自动生成工具(comake2,autotools,linux),希望能为开源做点微薄的贡献!

      序     在linux下C或C++项目开发,Makefile是必备的力气,但是发现手写很麻烦. 在百度有个comake2工具,用于自动生成Makefile工具,而在外边本想找一个同类工具,但发现 ...

  7. .NET平台开源项目速览(4).NET文档生成工具ADB及使用

    很久以前就使用ADB这个工具来生成项目的帮助文档.功能强大,在学习一些开源项目的过程中,官方没有提供CHM帮助文档,所以为了快速的了解项目结构和注释.就生成文档来自己看,非常好用.这也是一个学习方法吧 ...

  8. Linux下三个密码生成工具

    http://code.csdn.net/news/2820879 想出一个难破解且容易记的密码对不是一件简单的事情.在我为电脑设定一个新密码,或者在线注册了一个新的账号,需要输入密码的时候,脑袋就一 ...

  9. (转)Doxygen文档生成工具

    http://blog.csdn.net/lostaway/article/details/6446786 Doxygen 是一个支持 C/C++,以及其它多种语言的跨平台文档生成工具.如同 Java ...

随机推荐

  1. 高并发Redis(Mac)环境配置(一)

    一.产生原因: SNS交互型网站的兴起,对于高并发,大负载数据的操作,海量数据的存储和访问 NoSql四种类型: 键值存储(Redis优点可以快速查询,缺点缺少存储的结构化)              ...

  2. javascript总结26:Date

    1 获取Date对象 Date-引用类型,JavaScript中的内置对象 获取当前时间 var date = new Date(); //UTC的时间 //返回数字,时间的毫秒形式 var date ...

  3. 从零开始学习前端JAVASCRIPT — 9、JavaScript基础RegExp(正则表达式)

    1:正则的概念 正则表达式(regular expression)是一个描述字符规则的对象.可以用来检查一个字符串是否含有某个子字符串,将匹配的子字符串做替换或者从某个字符串中取出符合某个条件的子串等 ...

  4. 通过python实现wc基本功能

    ---恢复内容开始--- 1.Github项目地址: https://github.com/zhg1998/ww/blob/master/wc.py 2.项目相关要求: 写一个命令行程序,模仿已有wc ...

  5. C# 调用带输入输出参数的存储过程

    //调用存储过程执行类似于2//select count(*) from userinfo where username=username and pwd=pwd and grade=grade3// ...

  6. php的数组汉字符串常用函数

    <?php// function add($a,$b,$func){// if(!is_callable($func)){// return false;// }// $m=$a+$b+$fun ...

  7. Qt学习(一)

    1. 简介 跨平台 GUI 通常有三种实现策略 API 映射 相当于将不同平台的 API 提取公共部分.界面库使用同一套 API,将其映射到不同的底层平台上面.相当于取交集 如wxWidgets. 优 ...

  8. linux最基本命令

    1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切换到 ...

  9. mongodb 片键

    mongodb  片键 mongodb的片键是很难控制的,没有完美的片键,只能均衡即可: 片键的方案: 1.id的hashed: 作为第一个方案,你可以使用数据文档_id的哈希作为片键. 这个方案能够 ...

  10. geth attach

    1. geth attachgeth attach ipc:\\.\pipe\geth.ipc2. "Error: insufficient funds for gas * price + ...