Make QName/QNameModule a WritableObject
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QNameModule.java
index b415834e9e97574919e375e300638711d7c4236a..4a3eadd4fd156b5051c89858217298af0a4a2e98 100644 (file)
@@ -12,6 +12,9 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.base.MoreObjects;
 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.Serializable;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -21,8 +24,9 @@ import javax.annotation.Nullable;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.concepts.WritableObject;
 
-public final class QNameModule implements Comparable<QNameModule>, Immutable, Serializable, Identifier {
+public final class QNameModule implements Comparable<QNameModule>, Immutable, Serializable, Identifier, WritableObject {
     private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
     private static final long serialVersionUID = 3L;
 
@@ -79,6 +83,20 @@ public final class QNameModule implements Comparable<QNameModule>, Immutable, Se
         return new QNameModule(namespace, revision);
     }
 
+    /**
+     * Read a QNameModule from a DataInput. The format is expected to match the output format
+     * of {@link #writeTo(DataOutput)}.
+     *
+     * @param in DataInput to read
+     * @return A QNameModule instance
+     * @throws IOException if I/O error occurs
+     */
+    public static QNameModule readFrom(final DataInput in) throws IOException {
+        final String namespace = in.readUTF();
+        final String revision = in.readUTF();
+        return new QNameModule(URI.create(namespace), revision.isEmpty() ? null : Revision.of(revision));
+    }
+
     /**
      * Returns the namespace of the module which is specified as argument of
      * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
@@ -109,6 +127,12 @@ public final class QNameModule implements Comparable<QNameModule>, Immutable, Se
         return Revision.compare(revision, o.revision);
     }
 
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        out.writeUTF(namespace.toString());
+        out.writeUTF(revision == null ? "" : revision.toString());
+    }
+
     @Override
     public int hashCode() {
         if (hash == 0) {