From: Robert Varga Date: Sun, 11 Mar 2018 21:06:02 +0000 (+0100) Subject: Add QName.withModule(QNameModule) method X-Git-Tag: release/carbon-sr4~5 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F20%2F70820%2F2;p=yangtools.git Add QName.withModule(QNameModule) method In situations when a companion QName with specified QNameModule needs to be created, we can side-step localName checking and just directly reuse it. QName.withModule() does precisely that. Change-Id: Ib1ca72210c0114610dafba0dfbbce15a2b8ddc03 Signed-off-by: Robert Varga (cherry picked from commit 010c1f2204d4f0408a10b81a51c14933522825ad) --- diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java index 771c008f47..40b6c70cfc 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java @@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.common; import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import com.google.common.collect.Interner; import com.google.common.collect.Interners; import java.io.Serializable; @@ -70,7 +69,7 @@ public final class QName implements Immutable, Serializable, Comparable { private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile(QNAME_STRING_NO_NAMESPACE_NO_REVISION); - private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&', ':' }; + private static final char[] ILLEGAL_CHARACTERS = { '?', '(', ')', '&', ':' }; // Non-null private final QNameModule module; @@ -97,12 +96,12 @@ public final class QName implements Immutable, Serializable, Comparable { private static String checkLocalName(final String localName) { Preconditions.checkArgument(localName != null, "Parameter 'localName' may not be null."); - Preconditions.checkArgument(!Strings.isNullOrEmpty(localName), "Parameter 'localName' must be a non-empty string."); + Preconditions.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(String.format( - "Parameter 'localName':'%s' contains illegal character '%s'", localName, c)); + throw new IllegalArgumentException("Parameter 'localName':'" + localName + + "' contains illegal character '" + c + "'"); } } return localName; @@ -294,7 +293,7 @@ public final class QName implements Immutable, Serializable, Comparable { try { return new URI(namespace); } catch (final URISyntaxException ue) { - throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue); + throw new IllegalArgumentException("Namespace '" + namespace + "' is not a valid URI", ue); } } @@ -347,6 +346,18 @@ public final class QName implements Immutable, Serializable, Comparable { /** * Creates copy of this with revision and prefix unset. + * Returns a QName with the specified QNameModule and the same localname as this one. + * + * @param newModule New QNameModule to use + * @return a QName with specified QNameModule and same local name as this one + */ + public QName withModule(@Nonnull final QNameModule newModule) { + return new QName(newModule, localName); + } + + /** + * Returns a QName with the same namespace and local name, but with no revision. If this QName does not have + * a Revision, this object is retured. * * @return copy of this QName with revision and prefix unset. */