Class FastUUID

java.lang.Object
org.kingdoms.utils.internal.FastUUID

public final class FastUUID extends Object
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 Details

    • ZERO

      public static final UUID ZERO
  • Method Details

    • randomUUID

      public static UUID randomUUID(Random random)
      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

      public static UUID bytesToUUID(byte[] data)
      Copied from JDK15 UUID private constructor Anding (&) an integer with 0xFF leaves only the least significant byte, aka masking

      least 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 ^

      Parameters:
      data - a 16 long byte array to construct a UUID.
      Returns:
      a UUID created by the bytes.
    • equals

      public static boolean equals(UUID first, UUID other)
      Avoids the null and Object.getClass() check of the standard UUID.equals(Object) method for known objects.

      Hopefully JVM will inline the method calls.

    • fromString

      public static UUID fromString(CharSequence uuid)
      Parses a UUID from the given character sequence. The character sequence must represent a UUID as described in UUID.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 in UUID.toString()
    • toString

      public static String toString(UUID uuid)
      Returns a string representation of the given UUID. The returned string is formatted as described in UUID.toString().
      Parameters:
      uuid - the UUID to represent as a string
      Returns:
      a string representation of the given UUID