Publish identifier matches
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / AbstractQName.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.concepts.Identifier;
17 import org.opendaylight.yangtools.concepts.WritableObject;
18
19 /**
20  * Abstract superclass for sharing QName references, which can either be resolved {@link QName}s or unresolved
21  * {@link UnqualifiedQName} and {@link QualifiedQName}s.
22  */
23 @Beta
24 @NonNullByDefault
25 public abstract class AbstractQName implements Identifier, WritableObject {
26     private static final long serialVersionUID = 1L;
27
28     private final String localName;
29
30     AbstractQName(final String localName) {
31         this.localName = requireNonNull(localName);
32     }
33
34     /**
35      * Returns YANG schema identifier which were defined for this node in the YANG module.
36      *
37      * @return YANG schema identifier which were defined for this node in the YANG module
38      */
39     public final String getLocalName() {
40         return localName;
41     }
42
43     /**
44      * Return an interned reference to an equivalent object.
45      *
46      * @return Interned reference, or this object if it was interned.
47      */
48     public abstract AbstractQName intern();
49
50     @Override
51     public abstract int hashCode();
52
53     @Override
54     public abstract boolean equals(@Nullable Object obj);
55
56     @Override
57     public abstract String toString();
58
59     /**
60      * Returns a QName with the specified namespace and the same local name as this one.
61      *
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
65      */
66     public QName bindTo(final QNameModule namespace) {
67         return new QName(namespace, getLocalName());
68     }
69
70     /**
71      * Check whether a string is a valid {@code localName}.
72      *
73      * @param str String to check
74      * @return True if the string usable as a local name, false otherwise
75      */
76     static final boolean isValidLocalName(final @Nullable String str) {
77         return str != null && !str.isEmpty() && checkContent(str);
78     }
79
80     abstract Object writeReplace();
81
82     static final String checkLocalName(final @Nullable String localName) {
83         checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
84         checkArgument(checkContent(localName), "String '%s' is not a valid identifier", localName);
85         return localName;
86     }
87
88     private static boolean checkContent(final String localName) {
89         return YangNames.IDENTIFIER_START.matches(localName.charAt(0))
90             && YangNames.NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1;
91     }
92 }