Use RevisionUnion in QNameModule
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / XMLNamespace.java
index 3e98f06e51285aceae9071d7714b0e40367f49be..1803854c749740f1426043d0d23e70082fe17257 100644 (file)
@@ -7,25 +7,35 @@
  */
 package org.opendaylight.yangtools.yang.common;
 
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.Interner;
 import com.google.common.collect.Interners;
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
 import java.io.Serial;
 import java.io.Serializable;
 import java.net.URI;
 import java.net.URISyntaxException;
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.concepts.WritableObject;
 
 /**
  * A simple type capture of {@code namespace} statement's argument according to
  * <a href="https://www.rfc-editor.org/rfc/rfc6020#section-7.1.3">RFC6020</a>.
  */
-public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable, Serializable {
-    private static final Interner<XMLNamespace> INTERNER = Interners.newWeakInterner();
-    @Serial
+@NonNullByDefault
+public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable, Serializable, WritableObject {
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
+    private static final Interner<XMLNamespace> INTERNER = Interners.newWeakInterner();
 
     private final String namespace;
 
@@ -34,10 +44,10 @@ public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable,
     }
 
     // FIXME: add documentation
-    public static @NonNull XMLNamespace of(final String namespace) {
+    public static XMLNamespace of(final String namespace) {
         try {
-            // Validation only
-            new URI(namespace);
+            // FIXME: we want this validation, can we get it without the object allocation?
+            verifyNotNull(new URI(namespace));
         } catch (final URISyntaxException e) {
             throw new IllegalArgumentException("Namespace '" + namespace + "' is not a valid URI", e);
         }
@@ -50,7 +60,7 @@ public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable,
      *
      * @return Interned reference, or this object if it was interned.
      */
-    public @NonNull XMLNamespace intern() {
+    public XMLNamespace intern() {
         return INTERNER.intern(this);
     }
 
@@ -60,13 +70,26 @@ public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable,
         return namespace.compareTo(o.namespace);
     }
 
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        out.writeUTF(namespace);
+    }
+
+    public static XMLNamespace readFrom(final DataInput in) throws IOException {
+        try {
+            return of(in.readUTF());
+        } catch (IllegalArgumentException e) {
+            throw new IOException("Invalid namespace", e);
+        }
+    }
+
     @Override
     public int hashCode() {
         return namespace.hashCode();
     }
 
     @Override
-    public boolean equals(final Object obj) {
+    public boolean equals(final @Nullable Object obj) {
         return this == obj || obj instanceof XMLNamespace other && namespace.equals(other.namespace);
     }
 
@@ -79,4 +102,19 @@ public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable,
     Object writeReplace() {
         return new XNv1(this);
     }
+
+    @java.io.Serial
+    private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+        Revision.throwNSE();
+    }
+
+    @java.io.Serial
+    private void readObjectNoData() throws ObjectStreamException {
+        Revision.throwNSE();
+    }
+
+    @java.io.Serial
+    private void writeObject(final ObjectOutputStream stream) throws IOException {
+        Revision.throwNSE();
+    }
 }