Do not use exceptions for branching
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / AbstractQName.java
index da1521b76df5092ad3630358b9a1754690c89ae9..d24ed22b1ce99c3d74f3e1dba811347ef3244539 100644 (file)
@@ -61,13 +61,36 @@ public abstract class AbstractQName implements Identifier, WritableObject {
     @Override
     public abstract String toString();
 
+    /**
+     * Returns a QName with the specified namespace and the same local name as this one.
+     *
+     * @param namespace New namespace to use
+     * @return a QName with specified QNameModule and same local name as this one
+     * @throws NullPointerException if namespace is null
+     */
+    public QName bindTo(final QNameModule namespace) {
+        return new QName(namespace, getLocalName());
+    }
+
+    /**
+     * Check whether a string is a valid {@code localName}.
+     *
+     * @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) {
+        return str != null && !str.isEmpty() && checkContent(str);
+    }
+
     abstract Object writeReplace();
 
     static final String checkLocalName(final @Nullable String localName) {
-        checkArgument(localName != null, "Parameter 'localName' may not be null.");
         checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
-        checkArgument(IDENTIFIER_START.matches(localName.charAt(0)) && NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1,
-                "String '%s' is not a valid identifier", localName);
+        checkArgument(checkContent(localName), "String '%s' is not a valid identifier", localName);
         return localName;
     }
+
+    private static boolean checkContent(final String localName) {
+        return IDENTIFIER_START.matches(localName.charAt(0)) && NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1;
+    }
 }