📄 _uid.java
字号:
package com.tocow.utilities;
public final class _uid {
/**
* 常量定义
*/
protected static long INTERVAL = 20;
/**
* 两次取标识的间隔时间(以毫秒计算) 属性定义
*/
protected static int ramInt;
/**
* 随机生成的主机标识(最好改为从IP地址或网卡等特殊标识获取)
*/
protected static String uniqueHost = "";
/**
* 随机生成的主机标识,IP地址获取
*/
protected static short lastCount = Short.MAX_VALUE;
/**
* 上次生成标识的计数器
*/
protected static long lastTime = System.currentTimeMillis();
/**
* 上次生成标识的时间
*/
protected static short count;
/**
* 本次生成标识的计数器
*/
protected static long time;
/**
* 本次生成标识的时间 Function: 私有构造方法用于阻止创建本类的实例对象。
*
* @roseuid 3E826B4A0398
*/
private _uid() {
}
/**
* Funciton: 取一个唯一标识。 Return: 唯一标识
*
* @return java.lang.String
* @roseuid 3E8274A90204
*/
public static synchronized String getId() {
// 如果计数器用完则重置状态,否则直接以计数器和当前时间作用标识
if (lastCount == Short.MIN_VALUE) {
for (;;) {
// 取新的时间
time = System.currentTimeMillis();
// 如果与上次取标识时间差不足则等待一段时间
if (time <= lastTime + INTERVAL) {
try {
Thread.currentThread().sleep(INTERVAL);
} catch (java.lang.InterruptedException exc) {
} // 不必处理异常
continue;
}
// 重新初始化取标识的状态
lastTime = time;
lastCount = Short.MAX_VALUE;
break;
}
} else {
time = System.currentTimeMillis();
lastTime = time;
}
count = lastCount--;
// 返回结果
String id = Long.toString(time, 16);
if (uniqueHost.equals("")) {
ramInt = (new Object()).hashCode();
} else {
ramInt = (new String(uniqueHost)).hashCode();
}
id += ":" + Integer.toString(count, 16);
id += ":" + Integer.toString(ramInt, 16);
if (id.toUpperCase().length() <= 24)
return id.toUpperCase();
else
return id.toUpperCase().substring(0, 24);
}
/**
* Funciton: 取一个唯一标识。 Return: 唯一标识
*
* @param uHost
* @return java.lang.String
* @roseuid 3E8274A9024A
*/
public static synchronized String getId(String uHost) {
if (uHost == null)
uHost = "";
if (uHost.length() > 7) {
// 可以在这里改变uniqueHost的值
uniqueHost = uHost;
}
return getId();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -