巧借Long类实现自己的UUID生成
来源:互联网 发布:linux expect spawn 编辑:程序博客网 时间:2024/05/22 07:05
在使用数据库的时候,经常需要生成主键,用过数据库的自增长,但ID太有序了,很容易就能让人知道下一条记录的ID。也用过Hibernate的UUID生成,但生成的主键似乎又太长了点,于是想着自己写一个工具来实现。
首先想到的便是用时间戳+随机码的方式,对于并发量不是特别特别特别高的情况来说,基本是够用的,于是我一般都用Long.parseLong(""+(int)(Math.random()*100)+(new Date()).getTime()+(int)(Math.random()*100))的方式来生成一个Long型的值,但这个值一来太长了,二来很容易给人找出规律,毕竟时间戳部分的前几位是一致的,于是考虑到将这个Long值转成类似于16进制之类的字符串。不过数字+字母(分大小写)一共有62位,16进制只用到了其中的一部分,还有大把没用的,有点浪费。在参考了Long类的toHexString()方法源码后,自己改写了一个工具类。
package com.hing.tools;
import java.util.Date;
/** *//**
* author:hingwu email:hing3@163.com QQ:550598 MSN:hing3wu@hotmail.com(很少开)
*
* 2007-3-28 上午11:40:18
*/
public class MyLong ...{
public static final long MIN_VALUE = 0x8000000000000000L;
public static final long MAX_VALUE = 0x7fffffffffffffffL;
final static char[] digits = ...{ '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '-', '_' };
private static String toUnsignedString(long i, int shift) ...{
char[] buf = new char[64];
int charPos = 64;
int radix = 1 << shift;
long mask = radix - 1;
do ...{
buf[--charPos] = digits[(int) (i & mask)];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (64 - charPos));
}
// j为2的次方,如转成16进制就是4,32进制就是5...
public static String getRand(long i,int j)...{
return toUnsignedString(i, j);
}
// 随机码+时间戳+随机码的生成
public static Long getRand()...{
String str1,str2,str3;
str1=getRandStr(2);
str3=getRandStr(3);
str2=(new Date()).getTime()+"";
//System.out.println(str1+str2+str3);
return Long.parseLong(str1+str2+str3);
}
// 主键生成
public static String getKey()...{
return getRand(getRand(),6);
}
// 生成指定长度的随机串
public static String getRandStr(Integer length)...{
String str="";
while(str.length()!=length)...{
str=(Math.random()+"").substring(2,2+length);
}
return str;
}
}
import java.util.Date;
/** *//**
* author:hingwu email:hing3@163.com QQ:550598 MSN:hing3wu@hotmail.com(很少开)
*
* 2007-3-28 上午11:40:18
*/
public class MyLong ...{
public static final long MIN_VALUE = 0x8000000000000000L;
public static final long MAX_VALUE = 0x7fffffffffffffffL;
final static char[] digits = ...{ '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '-', '_' };
private static String toUnsignedString(long i, int shift) ...{
char[] buf = new char[64];
int charPos = 64;
int radix = 1 << shift;
long mask = radix - 1;
do ...{
buf[--charPos] = digits[(int) (i & mask)];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (64 - charPos));
}
// j为2的次方,如转成16进制就是4,32进制就是5...
public static String getRand(long i,int j)...{
return toUnsignedString(i, j);
}
// 随机码+时间戳+随机码的生成
public static Long getRand()...{
String str1,str2,str3;
str1=getRandStr(2);
str3=getRandStr(3);
str2=(new Date()).getTime()+"";
//System.out.println(str1+str2+str3);
return Long.parseLong(str1+str2+str3);
}
// 主键生成
public static String getKey()...{
return getRand(getRand(),6);
}
// 生成指定长度的随机串
public static String getRandStr(Integer length)...{
String str="";
while(str.length()!=length)...{
str=(Math.random()+"").substring(2,2+length);
}
return str;
}
}
测试类:
package com.test;
import java.util.Date;
import com.hing.tools.MyLong;
/** *//**
* author:hingwu
* email:hing3@163.com
* QQ:550598
* MSN:hing3wu@hotmail.com(很少开)
*
* 2007-3-15 下午04:37:26
*/
public class Test ...{
/** *//**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args)...{
// TODO Auto-generated method stub
for(int i=0;i<10;i++)...{
System.out.println("第"+i+"个:"+MyLong.getKey());
}
}
}
import java.util.Date;
import com.hing.tools.MyLong;
/** *//**
* author:hingwu
* email:hing3@163.com
* QQ:550598
* MSN:hing3wu@hotmail.com(很少开)
*
* 2007-3-15 下午04:37:26
*/
public class Test ...{
/** *//**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args)...{
// TODO Auto-generated method stub
for(int i=0;i<10;i++)...{
System.out.println("第"+i+"个:"+MyLong.getKey());
}
}
}
测试结果:
第0个:qK43fCDV
第1个:KCAZxkKE4
第2个:47knOADSDV
第3个:lKZzvVmDQ
第4个:4RweI2I-Ek
第5个:3eup9odSEr
第6个:5p1ZROteDU
第7个:bN0kVHKgP
第8个:2lEqwbPSDE
第9个:7xtVcOSEh
如果把digits中的字符顺序打乱的话,就更难给人找到规律了。
- 巧借Long类实现自己的UUID生成
- 采用Hibernate型式的UUID生成类
- 随机生成UUID的工具类
- 生成UUID的一个工具类
- UUID的生成方法
- uuid的生成
- 生成UUID的例子
- uuid的自动生成
- UUID 的生成方法
- 随机UUID的生成
- UUID随机数的生成,UUID编码
- 一个UUID生成算法的C语言实现 --- WIN32版本
- 一个UUID生成算法的C语言实现 --- WIN32版本
- 生成UUID算法的C语言实现-win32版本
- 一个UUID生成算法的C语言实现 --- WIN32版本
- JPA使用Hibernate实现,使用UUID.主键的生成策略.
- 自己的一些心里路程————java基本类型以及UUID的生成
- 数据库主键UUID,GUID和自己写的工具类
- 从其他sql server 服务器查询
- Spring 系列,第 3 部分: 进入 Spring MVC
- Spring 系列,第 4 部分: Spring JMS 消息处理 1-2-3
- 关于状态栏的一些问题
- 轻量级开发的成功秘诀,第 3 部分: Spring 露出水面
- 巧借Long类实现自己的UUID生成
- 轻量级开发的成功秘诀,第 4 部分: 轻量级容器的比较
- 转载)Windows消息大全
- 数据绑定以及Container.DataItem的具体分析
- 对话框中加入属性页
- 如何快速提高主页的流量?
- blog开张
- Perl中文件与目录的操作
- AOP@Work: AOP 工具比较,第 1 部分