X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FGUID.java;fp=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FGUID.java;h=a9a5af17d098ddd9c8bb0cb46692c0b75d1d0e28;hb=593a4eabb76b74cd382ecf3f181d57abe0d643f9;hp=0000000000000000000000000000000000000000;hpb=e8e2c65f356b30b35e4d8a1de66691a789c183bb;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/GUID.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/GUID.java new file mode 100644 index 000000000..a9a5af17d --- /dev/null +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/GUID.java @@ -0,0 +1,52 @@ +package org.argeo.api.uuid; + +import java.util.UUID; + +/** + * A variant 6 {@link UUID}. + * + * @see https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1 + */ +public class GUID extends TypedUuid { + private static final long serialVersionUID = APM.SERIAL; + + /** Constructor based on a {@link UUID}. */ + public GUID(UUID uuid) { + super(uuid); + if (uuid.variant() != 6) + throw new IllegalArgumentException("The provided UUID is not a GUID."); + } + + /** + * Formats N, D, B, P are supported: + * + * + * @see https://docs.microsoft.com/en-us/dotnet/api/system.guid.tostring + */ + public static String toString(UUID uuid, char format, boolean upperCase) { + String str; + switch (format) { + case 'D': + str = uuid.toString(); + break; + case 'N': + str = UuidBinaryUtils.toCompact(uuid); + break; + case 'B': + str = "{" + uuid.toString() + "}"; + break; + case 'P': + str = "(" + uuid.toString() + ")"; + break; + default: + throw new IllegalArgumentException("Unsupported format : " + format); + } + return upperCase ? str.toUpperCase() : str; + } + +}