2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.common;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Identifier;
16 import org.opendaylight.yangtools.concepts.WritableObject;
17 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
20 * Abstract superclass for sharing QName references, which can either be resolved {@link QName}s or unresolved
21 * {@link UnresolvedQName.Unqualified} and {@link UnresolvedQName.Qualified}s.
24 public abstract sealed class AbstractQName implements Identifier, WritableObject permits QName, UnresolvedQName {
26 private static final long serialVersionUID = 1L;
28 private final String localName;
30 AbstractQName(final String localName) {
31 this.localName = requireNonNull(localName);
35 * Returns YANG schema identifier which were defined for this node in the YANG module.
37 * @return YANG schema identifier which were defined for this node in the YANG module
39 public final String getLocalName() {
44 * Return an interned reference to an equivalent object.
46 * @return Interned reference, or this object if it was interned.
48 public abstract AbstractQName intern();
51 public abstract int hashCode();
54 public abstract boolean equals(@Nullable Object obj);
57 public abstract String toString();
60 * Returns a QName with the specified namespace and the same local name as this one.
62 * @param namespace New namespace to use
63 * @return a QName with specified QNameModule and same local name as this one
64 * @throws NullPointerException if namespace is null
66 public QName bindTo(final QNameModule namespace) {
67 return new QName(namespace, localName);
71 * Returns an {@link Unqualified} identifier formed by capturing {@link #getLocalName()}.
73 * @return An unqualified {@link UnresolvedQName}
75 public Unqualified unbind() {
76 return new Unqualified(localName);
80 * Check whether a string is a valid {@code localName}.
82 * @param str String to check
83 * @return True if the string usable as a local name, false otherwise
85 static final boolean isValidLocalName(final @Nullable String str) {
86 return str != null && !str.isEmpty() && checkContent(str);
90 abstract Object writeReplace();
92 static final String checkLocalName(final @Nullable String localName) {
93 checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
94 checkArgument(checkContent(localName), "String '%s' is not a valid identifier", localName);
98 private static boolean checkContent(final String localName) {
99 return YangNames.IDENTIFIER_START.matches(localName.charAt(0))
100 && YangNames.NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1;