Use RevisionUnion in QNameModule
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QName.java
index d836d9287d8ad86d814deafe13e0de13e44d9aef..5a291d9f88339a19bd97544d5a395c59bbda1b04 100644 (file)
@@ -15,6 +15,7 @@ import com.google.common.collect.Interners;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.io.Serial;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.regex.Matcher;
@@ -33,7 +34,7 @@ import org.eclipse.jdt.annotation.Nullable;
  * name, but from different schemas.
  *
  * <p>
- * The local name must conform to <a href="https://tools.ietf.org/html/rfc7950#section-6.2">RFC7950 Section 6.2</a>.
+ * The local name must conform to <a href="https://www.rfc-editor.org/rfc/rfc7950#section-6.2">RFC7950 Section 6.2</a>.
  *
  * <ul>
  * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
@@ -45,33 +46,9 @@ import org.eclipse.jdt.annotation.Nullable;
  * </ul>
  */
 public final class QName extends AbstractQName implements Comparable<QName> {
-    /**
-     * A {@link DataInput} which has an understanding of {@link QName}'s semantics.
-     */
-    @Beta
-    public interface QNameAwareDataInput extends DataInput {
-        /**
-         * Read a {@link QName} from the stream.
-         *
-         * @return A QName
-         * @throws IOException if an I/O error occurs.
-         */
-        @NonNull QName readQName() throws IOException;
-    }
-
-    @Beta
-    public interface QNameAwareDataOutput extends DataOutput {
-        /**
-         * Write a {@link QName} into the stream.
-         *
-         * @param qname A QName
-         * @throws  IOException if an I/O error occurs.
-         */
-        void writeQName(@NonNull QName qname) throws IOException;
-    }
-
     private static final Interner<QName> INTERNER = Interners.newWeakInterner();
     // Note: 5398411242927766414L is used for versions < 3.0.0 without writeReplace
+    @Serial
     private static final long serialVersionUID = 1L;
 
     @Regex
@@ -99,7 +76,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      *            YANG schema identifier
      */
     private QName(final XMLNamespace namespace, final String localName) {
-        this(QNameModule.create(namespace), checkLocalName(localName));
+        this(QNameModule.of(namespace), checkLocalName(localName));
     }
 
     public static @NonNull QName create(final String input) {
@@ -146,7 +123,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      */
     public static @NonNull QName create(final XMLNamespace namespace, final @Nullable Revision revision,
             final String localName) {
-        return create(QNameModule.create(namespace, revision), localName);
+        return create(QNameModule.ofRevision(namespace, revision), localName);
     }
 
     /**
@@ -159,7 +136,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      */
     public static @NonNull QName create(final XMLNamespace namespace, final Optional<Revision> revision,
             final String localName) {
-        return create(QNameModule.create(namespace, revision), localName);
+        return create(QNameModule.ofRevision(namespace, revision.orElse(null)), localName);
     }
 
     /**
@@ -171,7 +148,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      * @return Instance of QName
      */
     public static @NonNull QName create(final String namespace, final String localName, final Revision revision) {
-        return create(QNameModule.create(XMLNamespace.of(namespace), revision), localName);
+        return create(QNameModule.of(XMLNamespace.of(namespace), revision), localName);
     }
 
     /**
@@ -186,7 +163,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      *         to {@code YYYY-mm-dd}.
      */
     public static @NonNull QName create(final String namespace, final String revision, final String localName) {
-        return create(XMLNamespace.of(namespace), Revision.of(revision), localName);
+        return create(QNameModule.ofRevision(namespace, revision), localName);
     }
 
     /**
@@ -227,7 +204,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
             return aware.readQName();
         }
 
-        final QNameModule module = QNameModule.readFrom(in);
+        final var module = QNameModule.readFrom(in);
         return new QName(module, checkLocalName(in.readUTF()));
     }
 
@@ -263,7 +240,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      * @return XMLNamespace assigned to the YANG module.
      */
     public @NonNull XMLNamespace getNamespace() {
-        return module.getNamespace();
+        return module.namespace();
     }
 
     /**
@@ -272,7 +249,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      * @return revision of the YANG module if the module has defined revision.
      */
     public @NonNull Optional<Revision> getRevision() {
-        return module.getRevision();
+        return module.findRevision();
     }
 
     @Override
@@ -315,7 +292,7 @@ public final class QName extends AbstractQName implements Comparable<QName> {
         final StringBuilder sb = new StringBuilder().append('(').append(getNamespace());
         final Optional<Revision> rev = getRevision();
         if (rev.isPresent()) {
-            sb.append("?revision=").append(rev.get());
+            sb.append("?revision=").append(rev.orElseThrow());
         }
         return sb.append(')').append(getLocalName()).toString();
     }