- 浏览: 466793 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
yuan_bin1990:
您好,请问下demo如何运行啊,准备研究研究,但不知道入口啊。 ...
ssh2(struts2+spring2.5+hibernate3.3)自动生成代码程序 -
luyulong:
[b][/b][i][/i][ ...
jQuery进度条插件 jQuery progressBar -
txin0814:
mark..
读取文件目录 -
vurses:
[align=center][color=red][size= ...
include 与 jsp:include区别 -
Roshan2:
http://lijiejava.iteye.com/blog ...
Spring AOP 入门实例
转载,有兴趣的学学: java 5 和java 6里已带相应的类 java.util.UUID, 直接调用即可: System.out.println( java.util.UUID.randomUUID()); 但如果用java 1.4怎么办呢,其实这个类相关很少,可以用jdk5里拿出来,放到java 1.4里面用。从中也可以看出GUID生成的算法。不错吧? 源码需要稍做修改如下:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public final class UUID
implements Serializable, Comparable
{
private static final long serialVersionUID = 0xbc9903f7986d852fL;
private final long mostSigBits;
private final long leastSigBits;
private transient int version;
private transient int variant;
private volatile transient long timestamp;
private transient int sequence;
private transient long node;
private transient int hashCode;
private static volatile SecureRandom numberGenerator = null;
// static final boolean $assertionsDisabled = !UUID.desiredAssertionStatus();
public static void main(String[] args) {
System.out.println(UUID.randomUUID());
List list = new ArrayList();
String uid = "";
for (int i = 0; i < 100000; i++) {
uid = UUID.randomUUID().toString();
list.add(uid);
}
System.out.println(list.size());
removeDuplicateObj(list);
}
private static void removeDuplicateObj(List list) {
Set someSet = new HashSet(list);
Iterator iterator = someSet.iterator();
List tempList = new ArrayList();
int i = 0;
while (iterator.hasNext()) {
tempList.add(iterator.next().toString());
i++;
}
System.out.println(tempList.size());
}
private UUID(byte abyte0[])
{
version = -1;
variant = -1;
timestamp = -1L;
sequence = -1;
node = -1L;
hashCode = -1;
long l = 0L;
long l1 = 0L;
// if(!$assertionsDisabled && abyte0.length != 16)
// throw new AssertionError();
for(int i = 0; i < 8; i++)
l = l << 8 | (long)(abyte0[i] & 0xff);
for(int j = 8; j < 16; j++)
l1 = l1 << 8 | (long)(abyte0[j] & 0xff);
mostSigBits = l;
leastSigBits = l1;
}
public UUID(long l, long l1)
{
version = -1;
variant = -1;
timestamp = -1L;
sequence = -1;
node = -1L;
hashCode = -1;
mostSigBits = l;
leastSigBits = l1;
}
public static UUID randomUUID()
{
SecureRandom securerandom = numberGenerator;
if(securerandom == null)
numberGenerator = securerandom = new SecureRandom();
byte abyte0[] = new byte[16];
securerandom.nextBytes(abyte0);
abyte0[6] &= 0xf;
abyte0[6] |= 0x40;
abyte0[8] &= 0x3f;
abyte0[8] |= 0x80;
UUID uuid = new UUID(abyte0);
return new UUID(abyte0);
}
public static UUID nameUUIDFromBytes(byte abyte0[])
{
MessageDigest messagedigest;
try
{
messagedigest = MessageDigest.getInstance("MD5");
}
catch(NoSuchAlgorithmException nosuchalgorithmexception)
{
throw new InternalError("MD5 not supported");
}
byte abyte1[] = messagedigest.digest(abyte0);
abyte1[6] &= 0xf;
abyte1[6] |= 0x30;
abyte1[8] &= 0x3f;
abyte1[8] |= 0x80;
return new UUID(abyte1);
}
public static UUID fromString(String s)
{
String as[] = s.split("-");
if(as.length != 5)
throw new IllegalArgumentException((new StringBuilder())
.append("Invalid UUID string: ").append(s).toString());
for(int i = 0; i < 5; i++)
as[i] = (new StringBuilder()).append("0x").append(as[i]).toString();
long l = Long.decode(as[0]).longValue();
l <<= 16;
l |= Long.decode(as[1]).longValue();
l <<= 16;
l |= Long.decode(as[2]).longValue();
long l1 = Long.decode(as[3]).longValue();
l1 <<= 48;
l1 |= Long.decode(as[4]).longValue();
return new UUID(l, l1);
}
public long getLeastSignificantBits()
{
return leastSigBits;
}
public long getMostSignificantBits()
{
return mostSigBits;
}
public int version()
{
if(version < 0)
version = (int)(mostSigBits >> 12 & 15L);
return version;
}
public int variant()
{
if(variant < 0)
if(leastSigBits >>> 63 == 0L)
variant = 0;
else
if(leastSigBits >>> 62 == 2L)
variant = 2;
else
variant = (int)(leastSigBits >>> 61);
return variant;
}
public long timestamp()
{
if(version() != 1)
throw new UnsupportedOperationException("Not a time-based UUID");
long l = timestamp;
if(l < 0L)
{
l = (mostSigBits & 4095L) << 48;
l |= (mostSigBits >> 16 & 65535L) << 32;
l |= mostSigBits >>> 32;
timestamp = l;
}
return l;
}
public int clockSequence()
{
if(version() != 1)
throw new UnsupportedOperationException("Not a time-based UUID");
if(sequence < 0)
sequence = (int)((leastSigBits & 0x3fff000000000000L) >>> 48);
return sequence;
}
public long node()
{
if(version() != 1)
throw new UnsupportedOperationException("Not a time-based UUID");
if(node < 0L)
node = leastSigBits & 0xffffffffffffL;
return node;
}
public String toString()
{
return (new StringBuilder()).append(digits(mostSigBits >> 32, 8)).append("-").append(digits(mostSigBits >> 16, 4))
.append("-").append(digits(mostSigBits, 4)).append("-")
.append(digits(leastSigBits >> 48, 4)).append("-").append(digits(leastSigBits, 12)).toString();
}
private static String digits(long l, int i)
{
long l1 = 1L << i * 4;
return Long.toHexString(l1 | l & l1 - 1L).substring(1);
}
public int hashCode()
{
if(hashCode == -1)
hashCode = (int)(mostSigBits >> 32 ^ mostSigBits ^ leastSigBits >> 32 ^ leastSigBits);
return hashCode;
}
public boolean equals(Object obj)
{
if(!(obj instanceof UUID))
return false;
if(((UUID)obj).variant() != variant())
{
return false;
} else
{
UUID uuid = (UUID)obj;
return mostSigBits == uuid.mostSigBits && leastSigBits == uuid.leastSigBits;
}
}
public int compareTo(UUID uuid)
{
return mostSigBits >= uuid.mostSigBits ? mostSigBits <= uuid.mostSigBits ?
leastSigBits >= uuid.leastSigBits ?
((byte) (((byte)(leastSigBits <= uuid.leastSigBits ? 0 : 1)))) : -1 : 1 : -1;
}
private void readObject(ObjectInputStream objectinputstream)
throws IOException, ClassNotFoundException
{
objectinputstream.defaultReadObject();
version = -1;
variant = -1;
timestamp = -1L;
sequence = -1;
node = -1L;
hashCode = -1;
}
public int compareTo(Object obj)
{
return compareTo((UUID)obj);
}
}
发表评论
-
javascript 打印指定区域
2010-11-18 16:34 1585javascript网页html 打印指定区域在一张网页里面, ... -
MyEclipse 8.5 开发环境配置,汉化,Aptana2.0插件,SVN 插件,Flex Builder 3/4 插件安装
2010-10-26 14:27 1546MyEclipse 8.5 开发环境配置,汉化,Aptana2 ... -
java读取properties文件
2010-10-11 13:43 767使用J2SE API读取Properties文件的六种方法 1 ... -
dbcp基本配置和重连配置
2010-09-28 09:29 2663最近在看一些dbcp的相 ... -
JAVA实现文件转移
2010-09-28 09:26 947/** * //1.从旧文件拷贝内容到新文件 ... -
Properties读取类
2010-09-25 14:06 948package cn.feigme.util; ... -
JAVA读写ftp
2010-09-21 16:41 3540import java.io.DataInputStream; ... -
apache tomcat mysql负载均衡和集群
2010-09-14 10:30 1691前言:公司开发了一个网站,估计最高在线人数是3万,并发人数最多 ... -
Flash Builder 4 正式版序列号
2010-09-01 15:51 3980江湖上又出现新的FlashBuilder(Flex4)序列号: ... -
利用 org.apache.commons.io.FileUtils快速读写文件
2010-08-17 10:33 2725利用 org.apache.commons.io.FileUt ... -
netbeans常用快捷键
2010-08-10 16:26 9071、Application应用程序的 ... -
ERWIN7.1注册码
2010-06-23 12:15 1359终于找到ERWIN7.1注册码,也可在ERWIN7.2上注册。 ... -
Java压缩文件zip
2010-06-21 09:42 1137可以使用jdk提供的java.util.zip包的类来进行文件 ... -
文件资源操作
2010-06-16 21:52 10871.访问文件资源 假设有一个文件地位于 ... -
Java Regex To Use
2010-06-16 21:46 807Java代码 /** * 得到 ... -
Java Random and Java Disabuse
2010-06-16 21:46 1124一、Random 1、创建Random ... -
java 线程池
2010-06-16 21:44 11561)threadpool.xml Java代码 ... -
使用ThreadLocal,隔离多个线程之间的共享冲突
2010-06-16 21:29 1542早在Java 1.2推出之时,Java平台中就引入了一个新的 ... -
MyEclipse下开发Web Service
2010-06-16 21:28 1525开发环境 Sun Java 5+ ... -
jexcel使用
2010-06-16 21:23 1458Java代码 package excel.jx ...
相关推荐
标题“jdk1.4生成guid”指的是在Java 1.4版本中生成全局唯一标识符(GUID)的技术。GUID,全称Globally Unique Identifier,是一种确保在全球范围内唯一标识对象的128位数字。在Java中,通常使用`java.util.UUID`类...
JAVA代码,生成不重复编码 jdk1.4无法使用java.util.UUID类
UUID="ce22eeca-ecde-4536-8cc2-ef0dc36d4a8c" IPADDR="192.168.1.101" ### NETMASK="255.255.255.0" ### GATEWAY="192.168.1.1" ### 1.3修改主机名和IP的映射关系 vim /etc/hosts 192.168.1.101 itcast ...
1-jdk安装 2-解压缩zk 3-安装zeromq configure: error: cannot link with -luuid, install uuid-dev rpm -ivh uuid-1.6.1-10.el6.x86_64.rpm rpm -ivh uuid-devel-1.6.1-10.el6.x86_64.rpm error: Failed ...
UUID=7c855882-bb4a-4c38-a463-400408700e6c DEVICE=ens160 ONBOOT=yes IPADDR=10.48.104.59 PREFIX=24 GATEWAY=10.48.104.1 DNS1=114.114.114.114 ``` 配置完成后,需要重启网络服务使更改生效: ```bash service ...
- 目前仅支持面向JDK1.4的Oracle10g驱动。 11. **UUID**: - 使用128位UUID算法生成字符串类型的标识符。 - 具有全局唯一性。 #### 四、联合主键 在Hibernate中,可以使用复合主键来表示具有多个字段作为主键...
- `UUID="ce22eeca-ecde-4536-8cc2-ef0dc36d4a8c"`:设置 UUID。 - `IPADDR="192.168.1.101"`:设置 IP 地址。 - `NETMASK="255.255.255.0"`:设置子网掩码。 - `GATEWAY="192.168.1.1"`:设置默认网关。 #####...
UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and Sequences) 6.1.4.5. 程序分配的标识符(Assigned Identifiers) 6.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and Sequences) 6.1.4.5. 程序分配的标识符(Assigned Identifiers) 6.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
5.5.2. Using JDK 5.0 Annotations 5.6. Generated Properties 5.7. Auxiliary Database Objects 6. Collection Mapping 6.1. Persistent collections 6.2. Collection mappings 6.2.1. Collection foreign keys ...
UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences) 5.1.4.5. 程序分配的标识符(Assigned Identifiers) 5.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences) 5.1.4.5. 程序分配的标识符(Assigned Identifiers) 5.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and Sequences) 6.1.4.5. 程序分配的标识符(Assigned Identifiers) 6.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and Sequences) 6.1.4.5. 程序分配的标识符(Assigned Identifiers) 6.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences) 5.1.4.5. 程序分配的标识符(Assigned Identifiers) 5.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences) 5.1.4.5. 程序分配的标识符(Assigned Identifiers) 5.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...
UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences) 5.1.4.5. 程序分配的标识符(Assigned Identifiers) 5.1.4.6. 触发器实现的主键生成器(Primary keys assigned by ...