Java generation UUID

Source: Internet
Author: User

The UID meaning is a universal unique identifier (universally unique Identifier), which is a software construction standard and is also organized by the Open Software Foundation, OSF, in a distributed computing environment ( Distributed Computing Environment, DCE) part of the field. The purpose of the UUID is to allow all elements in a distributed system to have unique identification information without the need to specify the information through the central control terminal. In this way, everyone can create a UUID that does not clash with others. In such a case, there is no need to consider the name duplication problem when the database is established. Currently the most widely used UUID is Microsoft's globally Unique Identifiers (GUIDs), while other important applications are the Linux ext2/ext3 file system, LUKS encryption partition, GNOME, KDE, Mac OS X, and so on.

The following is an example of a specific build UUID:

ss[0]=====4cdbc040-657a-4847-b266-7e31d9e2c3d9,4cdbc040657a4847b2667e31d9e2c3d9 ss[1]=====72297c88-4260-4c05-9b05-d28bfb11d10b,72297c8842604c059b05d28bfb11d10b ss[2]=====6d513b6a-69bd-4f79-b94c-D65fc841ea95,6d513b6a69bd4f79b94cd65fc841ea95 ss[3]=====d897a7d3-87a3-4e38-9e0b-71013a6dbe4c,d897a7d387a34e389e0b71013a6dbe4c ss[4]=====5709f0ba-31e3-42bd-a28d-03485b257c94,5709f0ba31e342bda28d03485b257c94 ss[5]=====530fbb8c-eec9-48d1-ae1b-5f792daf09f3,530fbb8ceec948d1ae1b5f792daf09f3 ss[6]=====4bf07297-65b2-45ca-b905-6fc6f2f39158,4bf0729765b245cab9056fc6f2f39158 ss[7]=====6e5a0e85-b4a0-485f-be54-a758115317e1,6e5a0e85b4a0485fbe54a758115317e1 ss[8]=====245accec-3c12-4642-967f-e476cef558c4,245accec3c124642967fe476cef558c4 ss[9]=====ddd4b5a9-fecd-446c-bd78-63b70bb500a1,ddd4b5a9fecd446cbd7863b70bb500a1

As can be seen, the UUID refers to the number generated on a machine, which guarantees that all machines in the same time and space are unique. Typically, the platform provides the generated APIs. Based on standard calculations developed by the Open Software Foundation (OSF), Ethernet card addresses, nanosecond-seconds, chip ID codes, and many possible numbers are used

The UUID is composed of the following parts:

(1) The current date and time, the first part of the UUID is related to the time, if you generate a UUID after a few seconds to generate a UUID, then the first part is different, the rest is the same.

(2) Clock sequence

(3) Globally unique IEEE machine identification number, if there is a network card, from the network card MAC address obtained, no network card is obtained in other ways.

The only drawback to the UUID is that the resulting string will be longer. The most common use of UUID for this standard is the GUID of Microsoft (Globals Unique Identifiers). In ColdFusion, the UUID can be easily generated using the Createuuid () function in the form of: Xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16), where each x is 0-9 or a-f A hexadecimal number within the range. The standard UUID format is: xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12), which can be converted from cflib download CreateGUID () UDF.

The benefits of using UUID are reflected in distributed software systems (e.g. DCE/RPC, Com+,corba), which ensures that the identities generated by each node are not duplicated, and that the benefits of the UUID will be more pronounced with the development of integrated technologies such as Web services. Depending on the specific mechanism used, the UUID must not only be guaranteed to be different from each other, or at least be very different from any other generic unique identifiers that were generated before the 3400 AD.

The universal unique identifier can also be used to point to most possible objects. Microsoft and some other software companies tend to use the globally unique identifier (GUID), which is a type of universal unique identifier that can be used to point to the Building object module object and other software components. The first universal unique identifier is a component created in the Snare computer system (NCS) and subsequently become a distributed computing environment (DCE) of the Open Software Foundation (OSF).

 PackageCom.cib.cap4j.cfn.util;Importjava.net.InetAddress;ImportJava.security.SecureRandom;/*** UUID Tool class, used to generate a unique tag number uuid*/ Public classUuidgenerator {Private StaticSecureRandom seeder_static =NULL; Private Static byte[] ADDRESS =NULL; Private StaticString mid_value_static =NULL; PrivateString Midvalue =NULL; PrivateSecureRandom Seeder =NULL; Static {        Try{ADDRESS=inetaddress.getlocalhost (). getaddress (); StringBuffer Buffer=NewStringBuffer (8); Buffer.append (Tohex, ToInt (ADDRESS),8)); Mid_value_static=buffer.tostring (); Seeder_static=NewSecureRandom ();        Seeder_static.nextint (); } Catch(Exception ex) {ex.printstacktrace (); }    }     PublicUuidgenerator () {StringBuffer buffer=NewStringBuffer (16);        Buffer.append (mid_value_static); Buffer.append (Tohex (System.identityhashcode ( This), 8)); Midvalue=buffer.tostring (); Seeder=NewSecureRandom ();    Seeder.nextint (); }    /*** This method is used to generate a 32-bit unique token string * *@paramobj * Pass in a Reference object *@return     */     Public StaticString Generate (Object obj) {stringbuffer uid=NewStringBuffer (32); //get the system time        LongCurrenttimemillis =System.currenttimemillis (); Uid.append (Tohex (int) (Currenttimemillis & -1l), 8)); //get the Internet addressuid.append (mid_value_static); //get the object hash valueUid.append (Tohex (System.identityhashcode (obj), 8)); //get the random numberUid.append (Tohex (Getrandom (), 8)); returnuid.tostring (); }    /*** This method is used to generate a 32-bit string unique tag*/     PublicString Generate () {StringBuffer uid=NewStringBuffer (32); //get the system time        LongCurrenttimemillis =System.currenttimemillis (); Uid.append (Tohex (int) (Currenttimemillis & -1l), 8)); //get the Internet addressuid.append (Midvalue); //get the random numberUid.append (Tohex (Seeder.nextint (), 8)); returnuid.tostring (); }    Private StaticString Tohex (intValueintlength) {        Char[] hexdigits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; StringBuffer Buffer=Newstringbuffer (length); intShift = Length-1 << 2;  for(inti =-1; ++i <length;) {Buffer.append (Hexdigits[value>> Shift & 0XF]); Value<<= 4; }        returnbuffer.tostring (); }    Private Static intToInt (byte[] bytes) {        intValue = 0;  for(inti =-1; ++i <bytes.length;) {Value<<= 8; Value|=Bytes[i]; }        returnvalue; }    Private Static synchronized intGetrandom () {returnSeeder_static.nextint (); }}

Java generation UUID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.