Skip to content

3.0. Library utilities

Fabio Lima edited this page Sep 24, 2022 · 3 revisions

UuidValidador

A much faster validator than regular expression.

Validating UUIDs

isValid()

Checks if the UUID string is valid.

String uuid = // ...
UuidValidator.isValid(uuid);

UuidUtil

Some tools for UUIDs.

Changing UUIDs

setVersion()

Chenges the version number of a UUID.

UUID v1 = // ...
UUID v4 = UuidUtil.setVersion(v1, 4);

Checking UUIDs

isNil()

Checks if the UUID is a NIL UUID.

UUID uuid = // ...
UuidUtil.isNil(uuid);

isTimeBased()

Checks if the UUID is a UUIDv1.

UUID uuid = // ...
UuidUtil.isTimeBased(uuid);

isRandomBased()

Checks if the UUID is a UUIDv4.

UUID uuid = // ...
UuidUtil.isRandomBased(uuid);

Inspecting UUIDs

extractInstant()

Extracts the creation instant of a UUID.

UUID uuid = /...
Instant instant = UuidUtil.extractInstant(uuid);

extractNodeIdentifier()

Extracts the node identifier of a UUID.

UUID uuid = /...
long nodeid = UuidUtil.extractNodeIdentifier(uuid);

CombUtil

Some tools for COMBs.

Inspecting COMBs

extractPrefixInstant()

Extracts the creation instant of a Prefix COMB.

UUID comb = /...
Instant instant = CombUtil.extractPrefixInstant(comb);

extractSuffixInstant()

Extracts the creation instant of a Suffix COMB.

UUID comb = /...
Instant instant = CombUtil.extractSuffixInstant(comb);

UuidComparator

A better comparator for UUIDs than UUID#compareTo().

Sorting UUIDs

UuidComparator()

Sorts a list of objects as UUIDv1 or as unsigned 128-bit integers.

List<UUID> list = // ...
list.sort(new UuidComparator());

getDefaultInstance()

Sorts a list of objects as UUIDv1 or as unsigned 128-bit integers.

List<UUID> list = // ...
list.sort(UuidComparator.getDefaultInstance());

getOpaqueInstance()

Sorts a list of objects as unsigned 128-bit integers.

List<UUID> list = // ...
list.sort(UuidComparator.getOpaqueInstance());

Comparing UUIDs

defaultCompare()

Compares two objects as UUIDv1 or as unsigned 128-bit integers.

UUID uuid1 = // ...
UUID uuid2 = // ...
int result = UuidComparator.defaultCompare(uuid1, uuid2);

opaqueCompare()

Compares two objects as unsigned 128-bit integers.

UUID uuid1 = // ...
UUID uuid2 = // ...
int result = UuidComparator.opaqueCompare(uuid1, uuid2);

MachineId

Collects the hostname, MAC address and IP address of the current machine.

The calculated identifier is a truncated SHA-256 hash of the collected data.

Identifying machines

getMachineId()

Calculates a long ID for the current machine.

// returns 0x7bc3cfd7844f46ad
long id = MachineId.getMachineId();

getMachineUuid()

Calculates a UUID for the current machine.

// returns 7bc3cfd7-844f-46ad-51a9-1aa22d3c427a
UUID uuid = MachineId.getMachineUuid();

getMachineString()

Generates an identifying string for the current machine.

// returns "hostname FF-FF-FF-FF-FF-FF 255.255.255.255"
String string = MachineId.getMachineString();