All typed UUIDs implemented (except v2).
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / NameUuid.java
index a1c64a1a12b80b55f512b6436245ae95839f9707..376eea4bcf671e7ec2c6d5a78622107ec308706b 100644 (file)
@@ -1,16 +1,38 @@
 package org.argeo.api.uuid;
 
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.UUID;
 
-/** A name {@link UUID} whose values used for its construction are known. */
-public class NameUuid extends UnkownNameUuid {
+/** A name UUID whose values used for its construction are known. */
+public class NameUuid extends BasicNameUuid {
        private static final long serialVersionUID = APM.SERIAL;
 
        protected final TypedUuid namespace;
        protected final String name;
        protected final Charset encoding;
 
+       /**
+        * Default static builder which creates a v5 (SHA1) name based {@link UUID},
+        * using UTF-8 encoding. Use
+        * {@link #NameUuid(TypedUuid, String, Charset, boolean)} for more options.
+        */
+       public NameUuid(TypedUuid namespace, String name) {
+               this(namespace, name, StandardCharsets.UTF_8, true);
+       }
+
+       /** Static builder (an {@link TypedUuidFactory} may be more efficient). */
+       public NameUuid(TypedUuid namespace, String name, Charset encoding, boolean sha1) {
+               this(sha1 ? AbstractUuidFactory.createNameUUIDv5Static(namespace.uuid, name.getBytes(encoding))
+                               : AbstractUuidFactory.createNameUUIDv5Static(namespace.uuid, name.getBytes(encoding)), namespace, name,
+                               encoding);
+       }
+
+       /**
+        * Since no check is performed, the constructor is protected so that the object
+        * can only be built by the default methods of {@link TypedUuidFactory} (in this
+        * package) or by extending the class.
+        */
        protected NameUuid(UUID uuid, TypedUuid namespace, String name, Charset encoding) {
                super(uuid);
                assert namespace != null;
@@ -21,10 +43,6 @@ public class NameUuid extends UnkownNameUuid {
                this.encoding = encoding;
        }
 
-       public static long getSerialversionuid() {
-               return serialVersionUID;
-       }
-
        /** The namespace used to build this UUID. */
        public final TypedUuid getNamespace() {
                return namespace;