日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

uuid java 重复_Java中使用UUID工具类生成唯一标志防止重复

發布時間:2025/3/12 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uuid java 重复_Java中使用UUID工具类生成唯一标志防止重复 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.util.Random;

import java.util.concurrent.ThreadLocalRandom;/**

* 提供通用唯一識別碼(universally unique identifier)(UUID)實現

**/

public final class UUID implements java.io.Serializable, Comparable{private static final long serialVersionUID = -1185015143654744140L;/**

* SecureRandom 的單例

**/

private static classHolder

{static final SecureRandom numberGenerator =getSecureRandom();

}/** 此UUID的最高64有效位*/

private final longmostSigBits;/** 此UUID的最低64有效位*/

private final longleastSigBits;/**

* 私有構造

*

* @param data 數據*/

private UUID(byte[] data)

{long msb = 0;long lsb = 0;

assert data.length== 16 : "data must be 16 bytes in length";for (int i = 0; i < 8; i++)

{

msb= (msb << 8) | (data[i] & 0xff);

}for (int i = 8; i < 16; i++)

{

lsb= (lsb << 8) | (data[i] & 0xff);

}this.mostSigBits =msb;this.leastSigBits =lsb;

}/**

* 使用指定的數據構造新的 UUID。

*

* @param mostSigBits 用于 {@code UUID} 的最高有效 64 位

* @param leastSigBits 用于 {@code UUID} 的最低有效 64 位*/

public UUID(long mostSigBits, longleastSigBits)

{this.mostSigBits =mostSigBits;this.leastSigBits =leastSigBits;

}/**

* 獲取類型 4(偽隨機生成的)UUID 的靜態工廠。 使用加密的本地線程偽隨機數生成器生成該 UUID。

*

* @return 隨機生成的 {@code UUID}*/

public staticUUID fastUUID()

{return randomUUID(false);

}/**

* 獲取類型 4(偽隨機生成的)UUID 的靜態工廠。 使用加密的強偽隨機數生成器生成該 UUID。

*

* @return 隨機生成的 {@code UUID}*/

public staticUUID randomUUID()

{return randomUUID(true);

}/**

* 獲取類型 4(偽隨機生成的)UUID 的靜態工廠。 使用加密的強偽隨機數生成器生成該 UUID。

*

* @param isSecure 是否使用{@link SecureRandom}如果是可以獲得更安全的隨機碼,否則可以得到更好的性能

* @return 隨機生成的 {@code UUID}*/

public staticUUID randomUUID(boolean isSecure)

{

final Random ng= isSecure ?Holder.numberGenerator : getRandom();byte[] randomBytes = new byte[16];

ng.nextBytes(randomBytes);

randomBytes[6] &= 0x0f; /*clear version*/randomBytes[6] |= 0x40; /*set to version 4*/randomBytes[8] &= 0x3f; /*clear variant*/randomBytes[8] |= 0x80; /*set to IETF variant*/

return newUUID(randomBytes);

}/**

* 根據指定的字節數組獲取類型 3(基于名稱的)UUID 的靜態工廠。

*

* @param name 用于構造 UUID 的字節數組。

*

* @return 根據指定數組生成的 {@code UUID}*/

public static UUID nameUUIDFromBytes(byte[] name)

{

MessageDigest md;try{

md= MessageDigest.getInstance("MD5");

}catch(NoSuchAlgorithmException nsae)

{throw new InternalError("MD5 not supported");

}byte[] md5Bytes =md.digest(name);

md5Bytes[6] &= 0x0f; /*clear version*/md5Bytes[6] |= 0x30; /*set to version 3*/md5Bytes[8] &= 0x3f; /*clear variant*/md5Bytes[8] |= 0x80; /*set to IETF variant*/

return newUUID(md5Bytes);

}/**

* 根據 {@link #toString()} 方法中描述的字符串標準表示形式創建{@code UUID}。

*

* @param name 指定 {@code UUID} 字符串

* @return 具有指定值的 {@code UUID}

* @throws IllegalArgumentException 如果 name 與 {@link #toString} 中描述的字符串表示形式不符拋出此異常

**/

public staticUUID fromString(String name)

{

String[] components= name.split("-");if (components.length != 5)

{throw new IllegalArgumentException("Invalid UUID string:" +name);

}for (int i = 0; i < 5; i++)

{

components[i]= "0x" +components[i];

}long mostSigBits = Long.decode(components[0]).longValue();

mostSigBits<<= 16;

mostSigBits|= Long.decode(components[1]).longValue();

mostSigBits<<= 16;

mostSigBits|= Long.decode(components[2]).longValue();long leastSigBits = Long.decode(components[3]).longValue();

leastSigBits<<= 48;

leastSigBits|= Long.decode(components[4]).longValue();return newUUID(mostSigBits, leastSigBits);

}/**

* 返回此 UUID 的 128 位值中的最低有效 64 位。

*

* @return 此 UUID 的 128 位值中的最低有效 64 位。*/

public longgetLeastSignificantBits()

{returnleastSigBits;

}/**

* 返回此 UUID 的 128 位值中的最高有效 64 位。

*

* @return 此 UUID 的 128 位值中最高有效 64 位。*/

public longgetMostSignificantBits()

{returnmostSigBits;

}/**

* 與此 {@code UUID} 相關聯的版本號. 版本號描述此 {@code UUID} 是如何生成的。

*

* 版本號具有以下含意:

*

*

1 基于時間的 UUID

*

2 DCE 安全 UUID

*

3 基于名稱的 UUID

*

4 隨機生成的 UUID

*

*

* @return 此 {@code UUID} 的版本號*/

public intversion()

{//Version is bits masked by 0x000000000000F000 in MS long

return (int) ((mostSigBits >> 12) & 0x0f);

}/**

* 與此 {@code UUID} 相關聯的變體號。變體號描述 {@code UUID} 的布局。

*

* 變體號具有以下含意:

*

*

0 為 NCS 向后兼容保留

*

2 IETF?RFC?4122(Leach-Salz'" DESIGNTIMESP=518>http://www.ietf.org/rfc/rfc4122.txt">IETF?RFC?4122(Leach-Salz), 用于此類

*

6 保留,微軟向后兼容

*

7 保留供以后定義使用

*

*

* @return 此 {@code UUID} 相關聯的變體號*/

public intvariant()

{//This field is composed of a varying number of bits.//0 - - Reserved for NCS backward compatibility//1 0 - The IETF aka Leach-Salz variant (used by this class)//1 1 0 Reserved, Microsoft backward compatibility//1 1 1 Reserved for future definition.

return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63));

}/**

* 與此 UUID 相關聯的時間戳值。

*

*

* 60 位的時間戳值根據此 {@code UUID} 的 time_low、time_mid 和 time_hi 字段構造。

* 所得到的時間戳以 100 毫微秒為單位,從 UTC(通用協調時間) 1582 年 10 月 15 日零時開始。

*

*

* 時間戳值僅在在基于時間的 UUID(其 version 類型為 1)中才有意義。

* 如果此 {@code UUID} 不是基于時間的 UUID,則此方法拋出 UnsupportedOperationException。

*

* @throws UnsupportedOperationException 如果此 {@code UUID} 不是 version 為 1 的 UUID。*/

public longtimestamp() throws UnsupportedOperationException

{

checkTimeBase();return (mostSigBits & 0x0FFFL) << 48// | ((mostSigBits >> 16) & 0x0FFFFL) << 32// | mostSigBits >>> 32;

}/**

* 與此 UUID 相關聯的時鐘序列值。

*

*

* 14 位的時鐘序列值根據此 UUID 的 clock_seq 字段構造。clock_seq 字段用于保證在基于時間的 UUID 中的時間唯一性。

*

* {@code clockSequence} 值僅在基于時間的 UUID(其 version 類型為 1)中才有意義。 如果此 UUID 不是基于時間的 UUID,則此方法拋出

* UnsupportedOperationException。

*

* @return 此 {@code UUID} 的時鐘序列

*

* @throws UnsupportedOperationException 如果此 UUID 的 version 不為 1*/

public intclockSequence() throws UnsupportedOperationException

{

checkTimeBase();return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48);

}/**

* 與此 UUID 相關的節點值。

*

*

* 48 位的節點值根據此 UUID 的 node 字段構造。此字段旨在用于保存機器的 IEEE 802 地址,該地址用于生成此 UUID 以保證空間唯一性。

*

* 節點值僅在基于時間的 UUID(其 version 類型為 1)中才有意義。

* 如果此 UUID 不是基于時間的 UUID,則此方法拋出 UnsupportedOperationException。

*

* @return 此 {@code UUID} 的節點值

*

* @throws UnsupportedOperationException 如果此 UUID 的 version 不為 1*/

public longnode() throws UnsupportedOperationException

{

checkTimeBase();return leastSigBits & 0x0000FFFFFFFFFFFFL;

}/**

* 返回此{@code UUID} 的字符串表現形式。

*

*

* UUID 的字符串表示形式由此 BNF 描述:

*

*

* {@code

* UUID = ----

* time_low = 4*

* time_mid = 2*

* time_high_and_version = 2*

* variant_and_sequence = 2*

* node = 6*

* hexOctet =

* hexDigit = [0-9a-fA-F]

* }

*

*

*

*

* @return 此{@code UUID} 的字符串表現形式

* @see #toString(boolean)*/@OverridepublicString toString()

{return toString(false);

}/**

* 返回此{@code UUID} 的字符串表現形式。

*

*

* UUID 的字符串表示形式由此 BNF 描述:

*

*

* {@code

* UUID = ----

* time_low = 4*

* time_mid = 2*

* time_high_and_version = 2*

* variant_and_sequence = 2*

* node = 6*

* hexOctet =

* hexDigit = [0-9a-fA-F]

* }

*

*

*

*

* @param isSimple 是否簡單模式,簡單模式為不帶'-'的UUID字符串

* @return 此{@code UUID} 的字符串表現形式*/

publicString toString(boolean isSimple)

{

final StringBuilder builder= new StringBuilder(isSimple ? 32 : 36);//time_low

builder.append(digits(mostSigBits >> 32, 8));if (false ==isSimple)

{

builder.append('-');

}//time_mid

builder.append(digits(mostSigBits >> 16, 4));if (false ==isSimple)

{

builder.append('-');

}//time_high_and_version

builder.append(digits(mostSigBits, 4));if (false ==isSimple)

{

builder.append('-');

}//variant_and_sequence

builder.append(digits(leastSigBits >> 48, 4));if (false ==isSimple)

{

builder.append('-');

}//node

builder.append(digits(leastSigBits, 12));returnbuilder.toString();

}/**

* 返回此 UUID 的哈希碼。

*

* @return UUID 的哈希碼值。*/@Overridepublic inthashCode()

{long hilo = mostSigBits ^leastSigBits;return ((int) (hilo >> 32)) ^ (int) hilo;

}/**

* 將此對象與指定對象比較。

*

* 當且僅當參數不為 {@code null}、而是一個 UUID 對象、具有與此 UUID 相同的 varriant、包含相同的值(每一位均相同)時,結果才為 {@code true}。

*

* @param obj 要與之比較的對象

*

* @return 如果對象相同,則返回 {@code true};否則返回 {@code false}*/@Overridepublicboolean equals(Object obj)

{if ((null == obj) || (obj.getClass() != UUID.class))

{return false;

}

UUID id=(UUID) obj;return (mostSigBits == id.mostSigBits && leastSigBits ==id.leastSigBits);

}//Comparison Operations

/**

* 將此 UUID 與指定的 UUID 比較。

*

*

* 如果兩個 UUID 不同,且第一個 UUID 的最高有效字段大于第二個 UUID 的對應字段,則第一個 UUID 大于第二個 UUID。

*

* @param val 與此 UUID 比較的 UUID

*

* @return 在此 UUID 小于、等于或大于 val 時,分別返回 -1、0 或 1。

**/@Overridepublic intcompareTo(UUID val)

{//The ordering is intentionally set up so that the UUIDs//can simply be numerically compared as two numbers

return (this.mostSigBits < val.mostSigBits ? -1 : // (this.mostSigBits > val.mostSigBits ? 1 : // (this.leastSigBits < val.leastSigBits ? -1 : // (this.leastSigBits > val.leastSigBits ? 1 : // 0))));

}//-------------------------------------------------------------------------------------------------------------------//Private method start

/**

* 返回指定數字對應的hex值

*

* @param val 值

* @param digits 位

* @return 值*/

private static String digits(long val, intdigits)

{long hi = 1L << (digits * 4);return Long.toHexString(hi | (val & (hi - 1))).substring(1);

}/**

* 檢查是否為time-based版本UUID*/

private voidcheckTimeBase()

{if (version() != 1)

{throw new UnsupportedOperationException("Not a time-based UUID");

}

}/**

* 獲取{@link SecureRandom},類提供加密的強隨機數生成器 (RNG)

*

* @return {@link SecureRandom}*/

public staticSecureRandom getSecureRandom()

{try{return SecureRandom.getInstance("SHA1PRNG");

}catch(NoSuchAlgorithmException e)

{throw newUtilException(e);

}

}/**

* 獲取隨機數生成器對象

* ThreadLocalRandom是JDK 7之后提供并發產生隨機數,能夠解決多個線程發生的競爭爭奪。

*

* @return {@link ThreadLocalRandom}*/

public staticThreadLocalRandom getRandom()

{returnThreadLocalRandom.current();

}

}

總結

以上是生活随笔為你收集整理的uuid java 重复_Java中使用UUID工具类生成唯一标志防止重复的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。