- java.lang.Object
-
- com.github.f4b6a3.uuid.util.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.
-
-
Constructor Summary
Constructors Constructor Description UuidComparator()Creates a default implementation ofUuidComparator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompare(UUID uuid1, UUID uuid2)Compares two UUIDs.static intdefaultCompare(UUID uuid1, UUID uuid2)Compares two UUIDs.static UuidComparatorgetDefaultInstance()Returns a default implementation ofUuidComparator.static UuidComparatorgetOpaqueInstance()Returns an opaque implementation ofUuidComparator.static intopaqueCompare(UUID uuid1, UUID uuid2)Compares two UUIDs.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
-
-
-
Constructor Detail
-
UuidComparator
public UuidComparator()
Creates a default implementation ofUuidComparator.- See Also:
defaultCompare(UUID, UUID)
-
-
Method Detail
-
getDefaultInstance
public static UuidComparator getDefaultInstance()
Returns a default implementation ofUuidComparator.- Returns:
- a
UuidComparator - See Also:
defaultCompare(UUID, UUID)
-
getOpaqueInstance
public static UuidComparator getOpaqueInstance()
Returns an opaque implementation ofUuidComparator.- Returns:
- a opaque
UuidComparator - See Also:
opaqueCompare(UUID, UUID)
-
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:
UUID.compareTo(UUID)doesn't work well for time-based UUIDs;UUID.compareTo(UUID)can lead to unexpected behavior due to signedlongcomparison;UUID.compareTo(UUID)throwsNullPointerExceptionif anullUUID is given.
- Parameters:
uuid1- aUUIDuuid2- anotherUUID- Returns:
- -1, 0 or 1 as
u1is less than, equal to, or greater thanu2
-
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:
UUID.compareTo(UUID)can lead to unexpected behavior due to signedlongcomparison;UUID.compareTo(UUID)throwsNullPointerExceptionif anullUUID is given.
- Parameters:
uuid1- aUUIDuuid2- anotherUUID- Returns:
- -1, 0 or 1 as
u1is less than, equal to, or greater thanu2
-
compare
public int compare(UUID uuid1, UUID uuid2)
Compares two UUIDs.- Specified by:
comparein interfaceComparator<UUID>- Parameters:
uuid1- aUUIDuuid2- anotherUUID- Returns:
- -1, 0 or 1 as
u1is less than, equal to, or greater thanu2 - See Also:
defaultCompare(UUID, UUID)
-
-