We talked about different available versions for the variant type 2. In this tutorial, we learned about what a UUID is and its variants. It’ll also throw an UnsupportedOperationException for UUIDs other than of type v1. clockSequence(): It returns a 14-bit value constructed from the clock sequence field of a given UUID. This method will throw a UnsupportedOperationException when used with any other types of UUID.Ģ. timestamp(): Returns a timestamp value associated with a given v1 UUID. Other methods from the java.util package that comes handy when dealing with v1 UUIDs’ include:ġ. UUID uuid = Generators.timeBasedGenerator().generate() Note that this library provides various types of UUID generators. Let’s start by adding a dependency to java-uuid -generator in our POM: Int version = uuid.version() Working with a Time-Based UUID (v1): We can also query the variant and the version of a UUID: Long mostSignificantBits = uuid.getMostSignificantBits() īoth of these methods return a long value. Long leastSignificantBits = uuid.getLeastSignificantBits() getLeastSignificantBits() And getMostSignificantBits():Īs the name suggests, getLeastSignificantBits() and getMostSignificantBits() return the 64 least-significant and 64 most-significant bits respectively: Let’s cover a few other methods of the Java UUID class: 1. We can optionally use the equals() method for comparison as well. -1: if uuid1 is less than that of uuid2.So, we can use the compareTo() method to compare them:Īs we know, the compareTo() method returns: Java UUID class implements Comparable interface. It’ll throw an IllegalArgumentException for any invalid string passed in as an argument. With fromString(), we can create a UUID from a standard string representation: This method generates a v3 UUID (name-based). UUID uuid = UUID.nameUUIDFromBytes(byteArr) We can generate a UUID from a byte array using nameUUIDFromBytes(): It generates a v4 pseudo-random UUIDusing a cryptographically strong pseudo-random number generator: Let’s cover the methods in the Java UUID class which we can use to generate the UUID: 1. That means our UUID has a variant of 2.įor variant 2 UUIDs, there’re five different versions: Version Reserved, Microsoft Corporation backward compatibilityįor us, A = 8 (1000), so the first three MSBs are 100. Here, the value of A represents the variant and is determined by its first three MSBs (Most-Significant bits): MSB1 New UUID(long mostSigBits, long leastSigBits) UUID Versions and Variants:Ī variant determines the layout of the UUID. However, its constructor allows for generating any type of UUID: Java UUID class has a method for manipulating the Leach-Salz variant (variant 2). Another popular usage of UUID is for generating primary key values in the database. We can use UUID class for generating a random file name, a session or a transaction id. class in Java represents an immutable UUID. The standard representation of UUID is made up of hexadecimal digits:Īnd has 36 characters that include four hyphens ‘-‘. It’s also popularly known as GUID (Globally Unique Identifier). UUID (Universal Unique Identifier) represents a 128-bit long unique value.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |