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 com.google.common.annotations.Beta;
14 import com.google.common.base.CharMatcher;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Identifier;
18 import org.opendaylight.yangtools.concepts.WritableObject;
21 * Abstract superclass for sharing QName references, which can either be resolved {@link QName}s or unresolved
22 * {@link UnqualifiedQName} and {@link QualifiedQName}s.
26 public abstract class AbstractQName implements Identifier, WritableObject {
27 private static final long serialVersionUID = 1L;
28 private static final CharMatcher IDENTIFIER_START =
29 CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z').or(CharMatcher.is('_'))).precomputed();
30 private static final CharMatcher NOT_IDENTIFIER_PART =
31 IDENTIFIER_START.or(CharMatcher.inRange('0', '9')).or(CharMatcher.anyOf("-.")).negate().precomputed();
33 private final String localName;
35 AbstractQName(final String localName) {
36 this.localName = requireNonNull(localName);
40 * Returns YANG schema identifier which were defined for this node in the YANG module.
42 * @return YANG schema identifier which were defined for this node in the YANG module
44 public final String getLocalName() {
49 * Return an interned reference to an equivalent object.
51 * @return Interned reference, or this object if it was interned.
53 public abstract AbstractQName intern();
56 public abstract int hashCode();
59 public abstract boolean equals(@Nullable Object obj);
62 public abstract String toString();
65 * Returns a QName with the specified namespace and the same local name as this one.
67 * @param namespace New namespace to use
68 * @return a QName with specified QNameModule and same local name as this one
69 * @throws NullPointerException if namespace is null
71 public QName bindTo(final QNameModule namespace) {
72 return new QName(namespace, getLocalName());
76 * Check whether a string is a valid {@code localName}.
78 * @param str String to check
79 * @return True if the string usable as a local name, false otherwise
81 static final boolean isValidLocalName(final @Nullable String str) {
82 return str != null && !str.isEmpty() && checkContent(str);
85 abstract Object writeReplace();
87 static final String checkLocalName(final @Nullable String localName) {
88 checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
89 checkArgument(checkContent(localName), "String '%s' is not a valid identifier", localName);
93 private static boolean checkContent(final String localName) {
94 return IDENTIFIER_START.matches(localName.charAt(0)) && NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1;