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. UVa 12342 Tax Calculator (水题,纳税)

    今天在uva看到一个水题,分享一下. 题意:制定纳税的总额,有几个要求,如果第一个180000,不纳,下一个300000,纳10%,再一个400000,纳15%,再一个300000,纳20%,以后的纳 ...

  2. toolbox类

    新建Qt  应用,项目名称为“c”,基类选择“QWidget”,取消“创建界面”复选框的选中状态. 添加该工程的提供主要显示界面的函数所在的文件,在“c”项目名上单击鼠标右键,在弹出的快捷菜单中选择“ ...

  3. Ubuntu : 解决更新时出现 Unable to locate package update

    当用apt-get更新软件包时常出现错误提示Unable to locate package update, 尤其是在ubuntu server上,解决方法是:     先更新apt-get      ...

  4. c_c++基础问题(平时读书时笔记)

    1 IP私有地址: 10.0.0.0 -- 10.255.255.255 172.16.0.0 -- 172.31.255.255 192.168.0.0 -- 192.168.255.255 2OS ...

  5. Mysql自动设置时间(自动获取时间,填充时间)

    应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,不需要应用程序去特意记录,而由数据数据库获取当前时间自动记录创建时间: 2.在数据库中,要记录每条数据是什么时候修改的,不需要应用程序去特意 ...

  6. js链式调用

    我们都很熟悉jQuery了,只能jQuery中一种非常牛逼的写法叫链式操作 * $('#div').css('background','#ccc').removeClass('box').stop() ...

  7. no getter for property named 'power_state

    错误信息:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for pr ...

  8. [LeetCode 题解]: Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【windows】dos命令查看某个文件夹下所有文件目录列表

    dos命令  dir展示一个目录中的文件夹和文件列表  /a代表显示隐藏目录

  10. Redis 工具类

    项目里的Redis 工具类,写下来以备后用 public class RedisConnector { public class RedisParseResult<T> { public ...