Class UuidBuilder


  • public class UuidBuilder
    extends Object
    A UUID builder.

    Usage:

    
     SecureRandom random = new SecureRandom();
     UUID uuid = new UuidBuilder(4) // sets version 4 (random-based)
     		.put(random.nextLong()) // put the most significant 64 bits
     		.put(random.nextLong()) // put the least significant 64 bits
     		.build(); // return the built UUID
     
    • Constructor Detail

      • UuidBuilder

        public UuidBuilder()
        Instantiates a new builder without a version number.
      • UuidBuilder

        public UuidBuilder​(int version)
        Instantiates a new builder with a version number.
        Parameters:
        version - a value between 0 and 15
    • Method Detail

      • put

        public UuidBuilder put​(long value)
        Puts 8 bytes containing the given long.
        Parameters:
        value - a long value
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there are fewer than 8 bytes remaining
      • put

        public UuidBuilder put​(int value)
        Puts 4 bytes containing the given int.
        Parameters:
        value - an int value
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there are fewer than 4 bytes remaining
      • put

        public UuidBuilder put​(short value)
        Puts 2 bytes containing the given short.
        Parameters:
        value - a short value
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there are fewer than 2 bytes remaining
      • put

        public UuidBuilder put​(byte value)
        Puts the given byte.
        Parameters:
        value - a byte value
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there are fewer than 1 bytes remaining
      • put

        public UuidBuilder put​(byte[] array)
        Puts the given byte array.
        Parameters:
        value - a byte array
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there are fewer bytes remaining than the array length
      • build

        public UUID build()
        Builds a UUID after all 16 bytes are filled.

        This method ends the use of a builder.

        Successive calls will always return the same UUID value.

        Note: this method overrides bits 48 through 51 (version field) and bits 52 through 63 (variant field), 6 bits total, to comply the UUID specification.

        Throws:
        BufferUnderflowException - If there are bytes remaining to be filled