Add UnqualifiedQName.tryCreate() 77/94077/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Dec 2020 12:37:39 +0000 (13:37 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Dec 2020 13:02:05 +0000 (14:02 +0100)
This a better approach to performing a lazy check, as it captures
the valid string in a well-known construct.

JIRA: YANGTOOLS-1191
Change-Id: I7ffe00602d3d83189d9c3f8a59365648aa36720d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/AbstractQName.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnqualifiedQName.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/extension/UnrecognizedEffectiveStatementImpl.java

index d24ed22b1ce99c3d74f3e1dba811347ef3244539..d86c5c407f4308a3aea39f9ebe67bc1dec549d38 100644 (file)
@@ -78,7 +78,7 @@ public abstract class AbstractQName implements Identifier, WritableObject {
      * @param str String to check
      * @return True if the string usable as a local name, false otherwise
      */
-    public static final boolean isValidLocalName(final @Nullable String str) {
+    static final boolean isValidLocalName(final @Nullable String str) {
         return str != null && !str.isEmpty() && checkContent(str);
     }
 
index 0b6bffb54e2e65442683294a9e83ed23ffa412e3..821bc910a1711ea518154bd1dfb81da877d3d5e6 100644 (file)
@@ -44,6 +44,17 @@ public final class UnqualifiedQName extends AbstractQName implements Comparable<
         return new UnqualifiedQName(checkLocalName(localName));
     }
 
+    /**
+     * Create a new unqualified QName.
+     *
+     * @param localName The local name of this unqualified QName
+     * @return An UnqualifiedQName instance, or null if localName is not valid
+     */
+    @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Non-grok of @Nullable")
+    public static @Nullable UnqualifiedQName tryCreate(final String localName) {
+        return isValidLocalName(localName) ? new UnqualifiedQName(localName) : null;
+    }
+
     /**
      * Read an UnqualifiedQName from a DataInput. The format is expected to match the output format of
      * {@link #writeTo(DataOutput)}.
index 1f02d8b98a356d031e8e7cfdf246c6b67d51e33f..c34936e64b40ee1452c4b5a477d61998e77b2bbe 100644 (file)
@@ -9,9 +9,9 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.extension;
 
 import com.google.common.collect.ImmutableList;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.AbstractQName;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
@@ -82,10 +82,8 @@ final class UnrecognizedEffectiveStatementImpl extends UnknownEffectiveStatement
 
         final int colon = value.indexOf(':');
         if (colon == -1) {
-            if (AbstractQName.isValidLocalName(value)) {
-                return QName.unsafeOf(StmtContextUtils.getRootModuleQName(stmt), value).intern();
-            }
-            return null;
+            final UnqualifiedQName qname = UnqualifiedQName.tryCreate(value);
+            return qname == null ? null : qname.bindTo(StmtContextUtils.getRootModuleQName(stmt)).intern();
         }
 
         final QNameModule qnameModule = StmtContextUtils.getModuleQNameByPrefix(stmt, value.substring(0, colon));