Package org.kingdoms.utils.internal
Class FastUUID
java.lang.Object
org.kingdoms.utils.internal.FastUUID
A utility class for quickly and efficiently parsing
UUID instances from strings and writing UUID
instances as strings. The methods contained in this class are optimized for speed and to minimize garbage collection
pressure. In benchmarks, fromString(CharSequence) is a little more than 14 times faster than
UUID.fromString(String), and toString(UUID) is a little more than six times faster than
UUID.toString() when compared to the implementations in Java 8 and older. Under Java 9 and newer,
fromString(CharSequence) is about six times faster than the JDK implementation and toString(UUID)
does not offer any performance enhancements (or regressions!) the UUID.toString() will be faster.
Modified version by Crypto Morin
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic UUIDbytesToUUID(byte[] data) Copied from JDK15UUIDprivate constructor Anding (&) an integer with 0xFF leaves only the least significant byte, aka maskingstatic booleanAvoids the null andObject.getClass()check of the standardUUID.equals(Object)method for known objects.static UUIDfromString(CharSequence uuid) Parses a UUID from the given character sequence.static UUIDrandomUUID(Random random) Static factory to retrieve a type 4 (pseudo randomly generated) UUID.static StringReturns a string representation of the given UUID.
-
Field Details
-
ZERO
-
-
Method Details
-
randomUUID
Static factory to retrieve a type 4 (pseudo randomly generated) UUID. Copied from JDK15- Parameters:
random- the randomizer to use for generating the UUID.- Returns:
- A randomly generated
UUID
-
bytesToUUID
Copied from JDK15UUIDprivate constructor Anding (&) an integer with 0xFF leaves only the least significant byte, aka maskingleast significant bit (LSB): Example for a 32-bit int 011101010101010101010101010110110 ^^^^^^^^
0xff = 255 = 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 & 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 ------------------------------- 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 ^
-
equals
Avoids the null andObject.getClass()check of the standardUUID.equals(Object)method for known objects.Hopefully JVM will inline the method calls.
-
fromString
Parses a UUID from the given character sequence. The character sequence must represent a UUID as described inUUID.toString().- Parameters:
uuid- the character sequence from which to parse a UUID.- Returns:
- the UUID represented by the given character sequence.
- Throws:
IllegalArgumentException- if the given character sequence does not conform to the string representation as described inUUID.toString()
-
toString
Returns a string representation of the given UUID. The returned string is formatted as described inUUID.toString().- Parameters:
uuid- the UUID to represent as a string- Returns:
- a string representation of the given UUID
-