5e4f696b7f2390489f4598590d996b5043dfcb60
[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 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
18 /**
19  * Abstract superclass for sharing QName references, which can either be resolved {@link QName}s or unresolved
20  * {@link UnresolvedQName.Unqualified} and {@link UnresolvedQName.Qualified}s.
21  */
22 // FIXME: sealed to allow QName and UnresolvedQName only when we have JDK17+
23 @NonNullByDefault
24 public abstract class AbstractQName implements Identifier, WritableObject {
25     private static final long serialVersionUID = 1L;
26
27     private final String localName;
28
29     AbstractQName(final String localName) {
30         this.localName = requireNonNull(localName);
31     }
32
33     /**
34      * Returns YANG schema identifier which were defined for this node in the YANG module.
35      *
36      * @return YANG schema identifier which were defined for this node in the YANG module
37      */
38     public final String getLocalName() {
39         return localName;
40     }
41
42     /**
43      * Return an interned reference to an equivalent object.
44      *
45      * @return Interned reference, or this object if it was interned.
46      */
47     public abstract AbstractQName intern();
48
49     @Override
50     public abstract int hashCode();
51
52     @Override
53     public abstract boolean equals(@Nullable Object obj);
54
55     @Override
56     public abstract String toString();
57
58     /**
59      * Returns a QName with the specified namespace and the same local name as this one.
60      *
61      * @param namespace New namespace to use
62      * @return a QName with specified QNameModule and same local name as this one
63      * @throws NullPointerException if namespace is null
64      */
65     public QName bindTo(final QNameModule namespace) {
66         return new QName(namespace, getLocalName());
67     }
68
69     /**
70      * Check whether a string is a valid {@code localName}.
71      *
72      * @param str String to check
73      * @return True if the string usable as a local name, false otherwise
74      */
75     static final boolean isValidLocalName(final @Nullable String str) {
76         return str != null && !str.isEmpty() && checkContent(str);
77     }
78
79     abstract Object writeReplace();
80
81     static final String checkLocalName(final @Nullable String localName) {
82         checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
83         checkArgument(checkContent(localName), "String '%s' is not a valid identifier", localName);
84         return localName;
85     }
86
87     private static boolean checkContent(final String localName) {
88         return YangNames.IDENTIFIER_START.matches(localName.charAt(0))
89             && YangNames.NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1;
90     }
91 }