Class UUIDUtil
java.lang.Object
space.arim.omnibus.util.UUIDUtil
Utility class for manipulating valid UUIDs. Allows conversions between
All methods do not permit null, and may throw
A Note on Preconditions
So that it may be used to operate on large volumes of data, this class does not check comprehensive preconditions. It assumes mostly valid representations. Callers are encouraged to validate their own input if necessary.
UUID Forms
This class recognises 3 forms of UUIDs besides
1. Full UUID strings. This the common string based representation of a UUID as defined by
2. Short UUID strings. These are the same as full UUID strings except that they are not hyphenated.
3. Byte arrays. These must be 16 bytes in length. They convey the same data as a pair of
Methods are provided for efficient conversion between:
1. Full UUID strings and short UUID strings.
2. Short UUID strings and
3. Byte arrays and
Where applicable, conversion methods are designed to be at least as performant as more roundabout approaches. For example,
UUIDs, full UUID strings, short UUID strings, and byte arrays. All methods do not permit null, and may throw
NullPointerException
otherwise. A Note on Preconditions
So that it may be used to operate on large volumes of data, this class does not check comprehensive preconditions. It assumes mostly valid representations. Callers are encouraged to validate their own input if necessary.
UUID Forms
This class recognises 3 forms of UUIDs besides
java.util.UUID: 1. Full UUID strings. This the common string based representation of a UUID as defined by
UUID.toString(). UUIDs in this form may be converted
back to a java.util.UUID via the JDK's
UUID.fromString(String). 2. Short UUID strings. These are the same as full UUID strings except that they are not hyphenated.
3. Byte arrays. These must be 16 bytes in length. They convey the same data as a pair of
long values. Methods are provided for efficient conversion between:
1. Full UUID strings and short UUID strings.
2. Short UUID strings and
java.util.UUID. 3. Byte arrays and
java.util.UUID. Where applicable, conversion methods are designed to be at least as performant as more roundabout approaches. For example,
fromShortString(String) should be faster than combining
expandShortString(String) and UUID.fromString(String).- Author:
- A248
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcontractFullString(String fullUuid) Contracts the full form of a UUID string to its shortened form.static StringexpandShortString(String shortUuid) Expands a shortened version of a UUID to the full string form.static UUIDfromByteArray(byte[] byteArray) Reads a UUID from a byte array.static UUIDfromByteArray(byte[] byteArray, int offset) Reads a UUID from a byte array with a specified offset.static UUIDfromShortString(String shortUuid) Converts a short form uuid string to aUUID.static byte[]toByteArray(UUID uuid) Creates a byte array and writes a UUID to it.static voidtoByteArray(UUID uuid, byte[] byteArray, int offset) Writes a UUID to a byte array at the specified offset.static StringtoShortString(UUID uuid) Converts aUUIDto its short form string representation.
-
Method Details
-
expandShortString
Expands a shortened version of a UUID to the full string form. Inverse operation ofcontractFullString(String)- Parameters:
shortUuid- the short uuid string- Returns:
- the full uuid string
- Throws:
IllegalArgumentException- ifshortUuidis not of length 32
-
contractFullString
Contracts the full form of a UUID string to its shortened form. This is the inverse operation ofexpandShortString(String), and should be identical to replacing/removing all hyphens in the full uuid string, e.g.fullUuid.replace("-", "")for a valid full UUID string calledfullUuid.- Parameters:
fullUuid- the full uuid string- Returns:
- the short uuid string
- Throws:
IllegalArgumentException- iffullUuidis not of length 36
-
toShortString
Converts aUUIDto its short form string representation. This is the inverse operation offromShortString(String).
The returned string will always be of length 32.- Parameters:
uuid- the UUID- Returns:
- the short uuid string
-
fromShortString
Converts a short form uuid string to aUUID. This is the inverse operation oftoShortString(UUID)- Parameters:
shortUuid- the short uuid string- Returns:
- the UUID
- Throws:
IllegalArgumentException- ifshortUuidis not of length 32NumberFormatException- ifshortUuidis not a valid short uuid string
-
toByteArray
Creates a byte array and writes a UUID to it. This would be the inverse operation offromByteArray(byte[])- Parameters:
uuid- the UUID- Returns:
- the byte array, will always be length 16
-
toByteArray
Writes a UUID to a byte array at the specified offset. This would be the inverse operation offromByteArray(byte[], int)- Parameters:
uuid- the UUIDbyteArray- the byte array to write to, must be at least of length (offset + 16)offset- the offset in the byte array after which to write bytes- Throws:
IndexOutOfBoundsException- if the byte array is not of the right size
-
fromByteArray
Reads a UUID from a byte array. This is the inverse operation oftoByteArray(UUID).- Parameters:
byteArray- the byte array to read from, must be at least of length 16- Returns:
- the UUID
- Throws:
IndexOutOfBoundsException- if the byte array is not of the right size
-
fromByteArray
Reads a UUID from a byte array with a specified offset. This is the inverse operation oftoByteArray(UUID, byte[], int)- Parameters:
byteArray- the byte array to read from, must be at least of length (offset + 16)offset- the offset after which to begin reading bytes- Returns:
- the UUID
- Throws:
IndexOutOfBoundsException- if the byte array is not of the right size
-