Make QName use externalizable proxy pattern
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QName.java
index 4b52c0a2fd05322aa49ef18cc6459d92073a1fe3..2f6c2bf6c421d4bba45e0b0bb7044fb9380fdfce 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.base.CharMatcher;
 import com.google.common.collect.Interner;
 import com.google.common.collect.Interners;
 import java.io.DataInput;
@@ -30,14 +31,16 @@ import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.WritableObject;
 
 /**
- * The QName from XML consists of local name of element and XML namespace, but
- * for our use, we added module revision to it.
+ * The QName from XML consists of local name of element and XML namespace, but for our use, we added module revision to
+ * it.
  *
  * <p>
- * In YANG context QName is full name of defined node, type, procedure or
- * notification. QName consists of XML namespace, YANG model revision and local
- * name of defined type. It is used to prevent name clashes between nodes with
- * same local name, but from different schemas.
+ * In YANG context QName is full name of defined node, type, procedure or notification. QName consists of XML namespace,
+ * YANG model revision and local name of defined type. It is used to prevent name clashes between nodes with same local
+ * 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>.
  *
  * <ul>
  * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
@@ -47,11 +50,6 @@ import org.opendaylight.yangtools.concepts.WritableObject;
  * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
  * node in the YANG module</li>
  * </ul>
- *
- * <p>
- * QName may also have <code>prefix</code> assigned, but prefix does not
- * affect equality and identity of two QNames and carry only information
- * which may be useful for serializers / deserializers.
  */
 public final class QName implements Immutable, Serializable, Comparable<QName>, Identifier, WritableObject {
     private static final Interner<QName> INTERNER = Interners.newWeakInterner();
@@ -69,13 +67,16 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
     private static final String QNAME_STRING_NO_REVISION = "^\\((.+)\\)(.+)$";
     private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile(QNAME_STRING_NO_REVISION);
 
-    private static final char[] ILLEGAL_CHARACTERS = { '?', '(', ')', '&', ':' };
+    private static final CharMatcher IDENTIFIER_START =
+            CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z').or(CharMatcher.is('_'))).precomputed();
+    private static final CharMatcher NOT_IDENTIFIER_PART =
+            IDENTIFIER_START.or(CharMatcher.inRange('0', '9')).or(CharMatcher.anyOf("-.")).negate().precomputed();
 
     private final @NonNull QNameModule module;
     private final @NonNull String localName;
     private transient int hash = 0;
 
-    private QName(final @NonNull QNameModule module, final @NonNull String localName) {
+    private QName(final QNameModule module, final @NonNull String localName) {
         this.module = requireNonNull(module);
         this.localName = requireNonNull(localName);
     }
@@ -88,24 +89,19 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @param localName
      *            YANG schema identifier
      */
-    private QName(final @NonNull URI namespace, final @NonNull String localName) {
+    private QName(final URI namespace, final String localName) {
         this(QNameModule.create(namespace), checkLocalName(localName));
     }
 
-    private static @NonNull String checkLocalName(final @NonNull String localName) {
+    private static @NonNull String checkLocalName(final String localName) {
         checkArgument(localName != null, "Parameter 'localName' may not be null.");
         checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
-
-        for (final char c : ILLEGAL_CHARACTERS) {
-            if (localName.indexOf(c) != -1) {
-                throw new IllegalArgumentException("Parameter 'localName':'" + localName
-                    + "' contains illegal character '" + c + "'");
-            }
-        }
+        checkArgument(IDENTIFIER_START.matches(localName.charAt(0)) && NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1,
+                "String '%s' is not a valid identifier", localName);
         return localName;
     }
 
-    public static @NonNull QName create(final @NonNull String input) {
+    public static @NonNull QName create(final String input) {
         Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
         if (matcher.matches()) {
             final String namespace = matcher.group(1);
@@ -122,7 +118,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
         throw new IllegalArgumentException("Invalid input: " + input);
     }
 
-    public static @NonNull QName create(final @NonNull QName base, final @NonNull String localName) {
+    public static @NonNull QName create(final QName base, final String localName) {
         return create(base.getModule(), localName);
     }
 
@@ -135,7 +131,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      *            Local name part of QName. MUST NOT BE null.
      * @return Instance of QName
      */
-    public static @NonNull QName create(final @NonNull QNameModule qnameModule, final @NonNull String localName) {
+    public static @NonNull QName create(final QNameModule qnameModule, final String localName) {
         return new QName(requireNonNull(qnameModule, "module may not be null"), checkLocalName(localName));
     }
 
@@ -147,8 +143,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @param localName Local name part of QName. MUST NOT BE null.
      * @return Instance of QName
      */
-    public static @NonNull QName create(final @NonNull URI namespace, final @Nullable Revision revision,
-            final @NonNull String localName) {
+    public static @NonNull QName create(final URI namespace, final @Nullable Revision revision,
+            final String localName) {
         return create(QNameModule.create(namespace, revision), localName);
     }
 
@@ -160,8 +156,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @param localName Local name part of QName. MUST NOT BE null.
      * @return Instance of QName
      */
-    public static QName create(final @NonNull URI namespace, final @NonNull Optional<Revision> revision,
-            final @NonNull String localName) {
+    public static @NonNull QName create(final URI namespace, final Optional<Revision> revision,
+            final String localName) {
         return create(QNameModule.create(namespace, revision), localName);
     }
 
@@ -173,8 +169,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @param localName Local name part of QName. MUST NOT BE null.
      * @return Instance of QName
      */
-    public static @NonNull QName create(final @NonNull String namespace, final @NonNull String localName,
-            final Revision revision) {
+    public static @NonNull QName create(final String namespace, final String localName, final Revision revision) {
         return create(QNameModule.create(parseNamespace(namespace), revision), localName);
     }
 
@@ -189,8 +184,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @throws IllegalArgumentException If {@code namespace} is not valid URI or {@code revision} does not conform
      *         to {@code YYYY-mm-dd}.
      */
-    public static @NonNull QName create(final @NonNull String namespace, final @NonNull String revision,
-            final @NonNull String localName) {
+    public static @NonNull QName create(final String namespace, final String revision, final String localName) {
         return create(parseNamespace(namespace), Revision.of(revision), localName);
     }
 
@@ -203,7 +197,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @throws NullPointerException If any of parameters is null.
      * @throws IllegalArgumentException If {@code namespace} is not valid URI.
      */
-    public static @NonNull QName create(final @NonNull String namespace, final @NonNull String localName) {
+    public static @NonNull QName create(final String namespace, final String localName) {
         return create(parseNamespace(namespace), localName);
     }
 
@@ -216,7 +210,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @throws NullPointerException If any of parameters is null.
      * @throws IllegalArgumentException If <code>namespace</code> is not valid URI.
      */
-    public static @NonNull QName create(final @NonNull URI namespace, final @NonNull String localName) {
+    public static @NonNull QName create(final URI namespace, final String localName) {
         return new QName(namespace, localName);
     }
 
@@ -227,7 +221,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @return A QName instance
      * @throws IOException if I/O error occurs
      */
-    public static QName readFrom(final @NonNull DataInput in) throws IOException {
+    public static QName readFrom(final DataInput in) throws IOException {
         final QNameModule module = QNameModule.readFrom(in);
         return new QName(module, checkLocalName(in.readUTF()));
     }
@@ -282,7 +276,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
         // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
         // interner. It is important that it does not hold user-supplied reference (such a String instance from
         // parsing of an XML document).
-        final QName template = cacheMod == module ? this : QName.create(cacheMod, localName.intern());
+        final QName template = cacheMod == module ? this : new QName(cacheMod, localName.intern());
 
         return INTERNER.intern(template);
     }
@@ -296,12 +290,12 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
     }
 
     /**
-     * Compares the specified object with this list for equality.  Returns <tt>true</tt> if and only if the specified
+     * Compares the specified object with this list for equality.  Returns {@code true} if and only if the specified
      * object is also instance of {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
      * {@link #getRevision()} are equals to same properties of this instance.
      *
      * @param obj the object to be compared for equality with this QName
-     * @return <tt>true</tt> if the specified object is equal to this QName
+     * @return {@code true} if the specified object is equal to this QName
      */
     @Override
     public boolean equals(final Object obj) {
@@ -315,7 +309,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
         return Objects.equals(localName, other.localName) && module.equals(other.module);
     }
 
-    private static @NonNull URI parseNamespace(final @NonNull String namespace) {
+    private static @NonNull URI parseNamespace(final String namespace) {
         try {
             return new URI(namespace);
         } catch (final URISyntaxException ue) {
@@ -335,8 +329,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
             }
             sb.append(QNAME_RIGHT_PARENTHESIS);
         }
-        sb.append(localName);
-        return sb.toString();
+        return sb.append(localName).toString();
     }
 
     /**
@@ -345,7 +338,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @param newModule New QNameModule to use
      * @return a QName with specified QNameModule and same local name as this one
      */
-    public @NonNull QName withModule(final @NonNull QNameModule newModule) {
+    public @NonNull QName withModule(final QNameModule newModule) {
         return new QName(newModule, localName);
     }
 
@@ -356,7 +349,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @return a QName with the same namespace and local name, but with no revision.
      */
     public @NonNull QName withoutRevision() {
-        return getRevision().isPresent() ? new QName(module.withoutRevision(), localName) : this;
+        final QNameModule newModule;
+        return (newModule = module.withoutRevision()) == module ? this : new QName(newModule, localName);
     }
 
     /**
@@ -369,7 +363,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @param revision Date object to format
      * @return String representation or null if the input was null.
      */
-    public static @Nullable String formattedRevision(final @NonNull Optional<Revision> revision) {
+    public static @Nullable String formattedRevision(final Optional<Revision> revision) {
         return revision.map(Revision::toString).orElse(null);
     }
 
@@ -384,7 +378,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
      * @return true if this instance and other have equals localName and namespace.
      * @throws NullPointerException if {@code other} is null.
      */
-    public boolean isEqualWithoutRevision(final @NonNull QName other) {
+    public boolean isEqualWithoutRevision(final QName other) {
         return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
     }
 
@@ -406,4 +400,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName>,
         module.writeTo(out);
         out.writeUTF(localName);
     }
+
+    Object writeReplace() {
+        return new QNv1(this);
+    }
 }