Class UuidComparator

  • All Implemented Interfaces:
    Comparator<UUID>

    public final class UuidComparator
    extends Object
    implements Comparator<UUID>
    Comparator for UUIDs.

    The default static method compares two time-based UUIDs by comparing the time stamps first and then comparing the least significant bits as unsigned 64-bit integers. If both UUIDs are not time-based then it compares them as unsigned 128-bit integers.

    The opaque static method compares two UUIDs as unsigned 128-bit integers. It's the same as lexicographic sorting of UUID canonical strings.

    • Method Detail

      • defaultCompare

        public static int defaultCompare​(UUID uuid1,
                                         UUID uuid2)
        Compares two UUIDs.

        The default static method compares two time-based UUIDs by comparing the time stamps first and then comparing the least significant bits as unsigned 64-bit integers. If both UUIDs are not time-based then it compares them as unsigned 128-bit integers.

        The first of two UUIDs is greater than the second if the time stamp is greater for the first UUID. If the time stamps are equal, the first of two UUIDs is greater than the second if the most significant byte in which they differ is greater for the first UUID.

        It can be useful for these reasons:

        1. UUID.compareTo(UUID) doesn't work well for time-based UUIDs;
        2. UUID.compareTo(UUID) can lead to unexpected behavior due to signed long comparison;
        3. UUID.compareTo(UUID) throws NullPointerException if a null UUID is given.
        Parameters:
        uuid1 - a UUID
        uuid2 - another UUID
        Returns:
        -1, 0 or 1 as u1 is less than, equal to, or greater than u2
      • opaqueCompare

        public static int opaqueCompare​(UUID uuid1,
                                        UUID uuid2)
        Compares two UUIDs.

        The opaque static method compares two UUIDs as unsigned 128-bit integers. It's the same as lexicographic sorting of UUID canonical strings.

        The first of two UUIDs is greater than the second if the most significant byte in which they differ is greater for the first UUID.

        The opaque method is faster than the default method as it does not check the UUID version.

        It's referred to as "opaque" just because it works like a "blind byte-to-byte comparison".

        It can be useful for these reasons:

        1. UUID.compareTo(UUID) can lead to unexpected behavior due to signed long comparison;
        2. UUID.compareTo(UUID) throws NullPointerException if a null UUID is given.
        Parameters:
        uuid1 - a UUID
        uuid2 - another UUID
        Returns:
        -1, 0 or 1 as u1 is less than, equal to, or greater than u2
      • compare

        public int compare​(UUID uuid1,
                           UUID uuid2)
        Compares two UUIDs.
        Specified by:
        compare in interface Comparator<UUID>
        Parameters:
        uuid1 - a UUID
        uuid2 - another UUID
        Returns:
        -1, 0 or 1 as u1 is less than, equal to, or greater than u2
        See Also:
        defaultCompare(UUID, UUID)